How to use the mode function from crypto-js

Find comprehensive JavaScript crypto-js.mode code examples handpicked from public code repositorys.

crypto-js.mode is a property in the CryptoJS library for JavaScript that specifies the block cipher mode used for encryption or decryption.

25
26
27
28
29
30
31
32
33
34
},
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()
    }
    return ""
fork icon35
star icon57
watch icon1

+ 3 other calls in file

67
68
69
70
71
72
73
74
75
76
}

// v1: OFB, nopad, 1 iteration
try {
  decrypted = decryptAes(data, password, 1, {
    mode: CryptoJS.mode.OFB,
    padding: CryptoJS.pad.NoPadding});

  if (decrypted !== null && decrypted.length > 0) {
    return decrypted;
fork icon16
star icon17
watch icon6

+ 9 other calls in file

How does crypto-js.mode work?

The crypto-js.mode property in the CryptoJS library for JavaScript is used to specify the block cipher mode used for encryption or decryption. Block cipher modes are a way of applying a block cipher, like AES or DES, to data that is larger than the cipher's block size. They specify how the input data is divided into blocks, how those blocks are processed, and how they are combined to produce the output. The crypto-js.mode property is set on the CipherParams object that is returned by the CryptoJS.AES.encrypt and CryptoJS.AES.decrypt functions, and determines how the cipher is applied to the input data. The default mode is ECB (Electronic Codebook) mode, but other modes like CBC (Cipher Block Chaining) or CFB (Cipher Feedback) can be specified by setting the mode property on the CipherParams object. For example, you could use crypto-js.mode to specify CBC mode for AES encryption like this: javascript Copy code {{{{{{{ import { AES } from 'crypto-js'; const message = 'Hello, world!'; const key = 'mySecretKey'; const iv = 'myInitializationVector'; const ciphertext = AES.encrypt(message, key, { iv: iv, mode: AES.mode.CBC }); console.log(ciphertext.toString()); In this example, we first import the AES function from the CryptoJS library using the ES6 import statement. We then define a message string that we want to encrypt, a key string to use as the encryption key, and an iv string to use as the initialization vector. We call AES.encrypt with the message string, key, and an object that specifies the iv and mode properties. The mode property is set to AES.mode.CBC to use CBC mode for the encryption. We store the resulting CipherParams object as ciphertext, which includes the encrypted message and the initialization vector. Finally, we log the ciphertext to the console using console.log. In summary, crypto-js.mode is a property in the CryptoJS library for JavaScript that specifies the block cipher mode used for encryption or decryption. Block cipher modes are a way of applying a block cipher to data that is larger than the cipher's block size, and they determine how the input data is divided into blocks, how those blocks are processed, and how they are combined to produce the output. The crypto-js.mode property is set on the CipherParams object returned by the CryptoJS.AES.encrypt and CryptoJS.AES.decrypt functions, and can be used to specify different block cipher modes like CBC or CFB.

1
2
3
4
5
6
7
8
9
10

function I(X, l) {
    var C = X
        , q = CryptoJS.enc.Utf8.parse(C)
        , v = CryptoJS.TripleDES.decrypt(l, q, {
            'mode': CryptoJS.mode.ECB,
            'padding': CryptoJS.pad.Pkcs7
        });
    return v.toString(CryptoJS.enc.Utf8);
}
fork icon12
star icon55
watch icon2

17
18
19
20
21
22
23
24
25
26
    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

Ai Example

1
2
3
4
5
6
7
8
import { AES } from "crypto-js";

const message = "Hello, world!";
const key = "mySecretKey";
const iv = "myInitializationVector";

const ciphertext = AES.encrypt(message, key, { iv: iv, mode: AES.mode.CBC });
console.log(ciphertext.toString());

In this example, we first import the AES function from the CryptoJS library using the ES6 import statement. We then define a message string that we want to encrypt, a key string to use as the encryption key, and an iv string to use as the initialization vector. We call AES.encrypt with the message string, key, and an object that specifies the iv and mode properties. The mode property is set to AES.mode.CBC to use CBC mode for the encryption. We store the resulting CipherParams object as ciphertext, which includes the encrypted message and the initialization vector. Finally, we log the ciphertext to the console using console.log. When you run this example in a JavaScript environment like a web browser or Node.js, you should see a ciphertext string logged to the console, representing the encrypted message using AES encryption in CBC mode with the specified key and initialization vector.

7
8
9
10
11
12
13
14
15
16
//<script src="../js/rollups/tripledes.js"></script>
//<script src="../js/components/mode-ecb-min.js"></script>
var key = 'Pog4iu6OqIkKRpDT';
var keyHex = CryptoJS.enc.Utf8.parse(key);
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());
fork icon5
star icon14
watch icon2

139
140
141
142
143
144
145
146
147
148
aesEcbEncrypt(message, key = ';Z#^$;8+yhO!AhGo') {
    // utf8字符串—>WordArray对象,WordArray是一个保存32位整数的数组,相当于转成了二进制
    const keyHex = cryptoJS.enc.Utf8.parse(key);
    const messageHex = cryptoJS.enc.Utf8.parse(message);
    const encrypted = cryptoJS.AES.encrypt(messageHex, keyHex, {
        mode: cryptoJS.mode.ECB,
        padding: cryptoJS.pad.Pkcs7,
    });
    return encrypted.toString(); // base64结果
},
fork icon2
star icon2
watch icon2

15
16
17
18
19
20
21
22
23
};
// const signingData = (str, key) => {
//   let keyHex = CryptoJS.enc.Base64.parse(key);
//   return CryptoJS.TripleDES.encrypt(str, keyHex, {
//     iv: keyHex,
//     mode: CryptoJS.mode.ECB,
//     padding: CryptoJS.pad.Pkcs7,
//   }).toString();
// };
fork icon2
star icon2
watch icon1

