test2.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var crypto = require('crypto')
  2. ,fs = require('fs');
  3. function customPadding(str) {
  4. str = new Buffer(str,"utf8").toString("hex");
  5. var bitLength = str.length*8;
  6. if(bitLength < 256) {
  7. for(i=bitLength;i<256;i+=8) {
  8. str += 0x0;
  9. }
  10. } else if(bitLength > 256) {
  11. while((str.length*8)%256 != 0) {
  12. str+= 0x0;
  13. }
  14. }
  15. return new Buffer(str,"hex").toString("utf8");
  16. }
  17. //加密
  18. function cipher(algorithm, key, buf ,cb){
  19. var encrypted = "";
  20. var key = new Buffer(key);
  21. iv = new Buffer(0);
  22. var cip = crypto.createCipheriv(algorithm, key,iv);
  23. //cip.setAutoPadding(false);
  24. // buf = customPadding(buf);
  25. encrypted += cip.update(buf, 'utf8', 'base64');
  26. encrypted += cip.final('base64');
  27. return encrypted;
  28. }
  29. //解密
  30. function decipher(algorithm, key, encrypted,cb){
  31. var decrypted = "";
  32. var key = new Buffer(key);
  33. iv = new Buffer(0);
  34. var decipher = crypto.createDecipheriv(algorithm, key, iv);
  35. //decipher.setAutoPadding(false);
  36. decrypted += decipher.update(encrypted, 'base64', 'utf8');
  37. decrypted += decipher.final('utf8');
  38. return decrypted;
  39. }
  40. function cipherDecipherFile(data,algorithm, key){
  41. var s1 = new Date();
  42. let encrypted = cipher(algorithm, key,data);
  43. let txt = decipher(algorithm, key,encrypted);
  44. //console.log("原文:",txt,"des:",encrypted,"\n");
  45. }
  46. //console.log(crypto.getCiphers());
  47. var algs = [
  48. 'des-ecb'
  49. ];
  50. var key="miaomiao";
  51. var filename = "./b.txt";//"package.json";
  52. algs.forEach(function(name){
  53. var data_list = [
  54. {"aavv":new Date().getTime(), "ddsfds ":"sdfjk*===%sdfkjlk"},
  55. {"id":2343,"route":"im/login","data":{"a":"ddddddd"}},
  56. {"key= &&yy":{"aavv":new Date().getTime(), "ddsfds ":"sdfjk*===%sdfkjlk"}},
  57. {"key= &&yy":{"aa=你好=34&vv":new Date().getTime(), "dds234^&%fds ":"sdfjk*===%sdfkjlk"}},
  58. {"key= &&yy":{"aa=ʚ🌝ɞ阿lu=34&vv":new Date().getTime(), "dds234^&%fds ":{"ds7^*&":33333}}},
  59. ];
  60. let s = new Date().getTime();
  61. for (let j = 0; j < 10;j++) {
  62. for (let i = 0; i < data_list.length; i++) {
  63. cipherDecipherFile(JSON.stringify(data_list[i]),name,key);
  64. }
  65. }
  66. let e = new Date().getTime();
  67. console.log(e - s);
  68. })