My encryption and decryption is running good without using a file. But I have to store the encrypted data in a file and transfer it to a peer system, where I have to read the file and decrypt it.
The problem I face is, a single unicode on encryption gives me a set of unicodes(thats my cipher) like this.
☺ ╪iß
When I store these cipher in a file, the decryption will be done for each of the unicodes separately. But the decrption should be done for the respective set of unicodes to get back my plaintext. How should I store them in a file? How could I select the corresponding unicodes on decryption to get back my plaintext? I’m using the keys of size 1024 bits.
Encryption algorithms don’t work on unicode characters. They work on binary data: byte arrays.
If you need to encrypt unicode text (a String, for example), then choose a non lossy byte encoding for the String (like UTF-8, for example), transform the String to a byte array (
String.getBytes("UTF-8")), encrypt the byte array, and write the resulting byte array to the file.When decrypting, read the file as a byte array, decrypt this byte array (which gives another byte array), and recreate a String from this byte array using the same character encoding:
new String(plainTextBytes, "UTF-8").