<分区>
我正在尝试加密和解密我从用户那里得到的字符串。
我在谷歌上做了一些搜索,我找到了这段代码。
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();
}
但是给出的字符串是乱码(某种符号),返回了一个错误。
有没有其他方法可以加密和解密文本?或者可能需要对代码进行一些更改才能正常工作?
提前致谢!
这里是引用链接,以备不时之需
http://www.software-architect.net/articles/using-strong-encryption-in-java/introduction.html
编辑: 感谢@sgrillon 指出解决方案的链接。