问题描述
我用 Alice 公钥加密纯文本,但我想用 Bob 私钥而不是 Alice 私钥解密这个 Alice 密文。为此,我使用了代理重新加密技术。
proxy_key = (Bob_public_key)*(Alice_public_key.modInverse(prime_number)).mod(prime_number)
public static void main(String[] args) throws UnsupportedEncodingException {
char ch[]=new char[16];
String s = new String();
BigInteger phi1=new BigInteger("31");
//Alice private key= "pri1" and public key ="pub1"
String pri1 = "0000000000000007"; // 128 bit key
String pub1 = "0000000000000011";
//Encryption with Alice private and public key
String Cipher=encrypt(pri1,pub1,"Hellow World");
System.out.println("encrypted text = "+Cipher);
// CONVERT Alice public key "pub1" from String to BigInteger data type
BigInteger pk1= new BigInteger(pub1) ;
//Bob private key= "pri2" and public key ="pub2"
String pri2 = "0000000000000013"; // 128 bit key
String pub2 = "0000000000000017";
// CONVERT Bob public key "pub2" from String to BigInteger data type
BigInteger pk2= new BigInteger(pub2) ;
//Create ProxyKey (proxy_key = bobs_public_key *(Alice_public_key.modInverse(random_value).mod(ranom_value)
BigInteger proxy_key = pk2.multiply(pk1.modInverse(phi1)).mod(phi1);
//The blow code convert proxy_key which value is "1" to "0000000000000001" 128 bit key
s= String.valueOf(proxy_key);
byte[] bytes = s.getBytes("US-ASCII");
byte[] bytes1 =new byte[16];
for (int a=0;a<16;a++) {
if (a==15)
bytes1[a]=bytes[0];
else
bytes1[a]='0';
ch[a]= (char) (bytes1[a]);
}
String Proxy_re_encryption_key = String.copyValueOf(ch);
System.out.println("the value of Proxy Re Encryption key ="+Proxy_re_encryption_key);
//Encryption of Cipher with Proxy Re Encryption key
String Cipher2=encrypt(pri1,Proxy_re_encryption_key,Cipher);
System.out.println("2nd encrypted text = "+Cipher2);
//Decryption the 2nd cipher text with bobs public and private key
System.out.println("Real Text = "+decrypt(pri2,pub2,Cipher2));
}
但是我出错了
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can
arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:977)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1058)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:855)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2207)
at org.algorithm.work.junaid.EncryptionExample.decrypt(EncryptionExample.java:38)
at org.algorithm.work.junaid.EncryptionExample.main(EncryptionExample.java:95)
加解密码
package org.algorithm.work.junaid;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class EncryptionExample {
public static String encrypt(String key1,String key2,String value) {
try {
IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF8"));
SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec,iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string:"
+ Base64.encodeBase64String(encrypted));
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printstacktrace();
}
return null;
}
public static String decrypt(String key1,String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE,iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printstacktrace();
}
return null;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)