使用ruby对节点加密数据 [英] Encrypting data with ruby decrypting with node

查看:162
本文介绍了使用ruby对节点加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密ruby应用程序中的一些数据,然后在nodejs应用程序中解码。我一直在努力让这个工作,现在我只是试图用两种语言加密相同的数据,以获得相同的结果,但我似乎不能这样做。

I want to encrypt some data in a ruby app and then decode it in a nodejs app. I have been trying to get this to work and now I am just trying to encrypt the same piece of data in both languages to get the same result but I can't seem to do it.

//js
var crypto = require('crypto');

var key = crypto.createHash('sha1').update('key').digest('hex');
console.log(key); // a62f2225bf70bfaccbc7f1ef2a397836717377de

var encrypted = "";
var cipher = crypto.createCipher('bf-cbc', key);

encrypted += cipher.update('text');
encrypted += cipher.final('hex');

console.log(encrypted); //outputs 4eafd5542875bd3c

所以看起来像是从编码中得到一个十六进制字符串。

So it looks like I get a hexadecimal string from the encoding.

#ruby
require 'openssl'
require 'digest/sha1'
c = OpenSSL::Cipher::Cipher.new("bf-cbc")
c.encrypt
# your pass is what is used to encrypt/decrypt
c.key = key = Digest::SHA1.hexdigest("key")
p key # a62f2225bf70bfaccbc7f1ef2a397836717377de
e = c.update("text")
e << c.final
p e # 皋?;??

我缺少某种编码问题。我试图base64解码e,但是没有产生与节点应用程序相同的结果。任何指针?

Is there some sort of encoding issue that I am missing. I tried to base64 decode e but that didn't produce the same result as the node app. Any pointers?

更新:所以这是和朋友一样接近,我可以得到: https://gist.github.com/a880ea13d3b65a21a99d 。 Sheesh,我只是想在ruby加密一些东西,并在节点解密。

UPDATE: So this is as close as a friend and I can get: https://gist.github.com/a880ea13d3b65a21a99d. Sheesh, I just want to encrypt something in ruby and decrypt it in node.

UPDATE2:好吧,这个问题的代码让我有很多方法: a href =https://github.com/joyent/node/issues/1395 =nofollow> https://github.com/joyent/node/issues/1395

UPDATE2: Alright, the code in this issue gets me a lot of the way there: https://github.com/joyent/node/issues/1395

推荐答案

确定。我要感谢大家帮助我。基本上这个线程在这里回答我的问题: https://github.com/joyent/node/issues/1395 。我将继续前进,发布两个程序,以防任何人必须通过这个rigamarole。记住这不是意味着是核心安全的,这是红宝石加密数据和节点解密它的垫脚石。

OK. I want to thank everyone for helping me out. Basically this thread here answers my question: https://github.com/joyent/node/issues/1395. I am going to go ahead and post the two programs in case anyone else has to go through this rigamarole. Keep in mind this isn't mean to be hardcore secure, this is a stepping stone for ruby encrypting data and node decrypting it. You will have to take more steps to make sure higher security measures are taken.

代码位于此提示: https://gist.github.com/799d6021890f34734470

这些都是在ruby 1.9.2p290上运行的和节点0.4.10

These were run on ruby 1.9.2p290 and node 0.4.10

这篇关于使用ruby对节点加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