Created
December 13, 2019 21:34
-
-
Save tiagolr/37d3f57e368b7b60185a9be1a7211825 to your computer and use it in GitHub Desktop.
encryptAES2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AESDecrypt = function (ciphertext, key) { | |
var keybuf = bsv.crypto.Hash.sha256(key.toBuffer()) | |
var key = CryptoJS.enc.Hex.parse(keybuf.slice(0, 8).toString('hex')); | |
var iv = CryptoJS.enc.Hex.parse(keybuf.slice(8, 16).toString('hex')); | |
var decrypt = CryptoJS.AES.decrypt(ciphertext, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); | |
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); | |
return decryptedStr.toString(); | |
} | |
const AESEncrypt = function (plaintext, key) { | |
var keybuf = bsv.crypto.Hash.sha256(key.toBuffer()) | |
var key = CryptoJS.enc.Hex.parse(keybuf.slice(0, 8).toString('hex')); | |
var iv = CryptoJS.enc.Hex.parse(keybuf.slice(8, 16).toString('hex')); | |
var srcs = CryptoJS.enc.Utf8.parse(plaintext); | |
var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); | |
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment