0%

CryptoJS

CryptoJS

It is a powerful encryption library in JavaScript

Install

1
2
yarn add crypto-js
npm install crypto-js

Usage

1
2
3
4
5
6
7
8
// Return a object (WordArray)
CryptoJS.MD5("smallstars");

// Return string (default Hex)
CryptoJS.MD5("smallstars").toString(CryptoJS.enc.Hex);

// Return the object before conversion
CryptoJS.enc.Hex.parse(CryptoJS.MD5("smallstars").toString(CryptoJS.enc.Hex));

Simple Sample

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import CryptoJS from "crypto-js";

// The hexadecimal characters of sixteen length as the key
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF");
// The hexadecimal characters of sixteen length as the offset of key
const iv = CryptoJS.enc.Utf8.parse("ABCDEF1234123412");

// encrypt
function encrypt(message) {
let srcs = CryptoJS.enc.Utf8.parse(message);
let encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return encrypted.ciphertext.toString().toUpperCase();
}

// decrypt
function decrypt(message) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(message);
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();
}

References

-------------The end of this article, thanks for reading-------------