878
879
880
881
882
883
884
885
886
887
    var ivHex = CryptoJS.enc.Utf8.parse(key);
    // direct decrypt ciphertext
    var decrypted = CryptoJS.DES.decrypt(
        {ciphertext: CryptoJS.enc.Base64.parse(message)}, 
        keyHex, 
        {iv:ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7}
    );
    return decrypted.toString(CryptoJS.enc.Utf8);
}

fork icon1
star icon1
watch icon1

+ 4 other calls in file

83
84
85
86
87
88
89
90
91
92
  iv = wrapCryptoBuf(iv);
  msg = wrapCryptoBuf(msg);
  
  let encrypted = CryptoJS.AES.encrypt(msg, prv, {
    iv: iv,
    mode: CryptoJS.mode.CTR,
    padding: CryptoJS.pad.ZeroPadding
  });
  return encrypted.ciphertext;  // return CryptoJS.lib.WordArray
}
fork icon0
star icon0
watch icon2

+ 15 other calls in file

1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
iv = CryptoJS.enc.Utf8.parse(iv);

// var data = '{"gt":"019924a82c70bb123aae90d483087f94","challenge":"f3f54acc6c2de81517344f8fc25c88bd","offline":false,"new_captcha":true,"product":"popup","width":"300px","https":true,"api_server":"apiv6.geetest.com","protocol":"https://","type":"fullpage","static_servers":["static.geetest.com/","dn-staticdown.qbox.me/"],"beeline":"/static/js/beeline.1.0.1.js","voice":"/static/js/voice.1.2.2.js","click":"/static/js/click.3.0.7.js","fullpage":"/static/js/fullpage.9.1.0.js","slide":"/static/js/slide.7.8.9.js","geetest":"/static/js/geetest.6.0.9.js","aspect_radio":{"slide":103,"click":128,"voice":128,"beeline":50},"cc":20,"ww":true,"i":"6148!!7436!!CSS1Compat!!1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!2!!3!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!1!!-1!!-1!!-1!!0!!0!!0!!0!!230!!849!!1707!!920!!zh-CN!!zh-CN,zh,en!!-1!!2.25!!24!!Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36!!1!!1!!1707!!960!!1707!!920!!1!!1!!1!!-1!!Win32!!1!!-8!!bc665dae3a1ad081f784803fc723309c!!0!!internal-pdf-viewer,internal-pdf-viewer,internal-pdf-viewer,internal-pdf-viewer,internal-pdf-viewer!!0!!-1!!0!!20!!Arial,ArialBlack,ArialNarrow,Calibri,Cambria,CambriaMath,ComicSansMS,Consolas,Courier,CourierNew,Georgia,Helvetica,Impact,LucidaConsole,LucidaSansUnicode,MicrosoftSansSerif,MSGothic,MSPGothic,MSSansSerif,MSSerif,PalatinoLinotype,SegoePrint,SegoeScript,SegoeUI,SegoeUILight,SegoeUISemibold,SegoeUISymbol,Tahoma,Times,TimesNewRoman,TrebuchetMS,Verdana,Wingdings!!1670253944185!!-1!!-1!!-1!!12!!-1!!-1!!-1!!5!!-1!!-1"}'
var r = CryptoJS.AES.encrypt(e, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC
});

var o = r['ciphertext']['words'];
var i = r['ciphertext']['sigBytes'];
fork icon0
star icon0
watch icon1

54
55
56
57
58
59
60
61
62
63
 */
function aes(pwd, options) {
    var key = pwd;
    // get options
    var opt = options || {};
    opt.mode = opt.mode || CryptoJS.mode.CBC;
    opt.padding = opt.padding || CryptoJS.pad.Pkcs7;
    
    // 
    if(opt.iv) {
fork icon0
star icon0
watch icon1

+ 3 other calls in file