I am using RSA to encrypt a secret and then sending it to another host. The code works perfectly when it's done on PC -> PC traffic, but throws the following Exception for Android -> PC traffic.
The javax.crypto.BadPaddingException: Blocktype mismatch: 0
sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:332)
sun.security.rsa.RSAPadding.unpad(RSAPadding.java:272)
com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:354)
com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:380)
javax.crypto.Cipher.doFinal(Cipher.java:2121)
What must I do to resolve this issue?
public byte[] encrypt(byte[] plaintext){
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plaintext);
}
public byte[] decrypt(byte[] ciphertext){
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(ciphertext);
}