brix / crypto-js

JavaScript library of crypto standards.
Other
15.88k stars 2.39k forks source link

AES with hex input #411

Open masterchen opened 2 years ago

masterchen commented 2 years ago

After failing on several hours on “coding” with Hex input , i found there is a solution provided by a good guy, this works for me . so i move the code here for some one who need it :

encrypt(valueStringHex, keyStringHex) { const CryptoJS = require('crypto-js'); const value = CryptoJS.enc.Hex.parse(valueStringHex); const key = CryptoJS.enc.Hex.parse(keyStringHex); const ivvar = CryptoJS.enc.Hex.parse('00000000000000000000000000000000'); const encryptedStringHex = CryptoJS.AES.encrypt(value, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}).ciphertext.toString(); return encryptedStringHex; }

// encrypt('5ff58680541c5a5903f4833dfaa4281f', '41435231323535552d4a312041757458') // returns 79d8a373d47bb25df3c1956b04106b15 decrypt(valueStringHex, keyStringHex) { const CryptoJS = require('crypto-js'); const value = CryptoJS.enc.Hex.parse(valueStringHex); const key = CryptoJS.enc.Hex.parse(keyStringHex); const ivvar = CryptoJS.enc.Hex.parse('00000000000000000000000000000000'); const decryptedStringHex = CryptoJS.AES.decrypt({ciphertext: value}, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}); return decryptedStringHex.toString(); }

// decrypt('79d8a373d47bb25df3c1956b04106b15', '41435231323535552d4a312041757458') // returns 5ff58680541c5a5903f4833dfaa4281f

this is the org link : https://stackoverflow.com/questions/61837727/using-cryptojs-with-hex-string