-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCrypt.js
50 lines (40 loc) · 1.36 KB
/
testCrypt.js
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
/**
* Created by chaclus on 2017/1/10.
*/
var crypto = require('crypto')
,fs = require('fs');
//加密
function cipher(algorithm, key, buf ,cb){
var encrypted = "";
var cip = crypto.createCipher(algorithm, key);
encrypted += cip.update(buf, 'binary', 'hex');
encrypted += cip.final('hex');
cb(encrypted);
}
//解密
function decipher(algorithm, key, encrypted,cb){
var decrypted = "";
var decipher = crypto.createDecipher(algorithm, key);
decrypted += decipher.update(encrypted, 'hex', 'binary');
decrypted += decipher.final('binary');
cb(decrypted);
}
var cipherDecipher = function (data, algorithm, key) {
var s1 = new Date();
cipher(algorithm, key, data, function (encrypted) {
var s2 = new Date();
console.log('cipher:' + algorithm + ',' + (s2 - s1) + 'ms' + ">>>>" + encrypted + " :::::: " + encrypted.length);
decipher(algorithm, key, encrypted, function (txt) {
var s3 = new Date();
console.log('decipher:' + algorithm + ',' + (s3 - s2) + 'ms');
// console.log(txt);
});
});
};
//console.log(crypto.getCiphers());
var algs = ['blowfish','aes-256-cbc','cast','des','des3','idea','rc2','rc4','seed'];
var key = "abc";
var filename = "12345678901234567890";//"package.json";
algs.forEach(function (name) {
cipherDecipher(filename, name, key);
});