Java:RSA中的错误填充异常
我正在运行以下代码,我得到了一个Java:RSA中的错误填充异常,java,encryption,rsa,private-key,public-key,Java,Encryption,Rsa,Private Key,Public Key,我正在运行以下代码,我得到了一个错误填充异常-数据必须以零开始。有什么想法吗 这是密码 public class test { public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, NoSuchPaddingException
错误填充异常-数据必须以零开始。有什么想法吗
这是密码
public class test {
public static void main(String[] args) throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidKeyException,
SignatureException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException {
KeyPair k=Utilities.keys(2048);
PublicKey public_key=Utilities.public_key(k);
PrivateKey private_key=Utilities.private_key(k);
String hex1=Lib.stringToHex("test1/test2/test3");
byte[] plaintext=Lib.toByteArray(hex1);
byte [] cipher=Utilities.Encrypt_RSA(plaintext, public_key);
System.out.println(cipher.length);
byte[] newArray = new byte[128];
byte[] newArray2 = new byte[128];
// System.arraycopy(cipher, 0, newArray, 0, 512);
for (int i=0;i<cipher.length;i++){
if(i<cipher.length/2){
newArray[i]=cipher[i];
}
else if((i>cipher.length/2)&&(i<cipher.length)){
newArray2[i-128]=cipher[i];
}
}
// System.arraycopy(cipher, 512, newArray2, 0, 512);
byte[] cipher2=Utilities.Encrypt_RSA_Pr(newArray, private_key);
byte[] cipher3=Utilities.Encrypt_RSA_Pr(newArray2, private_key);
System.out.println(cipher2.length+" cipher2");
System.out.println(cipher3.length+" cipher3");
byte [] plain1=Utilities.Decrypt_RSA_Pub(cipher2,public_key);
byte [] plain2=Utilities.Decrypt_RSA_Pub(cipher3,public_key);
System.out.println(plain1.length);
System.out.println(plain2.length);
byte[] finald=new byte[256];
for(int i=0;i<256;i++){
if(i<128){
finald[i]=plain1[i];
}
else{
finald[i]=plain2[i-128];
}
}
for(int i=0;i<plain1.length;i++){
if(i>=64){
plain1[i]='\0';
}
}
System.out.println(plain1.length);
System.out.println(finald.length;
byte[] plainfinal=Utilities.Decrypt_RSA(finald, private_key);
你本想把密码一分为二,但你做错了。我怀疑这句话:
else if((i>cipher.length/2)&&(i<cipher.length)){
else if((i>cipher.length/2)和&(i=cipher.length/2)和&(i=cipher.length/2)异常到底发生在哪里?您是否检查了传入的内容以及格式是否正确?它运行正常,直到行:byte[]plainfinal=Utilities.Decrypt_RSA(finald,private_key);并引发异常。当密钥或密文无效时,应该会发生此错误。除了签名的大小之外,任何东西都可能出错。因此,这归结为调试上述代码。通常我会在调试器中运行它,但我不确定您在这里试图实现什么,我认为这不是很有用对任何其他人来说都很有用…问题可能是因为您使用私钥加密,在这种情况下很可能选择了错误的填充(用于签名)。您是否正在检查finald
是否等于cipher
?它不会。。。
else if((i>cipher.length/2)&&(i<cipher.length)){
else if((i>=cipher.length/2)&&(i<cipher.length)){