-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRsaAesGcmHybridEncryptionNodeJsCrypto.js
167 lines (148 loc) · 7.27 KB
/
RsaAesGcmHybridEncryptionNodeJsCrypto.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
const crypto = require('crypto')
const fs = require('fs');
console.log("RSA AES GCM hybrid encryption");
console.log("\nuses AES GCM 256 random key for the plaintext encryption");
console.log("and RSA OAEP SHA 1 2048 bit for the encryption of the random key\n");
var plaintextString = "The quick brown fox jumps over the lazy dog";
console.log("plaintext: ", plaintextString);
// # # # usually we would load the private and public key from a file or keystore # # #
// # # # here we use hardcoded keys for demonstration - don't do this in real programs # # #
var filenamePrivateKeyPem = "privatekey2048.pem";
var filenamePublicKeyPem = "publickey2048.pem";
// encryption
console.log("\n* * * Encryption * * *");
var publicKeyLoad = loadRsaPublicKeyPem();
// use this in production
//var publicKeyLoad = fs.readFileSync(filenamePublicKeyPem);
var completeCiphertextBase64 = rsaOaepSha1AesGcm256HybridStringEncryption (publicKeyLoad, plaintextString);
console.log("completeCiphertextBase64: ", completeCiphertextBase64);
// transport the encrypted data to recipient
// receiving the encrypted data, decryption
console.log("\n* * * Decryption * * *");
var completeCiphertextReceivedBase64 = completeCiphertextBase64;
console.log("completeCiphertextReceivedBase64: " + completeCiphertextReceivedBase64);
var privateKeyLoad = loadRsaPrivateKeyPem();
// use this in production
//var privateKeyLoad = fs.readFileSync(filenamePrivateKeyPem);
var decryptedtext = rsaOaepSha1AesGcm256HybridStringDecryption(privateKeyLoad, completeCiphertextReceivedBase64);
console.log("decryptedtext: " + decryptedtext);
function rsaOaepSha1AesGcm256HybridStringEncryption (publicKey, plaintext) {
// generate random key
var encryptionKey = generateRandomAesKey();
// encrypt plaintext
var ciphertextBase64 = aesGcmEncryptToBase64(encryptionKey, plaintext);
// encrypt the aes encryption key with the rsa public key
var encryptionKeyBase64 = rsaEncryptionOaepSha1(publicKey, encryptionKey);
return encryptionKeyBase64 + ':' + ciphertextBase64;
}
function rsaOaepSha1AesGcm256HybridStringDecryption (privateKey, completeCiphertext) {
var dataSplit = completeCiphertext.split(":");
var encryptionKeyReceived = base64Decoding(dataSplit[0]);
var nonce = base64Decoding(dataSplit[1]);
var ciphertext = dataSplit[2];
var gcmTag = base64Decoding(dataSplit[3]);
// decrypt the encryption key with the rsa private key
var decryptionKey = rsaDecryptionOaepSha1(privateKey, encryptionKeyReceived);
return aesGcmDecrypt(decryptionKey, nonce, ciphertext, gcmTag);
}
function aesGcmEncryptToBase64(key, data) {
var nonce = generateRandomNonce();
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce);
let encryptedBase64 = '';
cipher.setEncoding('base64');
cipher.on('data', (chunk) => encryptedBase64 += chunk);
cipher.on('end', () => {
// do nothing console.log(encryptedBase64);
// Prints: some clear text data
});
cipher.write(data);
cipher.end();
var nonceBase64 = base64Encoding(nonce);
var gcmTagBase64 = base64Encoding(cipher.getAuthTag());
return nonceBase64 + ':' + encryptedBase64 + ':' + gcmTagBase64;
}
function aesGcmDecrypt(key, nonce, ciphertext, gcmTag) {
const decipher = crypto.createDecipheriv('aes-256-gcm', key, nonce);
decipher.setAuthTag(gcmTag);
let decrypted = '';
decipher.on('readable', () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
decipher.on('end', () => {
// do nothing console.log(decrypted);
});
decipher.write(ciphertext, 'base64');
decipher.end();
return decrypted;
}
function generateRandomAesKey() {
return crypto.randomBytes(32);
}
function generateRandomNonce() {
return crypto.randomBytes(12);
}
function rsaEncryptionOaepSha1(publicKey, plaintext) {
var encrypted = crypto.publicEncrypt(
{ key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha1"}, plaintext);
return encrypted.toString('base64')
}
function rsaDecryptionOaepSha1(privateKey, ciphertext) {
var options = {key: privateKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha1" };
var decrypted = crypto.privateDecrypt(options, ciphertext);
return decrypted;
}
function base64Encoding(input) {
return input.toString('base64');
}
function base64Decoding(input) {
return Buffer.from(input, 'base64')
}
function loadRsaPrivateKeyPem() {
const key = '-----BEGIN PRIVATE KEY-----\n' +
'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwSZYlRn86zPi9\n' +
'e1RTZL7QzgE/36zjbeCMyOhf6o/WIKeVxFwVbG2FAY3YJZIxnBH+9j1XS6f+ewjG\n' +
'FlJY4f2IrOpS1kPiO3fmOo5N4nc8JKvjwmKtUM0t63uFFPfs69+7mKJ4w3tk2mSN\n' +
'4gb8J9P9BCXtH6Q78SdOYvdCMspA1X8eERsdLb/jjHs8+gepKqQ6+XwZbSq0vf2B\n' +
'MtaAB7zTX/Dk+ZxDfwIobShPaB0mYmojE2YAQeRq1gYdwwO1dEGk6E5J2toWPpKY\n' +
'/IcSYsGKyFqrsmbw0880r1BwRDer4RFrkzp4zvY+kX3eDanlyMqDLPN+ghXT1lv8\n' +
'snZpbaBDAgMBAAECggEBAIVxmHzjBc11/73bPB2EGaSEg5UhdzZm0wncmZCLB453\n' +
'XBqEjk8nhDsVfdzIIMSEVEowHijYz1c4pMq9osXR26eHwCp47AI73H5zjowadPVl\n' +
'uEAot/xgn1IdMN/boURmSj44qiI/DcwYrTdOi2qGA+jD4PwrUl4nsxiJRZ/x7PjL\n' +
'hMzRbvDxQ4/Q4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0wODDtzM/jtrpqyYa5JoEpPIaUSgPuTH/WhO3cDsx6\n' +
'3PIW4/CddNs8mCSBOqTnoaxh\n' +
'-----END PRIVATE KEY-----\n'; // important last crlf
return key;
}
function loadRsaPublicKeyPem() {
const key = '-----BEGIN PUBLIC KEY-----\n' +
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8EmWJUZ/Osz4vXtUU2S+\n' +
'0M4BP9+s423gjMjoX+qP1iCnlcRcFWxthQGN2CWSMZwR/vY9V0un/nsIxhZSWOH9\n' +
'iKzqUtZD4jt35jqOTeJ3PCSr48JirVDNLet7hRT37Ovfu5iieMN7ZNpkjeIG/CfT\n' +
'/QQl7R+kO/EnTmL3QjLKQNV/HhEbHS2/44x7PPoHqSqkOvl8GW0qtL39gTLWgAe8\n' +
'01/w5PmcQ38CKG0oT2gdJmJqIxNmAEHkatYGHcMDtXRBpOhOSdraFj6SmPyHEmLB\n' +
'ishaq7Jm8NPPNK9QcEQ3q+ERa5M6eM72PpF93g2p5cjKgyzzfoIV09Zb/LJ2aW2g\n' +
'QwIDAQAB\n' +
'-----END PUBLIC KEY-----\n'; // important last crlf
return key;
}