[英]RSA encryption Android and decryption in Java: javax.crypto.BadPaddingException: Decryption error
I have an Android app which encrypts a text using RSA 2048 bit key. 我有一个Android应用程序,该应用程序使用RSA 2048位密钥对文本进行加密。
Encryption and Decryption are working fine on the app. 加密和解密在该应用上正常运行。
My problem is that i am trying to encrypt on the application and then send the cipher to an external Java program for decryption. 我的问题是我试图在应用程序上进行加密,然后将密码发送到外部Java程序进行解密。
Here is how it is done: 这是完成的过程:
public static PrivateKey getPrivateKey(String key) {
try {
/* Add PKCS#8 formatting */
byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(new ASN1Integer(0));
ASN1EncodableVector v2 = new ASN1EncodableVector();
v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));
v2.add(DERNull.INSTANCE);
v.add(new DERSequence(v2));
v.add(new DEROctetString(byteKey));
ASN1Sequence seq = new DERSequence(v);
byte[] privKey = seq.getEncoded("DER");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(keySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String cipherString,PrivateKey privateKey){
try{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plainByte = cipher.doFinal(Base64.getDecoder().decode(cipherString));
return Base64.getEncoder().encodeToString(plainByte);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
But when i am trying to decryption the cipher i am getting error: 但是当我尝试解密密码时,我得到了错误:
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at RSA.decrypt(RSA.java:94)
at RSA.main(RSA.java:116)
what am i missing? 我想念什么?
UPDATE i regenerated a new pair of keys within Java itself and got rid of the part: 更新我在Java本身内重新生成了一对新的密钥,并且摆脱了这一部分:
/* Add PKCS#8 formatting */
byte[] byteKey = Base64.getDecoder().decode(key.getBytes());
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(new ASN1Integer(0));
ASN1EncodableVector v2 = new ASN1EncodableVector();
v2.add(new ASN1ObjectIdentifier(PKCSObjectIdentifiers.rsaEncryption.getId()));
v2.add(DERNull.INSTANCE);
v.add(new DERSequence(v2));
v.add(new DEROctetString(byteKey));
ASN1Sequence seq = new DERSequence(v);
byte[] privKey = seq.getEncoded("DER");
but I'm stuck with the same error! 但是我遇到了同样的错误!
Your padding is either not being set or set wrong. 您的填充没有设置或设置错误。
Try changing Cipher cipher = Cipher.getInstance("RSA");
尝试更改
Cipher cipher = Cipher.getInstance("RSA");
to Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
到
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
If that doesn't work you can see here for other padding modes. 如果这不起作用,您可以在这里看到其他填充模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.