var crypto = require('crypto');
const encrypt = (key, data) => {
key = getSecretKey(key);
let crypted='';
let cipher = crypto.createCipheriv('aes-128-ecb', key, "");
crypted = cipher.update(data, 'utf8', 'binary');
crypted += cipher.final('binary');
crypted = new Buffer(crypted, 'binary').toString('base64');
return crypted;
}
const decrypt = (key, data) => {
key = getSecretKey(key);
let decipher = crypto.createDecipheriv('aes-128-ecb', key,"");
const buf1 = Buffer.from(data,"base64").toString('hex');
let decrypted = decipher.update(buf1, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
const getSecretKey = (key) => {
let keysha1 = crypto.createHash('sha1').update(key).digest('buffer');
let realkey = crypto.createHash('sha1').update(keysha1).digest('hex').substring(0,32);
return new Buffer(realkey,'hex');
}
let data = {"secretKey":"180395be-c87b-4831-b96a-1e91bd7167f7","username":"王**","account":"wangfajin"};
data.timestamp = Date.now();
console.log(encrypt(SECRET_KEY, JSON.stringify(data)));