用的是nodejs加密模块 var crypto = require(‘crypto’); 输入参数 var des_en=des_encrypt({ alg: ‘des-ecb’, autoPad: true, key: ‘test1234’, // 7465737431323334 plaintext: ‘12345678’, //3132333435363738 iv: null })
var cipher = crypto.createCipheriv(alg,key,iv);
cipher.setAutoPadding(autoPad) //default true
var ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');
console.log(alg, ciph)
加密的结果和第三方软件不一样,应该怎么设置啊 加密后的:f525a4549f160e0d8eb1fcf3245119e3 第三方软:F525A4549F160E0D
你这个结果不是完全一样么,只需要把node的结果 slice 一下
@steambap 意思是后面的是没有用的数据吗?当然如果用这个截取后的长度在解密是不行的,报错!必须要原来的那个才能解密!不太清楚,请教下 //解密程序 function des_decrypt(param){ var key = Buffer.from(param.key); if (param.iv) { var iv =Buffer.from( param.iv ); }else{ var iv =Buffer.alloc(0); } var plaintext = param.plaintext; var alg = param.alg; var autoPad = param.autoPad; //decrypt 解密 var decipher = crypto.createDecipheriv(alg, key, iv); decipher.setAutoPadding(autoPad) var txt = decipher.update(plaintext, ‘hex’, ‘utf8’); txt += decipher.final(‘utf8’); console.log(alg, txt) return txt; }
console.log(’------------’); var des_de=des_decrypt({ alg: ‘des-ecb’, autoPad: true, key: ‘test1234’, plaintext: des_en1, iv: null }) console.log(‘解密后的:’+des_de);
plaintext‘ 这个地方如果是截取后的长度的话会保错,但是原来的就能解密出来’
报错内容: internal/crypto/cipher.js:164 const ret = this._handle.final(); ^
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipheriv.final (internal/crypto/cipher.js:164:28)
at des_decrypt (D:\ProNodejs\2018ITying\TCP\model\des01.js:97:21)
at Object.<anonymous> (D:\ProNodejs\2018ITying\TCP\model\des01.js:113:12)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
进程已结束,退出代码 1