引入 cryptojs 包

1
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>

DES 加密和解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//DES加密
function encryptByDES(message, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.ciphertext.toString();
}

//DES解密
function decryptByDES(ciphertext, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
var result_value = decrypted.toString(CryptoJS.enc.Utf8);
return result_value;
}

DES + Base64 加密和解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// DES 加密 + Base64 编码
function encryptByDES(message, key) {
// For the key, when you pass a string,
// it's treated as a passphrase and used to derive an actual key and IV.
// Or you can pass a WordArray that represents the actual key.
// If you pass the actual key, you must also pass the actual IV.
var keyHex = CryptoJS.enc.Utf8.parse(key);
// console.log(CryptoJS.enc.Utf8.stringify(keyHex), CryptoJS.enc.Hex.stringify(keyHex));
// console.log(CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(key).toString(CryptoJS.enc.Hex)));
// CryptoJS use CBC as the default mode, and Pkcs7 as the default padding scheme
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
// decrypt encrypt result
// var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, {
// mode: CryptoJS.mode.ECB,
// padding: CryptoJS.pad.Pkcs7
// });
// console.log(decrypted.toString(CryptoJS.enc.Utf8));
// when mode is CryptoJS.mode.CBC (default mode), you must set iv param
// var iv = 'inputvec';
// var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));
// var encrypted = CryptoJS.DES.encrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });
// var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });
// console.log('encrypted.toString() -> base64(ciphertext) :', encrypted.toString());
// console.log('base64(ciphertext) <- encrypted.toString():', encrypted.ciphertext.toString(CryptoJS.enc.Base64));
// console.log('ciphertext.toString() -> ciphertext hex :', encrypted.ciphertext.toString());
// return encrypted.toString();
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}

//Base64 解码 + DES 解密
function decryptByDES(ciphertext, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}

使用

1
2
3
4
5
6
7
8
var message = '111111';//需要加密的数据
var key = '12345678';//加密key
//加密
desMessage = encryptByDES(message, key);
console.log(desMessage);
//解密
message = decryptByDES(desMessage,key)
console.log(message);