Repository navigation
Expand file tree
/
Copy pathaesNodeExample.js
More file actions
55 lines (51 loc) · 2.75 KB
/
Copy pathaesNodeExample.js
File metadata and controls
55 lines (51 loc) · 2.75 KB
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
// A demonstration of AES128 CBC encryption and decryption for IIL meter communication
const crypto = require('crypto');
// Define the key and IV
const key = '69aF7&3KY0_kk89@'; //Default key
const iv = '420#abA%,ZfE79@M'; //Vector
// Convert the key and IV to buffers
const keyBuffer = Buffer.from(key, 'utf8');
const ivBuffer = Buffer.from(iv, 'utf8');
// Decryption function Example///////////////////////////////////////////////
function msgDecrypt(encryptedMessageHex, key, iv) {
// Create a decipher object with AES-128 CBC algorithm and 'NoPadding'
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
decipher.setAutoPadding(false); // Disable automatic padding //From Default PKCS7 set to NONE
try {
// Decrypt the message
let decryptedMessageHex = decipher.update(encryptedMessageHex, null, 'hex'); // Specify 'null' for input encoding and 'hex' for output encoding
decryptedMessageHex += decipher.final('hex');
return decryptedMessageHex;
} catch (error) {
console.error('Decryption error:', error.message);
}
}
// Encryption Example///////////////////////////////////////////////////////
function msgEncrypt(hexMessageBuffer, key, iv) {
// Create an AES-128-CBC cipher object with the key and IV
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
cipher.setAutoPadding(false); // Disable automatic padding //From Default PKCS7 set to NONE
try{
// Encrypt the hex-encoded message
let encryptedMessageHex2 = cipher.update(hexMessageBuffer, null, 'hex'); // Specify 'null' for input encoding and 'hex' for output encoding
encryptedMessageHex2 += cipher.final('hex');
return encryptedMessageHex2;
} catch (error) {
console.error('Encryption error:', error.message);
}
}
// Demo of Message Decryption function ///////////////////////////////////
const sampleMessage = '6d5c9d8bb016c12afc8bbaf8dd5fe7e1' //A typical sub-meter key-ex request message
// Define the hex-encoded encrypted message as a buffer
const encryptedMessageHex = Buffer.from(sampleMessage, 'hex'); // Incomming hex-encoded message
console.log('Origin Message (Hex):', sampleMessage);
console.log('Decrypted Message (Hex):', msgDecrypt(encryptedMessageHex, keyBuffer, ivBuffer));
console.log('');
// Demo of Message Encryption function ///////////////////////////////////
// Hex-encoded message to be encrypted
const hexMessage = '2a004a239000362f0200000000000000'; // decrypted message from the first part result
// Convert the hex-encoded message to a buffer
const hexMessageBuffer = Buffer.from(hexMessage, 'hex');
// the result should be same as the encrypted message in the first part (sampleMessage)
console.log('Origin Message (Hex):', hexMessage);
console.log('Encrypted Message (Hex):', msgEncrypt(hexMessageBuffer, keyBuffer, ivBuffer));