java encrypt js decrypt, an error is reported: 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);
}
The reason is that the string has a newline character, Base64 generates a newline, change to NO_WRAP
Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)
Recommended Read: https://stackoverflow.com/questions/21852889/remove-r-and-n-from-aes-encrypted-string
Read More:
- [Solved] javax.crypto.BadPaddingException: Decryption error
- Java implementation of inputsteam to Base64
- RSA Decryption Error: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
- Explicit and implicit conversion of Java data type
- jasypt springboot Error: Error creating bean with name ‘enableEncryptablePropertySourcesPostProcessor’ defined in class path resource
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- How to Open Files by Dragging and Dropping in C#
- JAVA: How to Use Minio to upload pictures
- JAVA: How to Read JSON Format Data (Web Game Development)
- Upload file error analysis standardmultiparthttpservletrequest
- H2 memory database Oracle mode page error: rg.springframework.dao.InvalidDataAccessResourceUsageException: could not prepar
- Jjwt error: ERROR 9856 — [nio-8083-exec-2] o.a.c.c. [. [. [. [/]. [dispatch server]
- ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]
- [How to Fix]Spring 3.0 could not find acceptable representation
- Comparison method violates its general contract [How to Solve]
- [PROJECT] itdage java to get the weather and send text messages
- [Solved] Circular view path [index]: would dispatch back to the current handler URL [] again. Che
- [Solved] Java Run Error: For input string: “XXX”
- Xdoc generates API documents based on Java annotations