我得到了一个使用Ruby和EzCrypto库进行aes-128-cbc加密的字符串。
下面是我用Ruby编写的加密代码:
require 'rubygems'
require 'ezcrypto'
@pwd = 'hello'; @salt = 'salt'
key = EzCrypto::Key.with_password @pwd,@salt, :algorithm=>"aes-128-cbc"
File.open('key.txt','w') do |file|
file.write(key.to_s)
end
File.open('secret.txt','w') do |file|
file.write(key.encrypt("hello"))
end现在我想用Node来解密这个字符串。我什么也得不到。我一定是做错了什么。下面是我的Node代码。
var crypto = require('crypto');
var fs = require('fs');
var secret = fs.readFileSync('secret.txt', 'binary');
var key = fs.readFileSync('key.txt', 'base64');
var decipher = crypto.createDecipher('aes-128-cbc', key);
var string = decipher.update(secret, 'binary', 'utf8');
string += decipher.final('utf8');
console.log("STRING: ", string)返回:STRING:
任何帮助都将不胜感激。
发布于 2011-07-01 07:23:33
事实证明,这是Ruby实现OpenSSL的一个问题。如果你深入研究Ruby的源代码,你会发现:
https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L210
Ruby总是将iv或初始化向量设置为"OpenSSL for Ruby rulez!“这太可笑了。Ruby的OpenSSL加密开箱即用,不能与其他语言兼容。
这意味着EzCrypto不能与节点一起工作:-(
我为Ruby编写了自己的密码包装器,我手动设置了IV。一旦修复好了,其他的一切都会感觉到位。
我真的希望这对其他人有帮助。我花了好长时间才弄明白。
发布于 2011-06-30 07:28:30
secret.txt包含的是二进制,而不是预期的UTF-8/十六进制。
https://stackoverflow.com/questions/6528163
复制相似问题