gpt4 book ai didi

java - 在 Java 中加密和解密文本

转载 作者:行者123 更新时间:2023-11-30 10:18:14 26 4
gpt4 key购买 nike

<分区>


我正在尝试加密和解密我从用户那里得到的字符串。
我在谷歌上做了一些搜索,我找到了这段代码。

   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 指出解决方案的链接。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com