-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDesCbcStringEncryptionCryptoJs.js
129 lines (111 loc) · 5.05 KB
/
DesCbcStringEncryptionCryptoJs.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var CryptoJS = require("crypto-js");
console.log('\n# # # SECURITY WARNING: This code is provided for achieve # # #');
console.log('# # # compatibility between different programming languages. # # #');
console.log('# # # It is not necessarily fully secure. # # #');
console.log('# # # It uses the OUTDATED and UNSECURE algorithm DES. # # #');
console.log('# # # It uses FIXED key and IV\'s that should NEVER used, # # #');
console.log('# # # instead use random generated keys and IVs. # # #');
console.log('# # # DO NOT USE THIS CODE IN PRODUCTION # # #');
const plaintext = 'The quick brown fox jumps over the lazy dog';
console.log('\nplaintext: ' + plaintext);
console.log('\nDES CBC String encryption with fixed key & iv');
// fixed encryption key 8 bytes long
var desEncryptionKey = CryptoJS.enc.Utf8.parse("12345678");
var desEncryptionKeyBase64 = base64Encoding(desEncryptionKey);
console.log('desEncryptionKey (Base64): ', desEncryptionKeyBase64);
console.log('\n* * * Encryption * * *');
let desCiphertextBase64 = desCbcEncryptToBase64(desEncryptionKey, plaintext);
console.log('ciphertext (Base64): ' + desCiphertextBase64);
console.log('output is (Base64) iv : (Base64) ciphertext');
console.log('\n* * * Decryption * * *');
console.log('ciphertext (Base64): ', desCiphertextBase64);
console.log('input is (Base64) iv : (Base64) ciphertext');
var desDecryptedtext = desCbcDecryptFromBase64(desEncryptionKey, desCiphertextBase64);
console.log('plaintext: ', desDecryptedtext);
console.log('\nTriple DES CBC String encryption with fixed key (16 bytes) & iv');
var des2EncryptionKey = CryptoJS.enc.Utf8.parse("1234567890123456");
console.log('des2EncryptionKey (Base64): ', base64Encoding(des2EncryptionKey));
console.log('\n* * * Encryption * * *');
let des2CiphertextBase64 = des3CbcEncryptToBase64(des2EncryptionKey, plaintext);
console.log('ciphertext (Base64): ' + des2CiphertextBase64);
console.log('output is (Base64) iv : (Base64) ciphertext');
console.log('\n* * * Decryption * * *');
console.log('ciphertext (Base64): ', des2CiphertextBase64);
console.log('input is (Base64) iv : (Base64) ciphertext');
var des2Decryptedtext = des3CbcDecryptFromBase64(des2EncryptionKey, des2CiphertextBase64);
console.log('plaintext: ', des2Decryptedtext);
console.log('\nTriple DES CBC String encryption with fixed key (24 bytes) & iv');
var des3EncryptionKey = CryptoJS.enc.Utf8.parse("123456789012345678901234");
console.log('des3EncryptionKey (Base64): ', base64Encoding(des3EncryptionKey));
console.log('\n* * * Encryption * * *');
let des3CiphertextBase64 = des3CbcEncryptToBase64(des3EncryptionKey, plaintext);
console.log('ciphertext (Base64): ' + des3CiphertextBase64);
console.log('output is (Base64) iv : (Base64) ciphertext');
console.log('\n* * * Decryption * * *');
console.log('ciphertext (Base64): ', des3CiphertextBase64);
console.log('input is (Base64) iv : (Base64) ciphertext');
var des3Decryptedtext = des3CbcDecryptFromBase64(des3EncryptionKey, des3CiphertextBase64);
console.log('plaintext: ', des3Decryptedtext);
function desCbcEncryptToBase64(key, data) {
// don't use this in production
var iv = generateFixedDesInitvector();
//var iv = generateRandomDesInitvector();
const cipher = CryptoJS.DES.encrypt(data, key,
{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
return base64Encoding(iv) + ':' + cipher.toString();
}
function desCbcDecryptFromBase64(key, data) {
var dataSplit = data.split(":");
var iv = base64Decoding(dataSplit[0]);
var ciphertextDecryption = dataSplit[1];
const cipherDecryption = CryptoJS.DES.decrypt(ciphertextDecryption, key,
{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
return CryptoJS.enc.Utf8.stringify(cipherDecryption).toString();
}
function des3CbcEncryptToBase64(key, data) {
// don't use this in production
var iv = generateFixedDesInitvector();
//var iv = generateRandomDesInitvector();
const cipher = CryptoJS.TripleDES.encrypt(data, key,
{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
return base64Encoding(iv) + ':' + cipher.toString();
}
function des3CbcDecryptFromBase64(key, data) {
var dataSplit = data.split(":");
var iv = base64Decoding(dataSplit[0]);
var ciphertextDecryption = dataSplit[1];
const cipherDecryption = CryptoJS.TripleDES.decrypt(ciphertextDecryption, key,
{
iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
return CryptoJS.enc.Utf8.stringify(cipherDecryption).toString();
}
function generateRandomDesKey() {
return CryptoJS.lib.WordArray.random(64/8);
}
function generateRandomDesInitvector() {
return CryptoJS.lib.WordArray.random(64/8);
}
function generateFixedDesInitvector() {
return CryptoJS.enc.Utf8.parse("12345678");
}
function base64Encoding(input) {
return CryptoJS.enc.Base64.stringify(input);
}
function base64Decoding(input) {
return CryptoJS.enc.Base64.parse(input);
}