@MarcusTettmar/

js_aes_267_cbc_1

Nodejs

Javascript AES_256_CBC example with SHA256 key and IV.

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6143926015786103","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"LyoKQ29tcGF0aWJsZSBNYWNybyBTY2hlZHVsZXIgKHd3dy5tanRuZXQuY29tKSBjb2RlOgogIExldD5BRVNfQUxHPUFFU18yNTZfQ0JDCiAgQUVTRW5jcnlwdD5oZWxsbyB3b3JsZCxteXBhc3N3b3JkLEVOQ1JZUFQscmVzdWx0CiAgQUVTRW5jcnlwdD5yZXN1bHQsbXlwYXNzd29yZCxERUNSWVBULG9yaWdpbmFsCiovCgpsZXQgZGF0YSA9ICdoZWxsbyB3b3JsZCc7CmxldCBwYXNzd29yZCA9ICdteXBhc3N3b3JkJzsKbGV0IGl2ID0gJzAwMDAwMDAwMDAwMDAwMDAnOwoKbGV0IHBhc3N3b3JkX2hhc2ggPSBjcnlwdG8uY3JlYXRlSGFzaCgnc2hhMjU2JykudXBkYXRlKHBhc3N3b3JkLCd1dGY4JykuZGlnZXN0KCdoZXgnKTsKCmxldCBrZXkgPSBoZXgyYmluKHBhc3N3b3JkX2hhc2gpOwpwYXNzd29yZF9oYXNoID0gQnVmZmVyLmFsbG9jKDMyLGtleSwiYmluYXJ5Iik7CgpsZXQgY2lwaGVyID0gY3J5cHRvLmNyZWF0ZUNpcGhlcml2KCdhZXMtMjU2LWNiYycsIHBhc3N3b3JkX2hhc2gsIGl2KTsKCmxldCBlbmNyeXB0ZWREYXRhID0gY2lwaGVyLnVwZGF0ZShkYXRhLCAndXRmOCcsICdiYXNlNjQnKSArIGNpcGhlci5maW5hbCgnYmFzZTY0Jyk7Cgpjb25zb2xlLmxvZygnQmFzZTY0IEVuY3J5cHRlZDonLCAgZW5jcnlwdGVkRGF0YSk7CgpsZXQgZGVjaXBoZXIgPSBjcnlwdG8uY3JlYXRlRGVjaXBoZXJpdignYWVzLTI1Ni1jYmMnLCBwYXNzd29yZF9oYXNoLCBpdik7CgpsZXQgZGVjcnlwdGVkVGV4dCA9IGRlY2lwaGVyLnVwZGF0ZShlbmNyeXB0ZWREYXRhLCAnYmFzZTY0JywgJ3V0ZjgnKSArIGRlY2lwaGVyLmZpbmFsKCd1dGY4Jyk7Cgpjb25zb2xlLmxvZygnRGVjcnlwdGVkIFRleHQ6JywgZGVjcnlwdGVkVGV4dCkKCmZ1bmN0aW9uIGhleDJiaW4oaGV4KQp7CiAgICB2YXIgYnl0ZXMgPSBbXSwgc3RyOwoKICAgIGZvcih2YXIgaT0wOyBpPCBoZXgubGVuZ3RoLTE7IGkrPTIpCiAgICAgICAgYnl0ZXMucHVzaChwYXJzZUludChoZXguc3Vic3RyKGksIDIpLCAxNikpOwoKICAgIHJldHVybiBTdHJpbmcuZnJvbUNoYXJDb2RlLmFwcGx5KFN0cmluZywgYnl0ZXMpOyAgICAKfQ=="},"asBuffer":null},"loaded":true}}
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
/*
Compatible Macro Scheduler (www.mjtnet.com) code:
  Let>AES_ALG=AES_256_CBC
  AESEncrypt>hello world,mypassword,ENCRYPT,result
  AESEncrypt>result,mypassword,DECRYPT,original
*/

let data = 'hello world';
let password = 'mypassword';
let iv = '0000000000000000';

let password_hash = crypto.createHash('sha256').update(password,'utf8').digest('hex');

let key = hex2bin(password_hash);
password_hash = Buffer.alloc(32,key,"binary");

let cipher = crypto.createCipheriv('aes-256-cbc', password_hash, iv);

let encryptedData = cipher.update(data, 'utf8', 'base64') + cipher.final('base64');

console.log('Base64 Encrypted:',  encryptedData);

let decipher = crypto.createDecipheriv('aes-256-cbc', password_hash, iv);

let decryptedText = decipher.update(encryptedData, 'base64', 'utf8') + decipher.final('utf8');

console.log('Decrypted Text:', decryptedText)

function hex2bin(hex)
{
    var bytes = [], str;

    for(var i=0; i< hex.length-1; i+=2)
        bytes.push(parseInt(hex.substr(i, 2), 16));

    return String.fromCharCode.apply(String, bytes);    
}
node v10.16.0