How to use the DES function from crypto-js

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

crypto-js.DES is a method for encrypting and decrypting data using the DES (Data Encryption Standard) algorithm in JavaScript.

6
7
8
9
10
11
12
13
14
15
//required
//<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
fork icon5
star icon14
watch icon2

57
58
59
60
61
62
63
64
65
}

export function encryptICV(macEncKey: Uint8Array, mac: Uint8Array) : Uint8Array {
  let dataWArray = CryptoJS.lib.WordArray.create(mac);
  let sessionEncKeyWArray = CryptoJS.lib.WordArray.create(resizeKey8(macEncKey));
  let encData = CryptoJS.DES.encrypt(dataWArray, sessionEncKeyWArray, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});

  return CryptoUtils.wordArrayToByteArray(encData.ciphertext);
}
fork icon0
star icon5
watch icon2

+ 3 other calls in file

How does crypto-js.DES work?

crypto-js.DES is a method for encrypting and decrypting data using the DES (Data Encryption Standard) algorithm in JavaScript. The DES algorithm is a symmetric key block cipher that works by dividing the plaintext data into blocks of 64 bits and then applying a series of 16 rounds of substitution and permutation to each block to create the encrypted ciphertext. The same key is used for both encryption and decryption, making it a symmetric encryption algorithm. In crypto-js.DES, the encryption and decryption functions take two arguments: the plaintext data to be encrypted or decrypted, and a key used for encryption and decryption. The key can be either a string or a WordArray object, which is a data type used by the crypto-js library to represent arbitrary-length binary data. For example, the following code encrypts a message using the crypto-js.DES method: javascript Copy code {{{{{{{ const CryptoJS = require('crypto-js'); const message = 'Hello, world!'; const key = 'MySecretKey'; const encryptedMessage = CryptoJS.DES.encrypt(message, key).toString(); console.log(encryptedMessage); In this code, we use the crypto-js.DES method to encrypt the message 'Hello, world!' using the key 'MySecretKey'. The resulting encrypted message is stored in the encryptedMessage variable, and is then logged to the console using console.log(). Note that CryptoJS.DES.encrypt() returns a CipherParams object, which is a data type used by the crypto-js library to represent encrypted data. To convert the CipherParams object to a string, we use the toString() method. Similarly, to decrypt an encrypted message, we can use the CryptoJS.DES.decrypt() method, like this: scss Copy code {{{{{{{ class="!whitespace-pre hljs language-scss">const decryptedMessage = CryptoJS.DES.decrypt(encryptedMessage, key).toString(CryptoJS.enc.Utf8); console.log(decryptedMessage); In this code, we use the CryptoJS.DES.decrypt() method to decrypt the encryptedMessage using the key. The resulting decrypted message is stored in the decryptedMessage variable, and is then logged to the console using console.log(). We use the toString() method with the CryptoJS.enc.Utf8 encoding to convert the decrypted data from a WordArray object to a UTF-8 encoded string. Overall, crypto-js.DES provides a way to perform symmetric key encryption and decryption using the DES algorithm in JavaScript, making it easy to secure sensitive data in web applications.

60
61
62
63
64
65
66
67
68
69
};
// 解密 des
export const decryptDesEcb = ciphertext => {
  const key = DES_KEY;
  const keyHex = CryptoJS.enc.Utf8.parse(key);
  const decrypted = CryptoJS.DES.decrypt(
    {
      ciphertext: CryptoJS.enc.Hex.parse(ciphertext),
    },
    keyHex,
fork icon0
star icon3
watch icon2

+ 3 other calls in file

863
864
865
866
867
868
869
870
871
872
873


function EncFunc(message) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var ivHex = CryptoJS.enc.Utf8.parse(key);
    var word = CryptoJS.enc.Utf8.parse(message);
    encrypted = CryptoJS.DES.encrypt(
        word, 
        keyHex, 
        {iv:ivHex, mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7}
    );
fork icon1
star icon1
watch icon1

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const CryptoJS = require("crypto-js");

const message = "Hello, world!";
const key = "MySecretKey";

// Encrypt message
const encryptedMessage = CryptoJS.DES.encrypt(message, key).toString();

console.log("Encrypted message:", encryptedMessage);

// Decrypt message
const decryptedMessage = CryptoJS.DES.decrypt(encryptedMessage, key).toString(
  CryptoJS.enc.Utf8
);

console.log("Decrypted message:", decryptedMessage);

In this example, we use the crypto-js.DES method to encrypt the message 'Hello, world!' using the key 'MySecretKey'. The resulting encrypted message is stored in the encryptedMessage variable, and is then logged to the console using console.log(). We then use the CryptoJS.DES.decrypt() method to decrypt the encryptedMessage using the key. The resulting decrypted message is stored in the decryptedMessage variable, and is then logged to the console using console.log(). The output of this code will be: yaml Copy code

78
79
80
81
82
83
84
85
86
87
    let iv = crypto.enc.Utf8.parse(this.params.iv)
    return crypto.AES.encrypt(str, key, { iv: iv }).toString()
  } else if (this.type == 'DES'){
    let key = crypto.enc.Utf8.parse(this.params.key)
    let iv = crypto.enc.Utf8.parse(this.params.iv)
    return crypto.DES.encrypt(str, key, { iv: iv }).toString()
  }
}

/**
fork icon0
star icon0
watch icon1