我正在使用react作为前端和spring mvc作为后端的Web应用程序.我需要将一些用户信息存储在浏览器的本地存储中.我不想将该信息作为纯文本存储在本地存储中.所以我想在服务器端进行AES加密并将这些数据推回到JS端.为此,我需要客户端解密框架.我发现crypto-js对所有这些都非常有用.我无法理解客户端缺少解密和解码的地方.
我首先解释我的Spring Side Encryption Code,这绝对没问题:
public class EncryptDecrypt {
private static final String SECRET_KEY_1 = "ssdkF$HUy2A#D%kd";
private static final String SECRET_KEY_2 = "weJiSEvR5yAC5ftB";
private IvParameterSpec ivParameterSpec;
private SecretKeySpec secretKeySpec;
private Cipher cipher;
public EncryptDecrypt() throws UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException {
ivParameterSpec = new IvParameterSpec(SECRET_KEY_1.getBytes("UTF-8"));
secretKeySpec = new SecretKeySpec(SECRET_KEY_2.getBytes("UTF-8"), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
}
public String encrypt(String toBeEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(toBeEncrypt.getBytes());
return Base64.encodeBase64String(encrypted);
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端,我无法用简单的事情解码和解密代码.这是我的客户端代码:
var CryptoJS = …Run Code Online (Sandbox Code Playgroud)