NODEJS(11)Platform
NODEJS(11)Platform - AUTH- AES1. AES - Introduction
DES-3DES-AES (Advanced Encryption Standard)
First to know
HEX is twice length of normal string. For example>
http://www.string-functions.com/string-hex.aspx
string(string) ——> HEX(737472696e67)
Second to know
List all the algorithms we support
>openssl list-cipher-algorithms
2. AES - Implementation
sillycat-aes/lib/app.js
var crypto = require('crypto');
module.exports = {
defaultAlgorithm: 'aes-256-cbc',
defaultFormat: 'hex',
ivLength: 16,
encrypt : function(data, key){
algorithm = this.defaultAlgorithm;
format = this.defaultFormat;
var iv = crypto.randomBytes(this.ivLength);
var cipher = crypto.createCipheriv(algorithm, new Buffer(key, 'hex'), iv);
var encrypted = cipher.update(data, 'utf8', format) + cipher.final(format);
//put the vector at the beginning
return iv.toString('hex') + encrypted;
},
decrypt : function(data, key){
algorithm = this.defaultAlgorithm;
format = this.defaultFormat;
var ivLength = this.ivLength * 2;
var iv = new Buffer(data.substring(0, ivLength), 'hex');
//get the vector from the begging of the string, hex is twice length the string
data = data.substring(ivLength);
var decipher = crypto.createDecipheriv(algorithm, new Buffer(key, 'hex'), iv);
var decrypted = decipher.update(data, format, 'utf8') + decipher.final('utf8');
return decrypted;
}
};
Here is the test class
sillcat-aes/test/encryptAndDecrypt.js
require("should");
var crypto = require('crypto');
var AESTool = require("../lib/app");
describe("Encrypt&Decrypt String", function(){
it("should encrypt&decrypt string password", function(){
console.log("");
var key = crypto.randomBytes(32);
var raw_data = "I love nodejs, I used to use javascript for a long time!";
var secret_data = AESTool.encrypt(raw_data, key);
console.log("raw_data is = " + raw_data);
console.log("secret data is = " + secret_data);
var decrypt_data = AESTool.decrypt(secret_data, key);
console.log("decrypt data is= " + decrypt_data);
decrypt_data.should.equal(raw_data);
});
});
3. Build the Test Case
>sudo npm install -g mocha
global install the mocha tool in my local machine.
value.should.be.a(‘object’).and.have.property(’name’, ‘Pedro’);
true.shuld.be.ok
false.should.ot.be.ok;
15.should.be.within(10,20);
“502”.should.match(/{3}/);
.should.have.length(3);
“abcdef”.should.include.string(‘bc’);
{a:1,b:2}.should.be.a(‘object’);
“test”.should.be.a(“string");
Then, try to run the test cases like this>
>mocha test/*.js
raw_data is = I love nodejs, I used to use javascript for a long time! secret data is = 39f8bdc4e8087fd4c53195702522db82c5bf60771fedfa765ea8b7d7ea6a69c29f09054a13c1a1e4d1ae52e285ebdc5ea62fe649e64781888626febfc384a3c3c7e306ae319ed586892009fc6596a890 decrypt data is= I love nodejs, I used to use javascript for a long time!
References:
AES Encrypt and Decrypt
http://blog.csdn.net/searchsun/article/details/2516191
http://nodejs.org/api/crypto.html
http://www.360doc.com/content/10/0326/17/229143_20367618.shtml
http://snoopyxdy.blog.163.com/blog/static/601174402012730105523656/
http://sillycat.iteye.com/blog/1468900
Dependency Injection
https://github.com/harmonyjs/dependency-injection
页:
[1]