-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto-example.js
173 lines (147 loc) · 5.55 KB
/
crypto-example.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Example usage of crypto-utils.js functions
// This example demonstrates how to use the encryption/decryption functions
// in a way that matches the existing codebase patterns
// For browser usage via CDN
// <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
// For Node.js usage
// Uncomment this if running in Node.js:
// const CryptoJS = require('crypto-js');
const {
aesEncrypt,
aesDecrypt,
customAesEncrypt,
customAesDecrypt,
desEncrypt,
desDecrypt,
hmacSign,
base64Encode,
base64Decode
} = require('./crypto-utils');
// Example data
const plaintext = "Hello, this is sensitive data!";
const aesKey = "E987E8B6BBEC3F920D219F7A4B5C619A"; // Example key from codebase
const desKey = "BCA7264DA3DA67F286CF4D7882FC276E"; // Example key from codebase
const hmacKey = "your-hmac-secret-key";
// Convert hex key to WordArray for CryptoJS
const aesKeyWordArray = CryptoJS.enc.Hex.parse(aesKey);
const desKeyWordArray = CryptoJS.enc.Hex.parse(desKey);
// Create IV as WordArray (16 bytes of zeros)
const iv = CryptoJS.lib.WordArray.create(new Uint8Array(16));
// Example 1: AES Encryption/Decryption (Standard)
async function aesExample() {
try {
// Convert plaintext to WordArray
const plaintextWordArray = CryptoJS.enc.Utf8.parse(plaintext);
// Encrypt
const encrypted = await aesEncrypt(plaintextWordArray, aesKeyWordArray);
console.log('AES Encrypted:', encrypted);
// Decrypt
const decrypted = await aesDecrypt(encrypted, aesKeyWordArray);
// Convert WordArray back to UTF8
const decryptedText = CryptoJS.enc.Utf8.stringify(decrypted);
console.log('AES Decrypted:', decryptedText);
} catch (error) {
console.error('AES Error:', error);
}
}
// Example 2: Custom AES Implementation (matches codebase pattern)
async function customAesExample() {
try {
// Convert plaintext to WordArray
const plaintextWordArray = CryptoJS.enc.Utf8.parse(plaintext);
// Custom encrypt with IV
const customEncrypted = await customAesEncrypt(plaintextWordArray, aesKeyWordArray, iv);
console.log('Custom AES Encrypted:', customEncrypted);
// Custom decrypt with IV
const customDecrypted = await customAesDecrypt(customEncrypted, aesKeyWordArray, iv);
// Convert WordArray back to UTF8
const decryptedText = CryptoJS.enc.Utf8.stringify(customDecrypted);
console.log('Custom AES Decrypted:', decryptedText);
} catch (error) {
console.error('Custom AES Error:', error);
}
}
// Example 3: DES Encryption/Decryption
async function desExample() {
try {
// Convert plaintext to WordArray
const plaintextWordArray = CryptoJS.enc.Utf8.parse(plaintext);
// DES encrypt
const desEncrypted = await desEncrypt(plaintextWordArray, desKeyWordArray);
console.log('DES Encrypted:', desEncrypted);
// DES decrypt
const desDecrypted = await desDecrypt(desEncrypted, desKeyWordArray);
// Convert WordArray back to UTF8
const decryptedText = CryptoJS.enc.Utf8.stringify(desDecrypted);
console.log('DES Decrypted:', decryptedText);
} catch (error) {
console.error('DES Error:', error);
}
}
// Example 4: HMAC Signing
async function hmacExample() {
try {
// Convert plaintext and key to WordArray
const plaintextWordArray = CryptoJS.enc.Utf8.parse(plaintext);
const hmacKeyWordArray = CryptoJS.enc.Utf8.parse(hmacKey);
// Generate HMAC
const signature = await hmacSign(plaintextWordArray, hmacKeyWordArray);
console.log('HMAC Signature:', signature);
} catch (error) {
console.error('HMAC Error:', error);
}
}
// Example 5: Base64 Encoding/Decoding
function base64Example() {
try {
// Convert plaintext to WordArray
const plaintextWordArray = CryptoJS.enc.Utf8.parse(plaintext);
// Encode
const encoded = base64Encode(plaintextWordArray);
console.log('Base64 Encoded:', encoded);
// Decode
const decoded = base64Decode(encoded);
// Convert WordArray back to UTF8
const decodedText = CryptoJS.enc.Utf8.stringify(decoded);
console.log('Base64 Decoded:', decodedText);
} catch (error) {
console.error('Base64 Error:', error);
}
}
// Create HTML elements for browser display
function createHtmlOutput() {
const outputDiv = document.createElement('div');
outputDiv.id = 'crypto-output';
document.body.appendChild(outputDiv);
const log = console.log;
console.log = function(...args) {
log.apply(console, args);
const p = document.createElement('p');
p.textContent = args.join(' ');
outputDiv.appendChild(p);
};
}
// Run examples
async function runExamples() {
// Create output div if in browser
if (typeof window !== 'undefined') {
createHtmlOutput();
}
console.log('Running Crypto Examples...\n');
console.log('1. AES Example:');
await aesExample();
console.log('\n2. Custom AES Example:');
await customAesExample();
console.log('\n3. DES Example:');
await desExample();
console.log('\n4. HMAC Example:');
await hmacExample();
console.log('\n5. Base64 Example:');
base64Example();
}
// Run examples when document is ready (browser) or immediately (Node.js)
if (typeof window !== 'undefined') {
window.addEventListener('DOMContentLoaded', runExamples);
} else {
runExamples();
}