Answer the question
In order to leave comments, you need to log in
How to encrypt (decrypt) data correctly using the AES algorithm through the CryptoJs library?
Hello, please tell me how to encrypt and decrypt data using the CryptoJs library?
Now I've come up with this:
const key = CryptoJS.lib.WordArray.random(256).toString()
// Шифрование сообщения
function encryptMessage(message) {
var iv = CryptoJS.lib.WordArray.random(256)
var encrypted = CryptoJS.AES.encrypt(message, key, { iv: iv });
return encrypted.toString()
}
// Дешифрование сообщения
function decryptMessage(encrypted) {
const decrypted = CryptoJS.AES.decrypt(encrypted, key)
return decrypted.toString(CryptoJS.enc.Utf8)
}
const key = CryptoJS.lib.WordArray.random(256).toString()
function encryptMessage(message) {
var salt = CryptoJS.lib.WordArray.random(32)
var iv = CryptoJS.lib.WordArray.random(256)
var newKey = CryptoJS.PBKDF2(key, salt, { keySize: 256, iterations: 10 })
var encrypted = CryptoJS.AES.encrypt(message, newKey, { iv: iv });
return encrypted.toString()
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question