I'm trying to encrypt and decrypt String i got from my user.
I've done some searching on google and i found this piece of code.
try{
String text="";
String key="Bar12345Bar12345";
System.out.print("Input Text>>");
text=sc.nextLine();
Key aesKey = new SecretKeySpec(key.getBytes(),"AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
String encryptedValue = Base64.getEncoder().encodeToString(encrypted);
System.out.println(encryptedValue);
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.out.println(decrypted);
}catch(Exception e){
e.printStackTrace();
}
But the String given is gibberish (some sort of symbol) which returned an error.
Is there any other method to encrypt and decrypt the text? Or maybe some changes needed for the code to works?
Thanks in advance!
Here is the referenced link in case you need it
http://www.software-architect.net/articles/using-strong-encryption-in-java/introduction.html
Edit: Thanks to @sgrillon for pointing out the link to the solution.