Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const encrypt = (m, pass) => {
const msg = JSON.stringify(m);
const salt = CryptoJS.lib.WordArray.random(128 / 8);
const key = CryptoJS.PBKDF2(pass, salt, {
keySize: keySize / 32,
iterations
});
const iv = CryptoJS.lib.WordArray.random(128 / 8);
const encrypted = CryptoJS.AES.encrypt(msg, key, {
iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
const transitmessage = salt.toString() + iv.toString() + encrypted.toString();
return transitmessage;
};
_aesDecryptedUtf8: function _aesDecryptedUtf8(ciphertext_base64, key_hex, iv_hex) {
ciphertext_base64 = ciphertext_base64.replace(/\n/, "");
var ciphertext_hex = this._checkBlockSize(this._b64ToHex(ciphertext_base64), 16); // the blockSize is 16
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext_hex)
});
var decrypted = CryptoJS.AES.decrypt(
cipherParams,
CryptoJS.enc.Hex.parse(key_hex),
{
iv: CryptoJS.enc.Hex.parse(iv_hex),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}
);
return decrypted.toString(CryptoJS.enc.Utf8);
},
function decrypt(word) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
function encrypt (text, originKey) {
originKey = originKey.slice(0, 16);
var key = CryptoJS.enc.Utf8.parse(originKey),
iv = CryptoJS.enc.Utf8.parse(originKey),
msg = JSON.stringify(text);
var ciphertext = CryptoJS.AES.encrypt(msg, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return ciphertext.toString()
}
encrypt: (message) => {//加密
return CryptoJS.AES.encrypt(message, AESkey, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
},
decrypt: (encrypt) => {//解密
_aesEncryptedBase64: function _aesEncryptedBase64(payload, key, iv) {
var encrypted = CryptoJS.AES.encrypt(
payload,
CryptoJS.enc.Hex.parse(key),
{
iv: CryptoJS.enc.Hex.parse(iv),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}
);
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
},
encrypt(data) {
const CryptoJS = require('crypto-js'); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");
const iv = CryptoJS.enc.Utf8.parse('ABCDEF1234123412');
let srcs = CryptoJS.enc.Utf8.parse(data);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
},
imgUpload(e, cb) {
function getUserEncryptionKeyR5(
processedUserPassword,
userKeySalt,
encryptionKey
) {
const key = CryptoJS.SHA256(
processedUserPassword.clone().concat(userKeySalt)
);
const options = {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.NoPadding,
iv: CryptoJS.lib.WordArray.create(null, 16)
};
return CryptoJS.AES.encrypt(encryptionKey, key, options).ciphertext;
}
static decrypt(val) {
let bytes = CryptoJS.AES.decrypt(val, CryptoJS.enc.Utf8.parse(criteria.CRYPTOJS_KEY), {iv: CryptoJS.enc.Utf8.parse(criteria.CRYPTOJS_IV),mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7});
return bytes.toString(CryptoJS.enc.Utf8);
}
CryptoAdapter.aesEncrypt = function aesEncrypt(plaintext, key_hex, iv_hex) {
return CryptoJS.AES.encrypt(
plaintext,
CryptoJS.enc.Hex.parse(key_hex),
{
iv: CryptoJS.enc.Hex.parse(iv_hex),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
}
);
};