对称加密算法

 

http://www.cnblogs.com/happyhippy/archive/2006/12/23/601353.html

  //参数的意思

  1. test_des({  
  2.     alg: 'des-ede3-cbc',   //进程  
  3.     autoPad: true, 
  4.     key: '0123456789abcd0123456789',  //钥匙
  5.     plaintext: '1234567812345678',  //明码文本
  6.     iv: '12345678'  //初始化向量
  7. })  

http://qson.iteye.com/blog/2041133

 

nodejs  对称加密

  1. var assert = require('assert');  
  2. var crypto = require('crypto');  
  3.   
  4. function test_des(param) {  
  5.     var key = new Buffer(param.key);  
  6.     var iv = new Buffer(param.iv ? param.iv : 0)  
  7.     var plaintext = param.plaintext  
  8.     var alg = param.alg  
  9.     var autoPad = param.autoPad  
  10.       
  11.     //encrypt  
  12.     var cipher = crypto.createCipheriv(alg, key, iv);  
  13.     cipher.setAutoPadding(autoPad)  //default true  
  14.     var ciph = cipher.update(plaintext, 'utf8', 'hex');  
  15.     ciph += cipher.final('hex');  
  16.     console.log(alg, ciph)  
  17.   
  18.     //decrypt  
  19.     var decipher = crypto.createDecipheriv(alg, key, iv);  
  20.     cipher.setAutoPadding(autoPad)  
  21.     var txt = decipher.update(ciph, 'hex', 'utf8');  
  22.     txt += decipher.final('utf8');      
  23.     assert.equal(txt, plaintext, 'fail');  
  24. }  
  25.   
  26. test_des({  
  27.     alg: 'des-ecb',  
  28.     autoPad: true,  
  29.     key: '01234567',  
  30.     plaintext: '1234567812345678',  
  31.     iv: null  
  32. })  
  33.   
  34. test_des({  
  35.     alg: 'des-cbc',  
  36.     autoPad: true,  
  37.     key: '01234567',  
  38.     plaintext: '1234567812345678',  
  39.     iv: '12345678'  
  40. })  
  41.   
  42. test_des({  
  43.     alg: 'des-ede3',    //3des-ecb  
  44.     autoPad: true,  
  45.     key: '0123456789abcd0123456789',  
  46.     plaintext: '1234567812345678',  
  47.     iv: null  
  48. })  
  49.   
  50. test_des({  
  51.     alg: 'des-ede3-cbc',    //3des-cbc  
  52.     autoPad: true,  
  53.     key: '0123456789abcd0123456789',  
  54.     plaintext: '1234567812345678',  
  55.     iv: '12345678'  
  56. })  
posted @ 2017-01-04 10:35  会奔跑的胖子  阅读(262)  评论(0编辑  收藏  举报