java 加密js 解密,报错 Uncaught Error: Malformed UTF-8 data
public static String encrypt(String strToEncrypt, SecretKey secretKey) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encryptedString = new String(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes())));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return encryptedString;
}
js 端
decrypt(secret_key,code) {
return CryptoJS.AES.decrypt(code, this.parse(secret_key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
}
原因是字符串有换行符,Base64 生成出现换行,改为NO_WRAP
Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)