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:
- crypto-js Error: Malformed UTF-8 data [How to Solve]
- [Solved] javax.crypto.BadPaddingException: Decryption error
- How to Solve Image Upload Error: Uncaught (in promise) DOMException: Failed to execute ‘put‘ on ‘IDBObjectStore‘
- [Solved] Maven Startup Error: Malformed \uxxxx encoding
- [Solved] IDEA globally replace yml Error: you have entered malformed replacement
- Failed to auto-configure a DataSource: ‘spring.datasource.url‘ is not specified and no embedded data
- JAVA: How to Read JSON Format Data (Web Game Development)
- Uncaught SyntaxError: Unexpected token o in JSON at position 1 [How to Solve]
- Uncaught TypeError: Cannot read property ‘toFixed‘ of null
- How to Solve the Primary Key of mybatisPlus Inserted Data is too Large Issue
- Java.sql.sqlexception: unable to read more data from socket
- When the database table field is set to self incrementing, use the entity class to insert or update the data to solve the error (Hibernate Framework)
- Explicit and implicit conversion of Java data type
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- [Solved] JPA query data error: Page 1 of 0 containing UNKNOWN instances
- [Solved] write javaBean error, fastjson version 1.2.76, class org.apache.flink.table.data.binary
- Conversion of two data types in Java
- Jmeter Error: ERROR o.a.j.JMeter: Uncaught exception in thread Thread[AWT-EventQueue-0,6,main]
- Defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configuration.
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu