MD5/AES encryption and decryption of request parameters using CryptoJS

ApiPost has CryptoJS built in (github.com/brix/crypto… , can facilitate the request parameters for a variety of encryption and decryption.

MD5 encryption

Cryptojs.md5 (' string to be encrypted ').toString()Copy the code

SHA256 encryption

Cryptojs.sha256 (' string to be encrypted ').toString()Copy the code

Base64 encryption

CryptoJS. Enc. Base64. Stringify (CryptoJS. Enc. Utf8. Parse (' to be encrypted string))Copy the code

Base64 decryption

CryptoJS. Enc. Base64. Parse (" to decrypt the string "). The toString (CryptoJS. Enc. Utf8)Copy the code

AES Simple encryption

Encrypt (' String to be encrypted ', 'secret key ').toString()Copy the code

AES simple decryption

Cryptojs.aes.decrypt (' string to decrypt ', 'secret key ').tostring (cryptojs.enc.utf8)Copy the code

Custom AES encryption and decryption function

The above examples are two simple AES encryption and decryption schemes. In most cases, we need to customize more parameters of AES encryption and decryption, such as encryption mode and padding.

Const key = cryptojs.enc.utf8.parse (" key "); // Hexadecimal number as key const iv = cryptojs.enc.utf8.parse (' offset '); 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(); } // Encrypt method function Encrypt(word) {let SRCS = cryptojs.enc.utf8.parse (word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } // Mode is the encryption mode, padding is the padding.Copy the code

Sample request