You are missing some steps in the code, which makes verification impossible. However, there are a few tips to suggest a problem. Your decryptData method takes a String argument and then calls String.getBytes() to get the data, which is then decrypted. However, the result of encryption is a sequence of bytes that is not the encoding of any valid string. Perhaps you wanted base64 to decode the input instead of calling getBytes() . In general, to perform decryption and decoding, you must undo the actions that you performed during encryption and encoding. So, if the plaintext is a byte [], then the following steps:
byte [] β Encrypt β byte [] β Base64 encode β String.
then in the direction of decryption, you start with the Base64 line, you should:
String β Base64 decode β byte [] β decrypt β byte []
Also, another problem that is bad practice and the source of many portability errors is the use of default values. You use the default settings in two places, and they are both unpleasant. First, you use the default no-args method String.getBytes() and presumably map this to a single-arg String (byte []) . This uses the default character set for the platform, but it may vary across platforms. Therefore, always specify a character set. For most applications, UTF-8 is the perfect choice. Secondly, you call Cipher.getInstance('RSA') without specifying a Cipher.getInstance('RSA') . Oracle Java and Android Java will provide you with various add-ons and therefore your code will not be portable between platforms. Always indicate the complete string. The choice here is a bit trickier if you need portability for older Java implementations. Canceling OAEP should be your first choice, therefore Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); probably the right choice. See more details.
How to encrypt longer texts, see answer from Henry .
source share