dongtao9158 2014-12-22 20:42
浏览 79

如何在Ruby中执行3DES加密?

I have a legacy Yii PHP system that is performing 3DES Encryption.

$data = 'id=1,username=admin,email=webmaster@example.com';
$encrypted_key = mhash(MHASH_SHA1,"foo_bar_key");
$encrypted_value = $StringHelper::encrypt($data, $encrypted_key); 
echo $encrypted_value 
//output => "0G8ITzc32wvqpDIjt6O7tZV9VKwU8KK7p0xI%2Fa%2FjiPOR%2FLrpfvsfMg9fyV2vAHgf"

I'm trying to get this same encrypted value in Ruby using 3DES encryption but I am not able to get it.

require 'openssl'
require 'mhash'
require 'base64'

@data = "id=1,username=admin,email=webmaster@example.com"
@encrypted_key = Mhash.sha1("foo_bar_key")

cipher = OpenSSL::Cipher::Cipher.new('des-ede-cbc')
cipher.encrypt
cipher.key = @encrypted_key
output = cipher.update(@data)

puts output 
# output => "\xFC\xCAR\xE2L\xB1\x1A\xCA\x83E\x9C\xEEA\xFD\x9D'I\v\x8E9\xFA4\x14\x1F&\xBFR\x8A\xD3\xBDL\xC64\xABf\xCD\x85\x87\x88^"
#Base64.encode64(output) => "/MpS4kyxGsqDRZzuQf2dJ0kLjjn6NBQfJr9SitO9TMY0q2bNhYeIXg==
"

Does anyone know what I'm doing wrong?

Thanks.

  • 写回答

2条回答 默认 最新

  • drtoaamk20278 2014-12-22 23:53
    关注

    I haven't been able to find the $StringHelper::encrypt function, but if it is compatible, you just need to also perform cipher.final in your Ruby code and concatenate the output of update and final as clearly specified in the instructions.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来