使用node.js解密openssl aes-256-cbc?

时间:2017-11-14 23:06:01

标签: node.js cryptojs

我使用以下命令在命令行加密我的文件:

openssl aes-256-cbc -e -in test.env -out test.env.encrypted

然后尝试使用:

在node.js上解密
crypto    = require('crypto')
algorithm = 'aes-256-cbc'
password  = 'test'

fs = require 'fs'
decrypt = (text) ->
  decipher = crypto.createDecipher(algorithm, password)
  dec = decipher.update(text, 'hex', 'utf8')
  dec += decipher.final('utf8')
  dec

file = fs.readFileSync './test.env.encrypted', 'utf-8'

console.log decrypt file

但我收到以下错误:

TypeError: Bad input string at Decipher.update (crypto.js:168:26) at decrypt (/Users/h/tmp/encrypt_test/test.coffee:10:18) at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:16:13) at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:1:1) at Module._compile (module.js:569:30) at Object.CoffeeScript.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/index.js:63:23) at compileScript (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:265:29) at compilePath (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:220:14) at Object.exports.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:141:20) at Object.<anonymous> (/usr/local/lib/node_modules/coffeescript/bin/coffee:15:45) at Module._compile (module.js:569:30) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:503:32) at tryModuleLoad (module.js:466:12) at Function.Module._load (module.js:458:3) at Function.Module.runMain (module.js:605:10) at startup (bootstrap_node.js:158:16) at bootstrap_node.js:575:3

我做错了什么?

UPDATE:

根据詹姆斯的说法,我现在正在发送一个&#34; hex&#34;字符串到我的解密函数:

crypto    = require('crypto')
algorithm = 'aes-256-cbc'
password  = 'test'

fs = require 'fs'

decrypt = (text) ->
  decipher = crypto.createDecipher(algorithm, password)
  dec = decipher.update(text, 'hex', 'utf8')
  dec += decipher.final('utf8')
  dec

file = fs.readFileSync './test.env.encrypted'

console.log decrypt file.toString('hex')

然后它产生了一个新的错误:

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
    at Decipher.final (crypto.js:181:26)
    at decrypt (/Users/h/tmp/encrypt_test/test.coffee:12:19)
    at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:17:13)
    at Object.<anonymous> (/Users/h/tmp/encrypt_test/test.coffee:1:1)
    at Module._compile (module.js:569:30)
    at Object.CoffeeScript.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/index.js:63:23)
    at compileScript (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:265:29)
    at compilePath (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:220:14)
    at Object.exports.run (/usr/local/lib/node_modules/coffeescript/lib/coffeescript/command.js:141:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/coffeescript/bin/coffee:15:45)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

文件使用以下方式加密:

openssl aes-256-cbc -e -in test.env -out test.env.encrypted

test

作为密码

1 个答案:

答案 0 :(得分:1)

您对decipher.update的调用需要hex个编码字符串,但您传递的是utf-8字符串。

readFileSync更改为使用hex编码阅读,或将update调用更改为期望utf-8输入。