-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathencryptDecrypt.js
More file actions
100 lines (62 loc) · 2.58 KB
/
encryptDecrypt.js
File metadata and controls
100 lines (62 loc) · 2.58 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
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
'use strict';
const https = require('https');
const crypto = require('crypto');
/*
Special thansk to Ben Nadel
https://www.bennadel.com/blog/3118-using-aes-ecb-pkcs5padding-encryption-in-coldfusion-and-decrypting-values-in-node-js.htm
*/
const tokenKey = "x8FWQUedjyiUGlTf5appPQ==";
const appCode = "DEMOAPP";
const companyCode = "10";
const version = "2";
const method = "getStudents";
const endPoint = "https://api.tassweb.com.au/tassweb/api/index.cfm";
let parameterString = '{"currentstatus":"current"}';
var encrypt = function( parameterString , tokenKey ) {
var binaryEncryptionKey = new Buffer.from( tokenKey, "base64" );
var cipher = crypto.createCipheriv( "AES-128-ECB", binaryEncryptionKey, Buffer.alloc(0) );
var encryptedString = (
cipher.update( parameterString, "utf8", "base64" ) +
cipher.final( "base64" )
);
return encryptedString;
};
var decrypt = function( encryptedInput , tokenKey ) {
var binaryEncryptionKey = new Buffer.from( tokenKey, "base64" );
var binaryIV = new Buffer.from( '' );
var decipher = crypto.createDecipheriv( "AES-128-ECB", binaryEncryptionKey, binaryIV );
// When decrypting we're converting the Base64 input to UTF-8 output.
var decryptedString = (
decipher.update( encryptedInput, "base64", "utf8" ) +
decipher.final( "utf8" )
);
return decryptedString;
};
var constructRequest = function() {
var token = encodeURIComponent( encrypt ( parameterString , tokenKey ) );
return endPoint + '?appcode=' + appCode +'&v=' + version + '&method=' + method + '&company=' + companyCode + '&token=' + token;
};
var makeAPIRequest = function(){
var requestURL = constructRequest();
https.get( requestURL , (resp) => {
if ( resp.statusCode === 200 ) {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
let searchResponse = JSON.parse(data);
console.log( searchResponse );
});
} else {
console.log( "Error: " + resp.statusCode );
}
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
console.log( 'Original Paramater String : ' + parameterString );
console.log( 'Encrypted Paramater String : ' + encrypt ( parameterString , tokenKey ) );
console.log( 'Decrypted Paramater String : ' + decrypt ( encrypt ( parameterString , tokenKey ) , tokenKey ) );
console.log( 'Request URL : ' + constructRequest() );
makeAPIRequest();