I have some problems with my android app. I am trying to use an RSA encryption / decryption application. That's my problem:
I can clearly encrypt short sentences, but when I try to decrypt this message to the source text, I give an error ("too much data for the RSA block"). And also, if I want to encrypt long sentences, I have the same error. I had a search for this problem, and I found some solution on these sites:
Site 1
Site 2
Site 3
But I don’t understand anything, these decisions are so complicated. How can I fix this problem, can someone give me a simpler solution? Thanks.
EDIT: These are the code blocks that I use for this project.
public String RSAEncrypt(String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException { publicKey = getPublicKey(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherData = cipher.doFinal(plain.getBytes()); return Base64.encodeToString(cipherData, Base64.DEFAULT); } public String RSADecrypt(byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException { privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] cipherData = cipher.doFinal(encryptedBytes); return Base64.encodeToString(cipherData, Base64.DEFAULT); }
source share