I am writing a simple java programs to encrypt/decrypt using DES and RSA algorithms using Java. In Java, in order to encrypt and decrypt using the function doFinal
, I have to conver the message (plain text) into bytes. I used the function .getBytes
to do so. Here is part of the code:
Cipher desCipher
// Create the cipher
desCipher = Cipher.getInstance("DES/ECB/NoPadding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] textEncrypted = desCipher.doFinal(msgBytes);
Then, return it as:
return new String(base64Cipher);
In this case, I get the cipher text looks in strange characters suc as:
Encrypted Message: °;zRfwجvë “ëثw‰ڑل7دM#¼7وm‡î¤
In some internet tutorials, they used Base64 encoding before returning the cipher such as this reference: http://techie-experience.blogspot.com/2012/10/encryption-and-decryption-using-aes.html
When I used Base64 as follows:
byte[] textEncrypted = desCipher.doFinal(msgBytes);
// converts to base64 for easier display.
byte[] base64Cipher = Base64.encodeBase64(textEncrypted);
return new String(base64Cipher);
The cipher text appears in alpha and numerical characters as:
Encrypted Message: Xz93ODw3gEKm3hziPr0UJ+ed55nWLGpTR9uHjZjLwtg=
Which method is correct? Does RSA cipher supposed to be in alpha-numeric characters only? If not, why that referee suggests using it ?