Skip to content

Commit

Permalink
implement aes encryption with aes-256 cbc mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Deepal Jayasekara committed Jan 8, 2017
1 parent cc9371e commit 8bdc959
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 11 deletions.
5 changes: 0 additions & 5 deletions dist/lib/dukpt.lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/dukpt.lib.js.map

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions dist/lib/encryption.lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/lib/encryption.lib.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/test.js
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions dist/test/lib/encryption.lib.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/test/lib/encryption.lib.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions lib/dukpt.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ class Dukpt {
return DataOperations.XORdataHex(variantMask, derivedPEK); // apply mask
}

encryptAES(key, data, encrypt) {
throw new Error('not implemented yet!');
}

encryptTDES(key, data, encryptTrueFalse) {

let CBC = 1; // cipher block chaining enabled
Expand Down
46 changes: 46 additions & 0 deletions lib/encryption.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const crypto = require('crypto');

class Encryption {
static encryptAES(hexKey, hexData, encryptionAlgorithm) {

encryptionAlgorithm = encryptionAlgorithm || 'aes-256-cbc';

const keyBuf = Buffer.from(hexKey, 'hex');

if (keyBuf.length != 32) {
throw new Error('key for aes encryption must be 32 bytes in length');
}

const dataBuf = Buffer.from(hexData, 'hex');
const iv = Buffer.from('00000000000000000000000000000000', 'hex');

const cipher = crypto.createCipheriv(encryptionAlgorithm, keyBuf, iv).setAutoPadding(true);
let encrypted = cipher.update(dataBuf);
encrypted += cipher.final('binary');

return Buffer.from(encrypted, 'binary').toString('hex');
}

static decryptAES(hexKey, encryptedHexData, encryptionAlgorithm) {

encryptionAlgorithm = encryptionAlgorithm || 'aes-256-cbc';

const keyBuf = Buffer.from(hexKey, 'hex');

if (keyBuf.length != 32) {
throw new Error('key for AES encryption must be 32 bytes in length');
}

const dataBuf = Buffer.from(encryptedHexData, 'hex');
const iv = Buffer.from('00000000000000000000000000000000', 'hex');

const cipher = crypto.createDecipheriv(encryptionAlgorithm, keyBuf, iv);
let decrypted = cipher.update(dataBuf);
decrypted += cipher.final();

return Buffer.from(decrypted, 'ascii').toString('hex');

}
}

module.exports = Encryption;
58 changes: 58 additions & 0 deletions test/lib/encryption.lib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const RandExp = require('randexp');
const Encryption = require('../../lib/encryption.lib');
const crypto = require('crypto');
const should = require('should');

function generateRandomString() {
return new RandExp(/[a-zA-Z0-9]{10,20}/).gen();
}

describe('aes encryption test suite', () => {
it('should encrypt a string properly and should output a hex encoded string', (done) => {
const randomString = generateRandomString();
const key = crypto.createHash('sha256').update(randomString).digest();
const encrypted = Encryption.encryptAES(key,Buffer.from(randomString, 'ascii').toString('hex'));
(/^[a-fA-F0-9]+$/).test(encrypted).should.be.true();
done();
});

it('should throw an error when a key with an incorrect length is provided for encryption', (done) => {
const randomString = generateRandomString();
const key = crypto.createHash('sha512').update(randomString).digest();
try{
Encryption.encryptAES(key,Buffer.from(randomString, 'ascii').toString('hex'));
}
catch(err){
should.exist(err);
}

done();
})
});

describe('3des encryption test suite', () => {
it('should decrypt properly when encrypted string is provided', (done) => {
const randomString = generateRandomString();
const key = crypto.createHash('sha256').update(randomString).digest();
const encrypted = Encryption.encryptAES(key,Buffer.from(randomString, 'ascii').toString('hex'));
const decrypted = Encryption.decryptAES(key, encrypted);
(/^[a-fA-F0-9]+$/).test(decrypted).should.be.true();
Buffer.from(decrypted, 'hex').toString('ascii').should.equal(randomString);
done();
});

it('should throw an error when a key with incorrect length is provided for decryption', (done) => {
const randomString = generateRandomString();
const key = crypto.createHash('sha256').update(randomString).digest();
const decryptKey = crypto.createHash('sha512').update(randomString).digest();
const encrypted = Encryption.encryptAES(key,Buffer.from(randomString, 'ascii').toString('hex'));
try{
const decrypted = Encryption.decryptAES(decryptKey, encrypted);
}
catch(err){
should.exist(err);
err.message.should.equal('key for AES encryption must be 32 bytes in length');
}
done();
});
});

0 comments on commit 8bdc959

Please sign in to comment.