How to use the enc function from crypto-js
Find comprehensive JavaScript crypto-js.enc code examples handpicked from public code repositorys.
crypto-js.enc is a module in the crypto-js library that provides encoding and decoding functions for various data formats, including UTF-8, Base64, and Hex.
23 24 25 26 27 28 29 30 31 32
iv: function() { return timetrans(new Date(),'day').replace(/-/g,'') }, encrypt: function(b, c, a) { if (c) { return (CryptoJS.TripleDES.encrypt(b, CryptoJS.enc.Utf8.parse(c), { iv: CryptoJS.enc.Utf8.parse(a || DES3.iv()), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })).toString()
+ 7 other calls in file
132 133 134 135 136 137 138 139 140
var encodeData = CryptoJS.enc.Base64.stringify(srcs); return encodeData } function base64Decode() { var srcs = CryptoJS.enc.Base64.parse(encodeData); var decodeData = srcs.toString(CryptoJS.enc.Utf8); return decodeData }
+ 23 other calls in file
How does crypto-js.enc work?
crypto-js.enc is a module in the crypto-js library that provides functions for encoding and decoding data in various formats. When you want to encode or decode data using one of the supported formats, you first create an instance of the corresponding crypto-js encoder or decoder function from the crypto-js.enc module. For example, to encode a string as Base64, you would use crypto-js.enc.Base64.stringify(). This function takes a WordArray object as input and returns a Base64-encoded string. To decode a Base64-encoded string back to its original form, you would use crypto-js.enc.Base64.parse(). This function takes a Base64-encoded string as input and returns a WordArray object containing the decoded data. In addition to Base64, crypto-js.enc also provides encoding and decoding functions for other formats, including UTF-8, Hex, Latin1, and UTF-16. crypto-js.enc is commonly used in combination with other modules in the crypto-js library to implement various cryptographic functions, such as encryption, decryption, hashing, and authentication. Overall, crypto-js.enc provides a flexible and easy-to-use set of encoding and decoding functions for working with data in a variety of formats.
30 31 32 33 34 35 36 37 38 39
const mnemonic = generateSeed(randomBytesFunc) return await generateWalletFromSeed(mnemonic) } function createTerraAddress(publicKey) { const message = CryptoJS.enc.Hex.parse(publicKey.toString(`hex`)) const hash = ripemd160(sha256(message)).toString() const address = Buffer.from(hash, `hex`) return bech32ify(address, `terra`) }
GitHub: nknorg/nkn-client-js
0 1 2 3 4 5 6 7 8 9
'use strict'; const CryptoJS = require('crypto-js'); function cryptoHexStringParse(hexString) { return CryptoJS.enc.Hex.parse(hexString) } function sha256(str) { return CryptoJS.SHA256(str).toString();
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const CryptoJS = require("crypto-js"); // Encode a string as Base64 const plaintext = "Hello, world!"; const ciphertext = CryptoJS.enc.Base64.stringify( CryptoJS.enc.Utf8.parse(plaintext) ); console.log(ciphertext); // Output: "SGVsbG8sIHdvcmxkIQ==" // Decode a Base64-encoded string const decodedText = CryptoJS.enc.Utf8.stringify( CryptoJS.enc.Base64.parse(ciphertext) ); console.log(decodedText); // Output: "Hello, world!"
In this example, we first import the crypto-js library and create a CryptoJS object to access its modules. We then use CryptoJS.enc.Utf8.parse() to convert the plaintext string to a WordArray object, and then use CryptoJS.enc.Base64.stringify() to encode the WordArray as a Base64-encoded string. We log the resulting ciphertext string to the console, which should output "SGVsbG8sIHdvcmxkIQ==". To decode the Base64-encoded string back to its original form, we use CryptoJS.enc.Base64.parse() to convert the ciphertext string to a WordArray object, and then use CryptoJS.enc.Utf8.stringify() to decode the WordArray back to a string. We log the resulting decodedText string to the console, which should output "Hello, world!". Overall, crypto-js.enc provides a simple and flexible way to encode and decode data in various formats, including Base64, UTF-8, Hex, and more.
126 127 128 129 130 131 132 133 134 135
function hmac(key, msg, algo){ const key_hex = ba2hex(key) const msg_hex = ba2hex(msg) const key_words = CryptoJS.enc.Hex.parse(key_hex) const msg_words = CryptoJS.enc.Hex.parse(msg_hex) let hash; if (algo === 'md5') { hash = CryptoJS.HmacMD5(msg_words, key_words) return wa2ba(hash.words)
+ 19 other calls in file
GitHub: ICObench/data-api
9 10 11 12 13 14 15 16 17 18
get(action, callback, data = {}) { let dataJSON = JSON.stringify(data); let sign = CryptoJS.HmacSHA384(dataJSON, this.privateKey); sign = CryptoJS.enc.Base64.stringify(sign); request.post(this.apiUrl + action, { headers: { 'Accept': 'application/json',
+ 3 other calls in file
GitHub: webfansplz/null-cli
6 7 8 9 10 11 12 13 14 15
sha256: str => require('crypto-js/sha256')(str).toString(), sha384: str => require('crypto-js/sha384')(str).toString(), sha512: str => require('crypto-js/sha512')(str).toString(), md5: str => require('crypto-js/md5')(str).toString(), base64: str => CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(str)), hex: str => CryptoJS.enc.Hex.stringify(CryptoJS.enc.Utf8.parse(str)), aes: (str, aeskey, aesiv) => { const key = CryptoJS.enc.Utf8.parse(aeskey); const iv = CryptoJS.enc.Utf8.parse(aesiv); return CryptoJS.AES.encrypt(str, key, { iv }).ciphertext.toString();
+ 31 other calls in file
184 185 186 187 188 189 190 191 192 193
function sign(input, key, method, type) { var base64str; if(type === "hmac") { hmac = cjshmac[method](input, key); base64str = crypto.enc.Base64.stringify(hmac) } else if(type == "sign") { base64str = crypto.createSign(method).update(input).sign(key, 'base64'); }
16 17 18 19 20 21 22 23 24 25
function encrypt(data) { let algorithm = 'des-128-cbc' let passwd = '{1dYgqE)h9,R)hKqEcv4]k[h' let iv = '01234567' str = cryptojs.TripleDES.encrypt("" + data, cryptojs.enc.Utf8.parse(passwd), { iv: cryptojs.enc.Utf8.parse(iv), mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 }) return str.toString()
32 33 34 35 36 37 38 39 40 41
} function decryptGeneral(f, dataHex, keyHex, ivHex) { const data = CryptoJS.enc.Hex.parse(dataHex); const key = CryptoJS.enc.Hex.parse(keyHex); const iv = CryptoJS.enc.Hex.parse(ivHex); const encrypted = {}; encrypted.key = key; encrypted.iv = iv; encrypted.ciphertext = data;
+ 27 other calls in file
11 12 13 14 15 16 17 18 19
var encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); // console.log('直接 toString' + encrypted.toString()) // 对加密结果直接 toString 就是 base64 // var enstr = CryptoJS.enc.Base64.parse(encrypted.toString()); // return enstr.toString().toUpperCase(); return encrypted.ciphertext.toString().toUpperCase() }
+ 3 other calls in file
36 37 38 39 40 41 42 43 44 45
iv: iv }); return CryptoJS.enc.Utf8.stringify(decrypted).toString(); }, ECBDecrypt(data) { var key = CryptoJS.enc.Utf8.parse(aseKey); var ivs = CryptoJS.enc.Utf8.parse(iv); var decrypt = CryptoJS.AES.decrypt(data, key, { iv: ivs,
+ 41 other calls in file
GitHub: zhoujie16/zxm_server
49 50 51 52 53 54 55 56 57 58
return CryptoJS.SHA224(message).toString(); }; // 加密解密 des export const encryptDesEcb = message => { const key = DES_KEY; const keyHex = CryptoJS.enc.Utf8.parse(key); const encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7, });
+ 5 other calls in file
GitHub: smdpro/IPG-node
21 22 23 24 25 26 27 28 29 30
// padding: CryptoJS.pad.Pkcs7, // }).toString(); // }; const hmacSHA256 = (data, key) => CryptoJS.enc.Base64.stringify( CryptoJS.HmacSHA256(data, CryptoJS.enc.Base64.parse(key)) ); module.exports = {
+ 3 other calls in file
15 16 17 18 19 20 21 22 23 24
}, parse: function(encryptedHexString) { const KatuMark = [0x9527,0x4396] const SaltMark = [0x53616c74, 0x65645f5f] const ciphertext = CryptoJS.enc.Hex.parse(encryptedHexString) const ciphertextWords = ciphertext.words if(ciphertextWords[0] !== KatuMark[0] || ciphertextWords[1] !== KatuMark[1]){ throw Error("ciphertext format error") }
+ 8 other calls in file
GitHub: mnismt/MyHash
117 118 119 120 121 122 123 124 125 126
rabbitlegacy() { return CryptoJS.RabbitLegacy.encrypt(this.text, this.serect).toString(); }, base64() { let words = CryptoJS.enc.Utf8.parse(this.text); return CryptoJS.enc.Base64.stringify(words); } } exports.encrypt = encrypt;
+ 35 other calls in file
70 71 72 73 74 75 76 77 78 79
outputLength: 256, }); } // Result return { priv: CryptoJS.enc.Hex.stringify(data), }; } /**
+ 6 other calls in file
115 116 117 118 119 120 121 122 123 124
assert(pbkdf2_iterations, "pbkdf2_iterations missing"); var salt = CryptoJS.lib.WordArray.random(16); var stretched_password = stretchPassword(password, salt, pbkdf2_iterations); var iv = salt; var payload = CryptoJS.enc.Utf8.parse(data); var encrypted = CryptoJS.AES.encrypt(payload, stretched_password, { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Iso10126, iv: iv
+ 9 other calls in file
GitHub: MooNight16/qinglong-js
532 533 534 535 536 537 538 539 540 541 542
//////////////////////////////////////////////////////////////////// //AES/DES加解密,CryptoJS function EncryptCrypto(method,mode,padding,message,key,iv) { return CryptoJS[method].encrypt( CryptoJS.enc.Utf8.parse(message), CryptoJS.enc.Utf8.parse(key), {mode:CryptoJS.mode[mode], padding:CryptoJS.pad[padding], iv:CryptoJS.enc.Utf8.parse(iv)} ).ciphertext.toString(CryptoJS.enc.Base64); }
+ 23 other calls in file
crypto-js.AES is the most popular function in crypto-js (907 examples)