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()
fork icon35
star icon57
watch icon1

+ 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
}
fork icon34
star icon57
watch icon0

+ 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`)
}
fork icon39
star icon0
watch icon1

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();
fork icon22
star icon54
watch icon10

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)
fork icon21
star icon30
watch icon9

+ 19 other calls in file

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',
fork icon20
star icon27
watch icon4

+ 3 other calls in file

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();
fork icon15
star icon86
watch icon2

+ 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');
  }
fork icon147
star icon6
watch icon2

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()
fork icon10
star icon15
watch icon2

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;
fork icon8
star icon4
watch icon5

+ 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()
}
fork icon5
star icon14
watch icon2

+ 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,
fork icon2
star icon8
watch icon2

+ 41 other calls in file

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,
  });
fork icon0
star icon3
watch icon2

+ 5 other calls in file

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 = {
fork icon2
star icon2
watch icon1

+ 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")
    }
fork icon0
star icon2
watch icon1

+ 8 other calls in file

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;
fork icon0
star icon1
watch icon1

+ 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),
    };
}

/**
fork icon61
star icon0
watch icon2

+ 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
fork icon16
star icon17
watch icon6

+ 9 other calls in file

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);
}
fork icon4
star icon7
watch icon1

+ 23 other calls in file