ruby - 在 ruby​​ 中解密河豚加密字符串仅返回该字符串的 1/2

标签 ruby encryption blowfish

这与我昨天的问题有关(得到了很好的结果): Encrypting a string with Blowfish in ruby returns a shorter string than the same process in php

现在我认为在相反的方向上有类似的问题。我使用 php 加密字符串:

php > require_once 'Crypt/Blowfish.php';
php > $input = "input string";
php > $key = "some key";
php > $crypt = new Crypt_Blowfish($key);
php > echo bin2hex($crypt->encrypt($input));
79af8c8ee9220bdec2d1c9cfca7b13c6

这正是预期的结果。但是,当我尝试解密 ruby​​ 中的字符串时,它只提供了输入的一个子集:

irb(main):001:0> require 'rubygems'
r=> true
irb(main):002:0> require 'crypt/blowfish'
=> true
irb(main):003:0> key = "some key"
=> "some key"
irb(main):004:0> input = "79af8c8ee9220bdec2d1c9cfca7b13c6"
=> "79af8c8ee9220bdec2d1c9cfca7b13c6"
irb(main):005:0> block = input.gsub(/../) { |match| match.hex.chr }
=> "y\257\214\216\351\"\v\336\302\321\311\317\312{\023\306"
irb(main):006:0> blowfish = Crypt::Blowfish.new(key)
=> #<Crypt::Blowfish:0xb73acbd8 @sBoxes=[[3156471959, 1769696695, 1443271708, 181204541, 
... 1894848609], @key="some key">
irb(main):008:0> blowfish.decrypt_block(block)
=> "input st"

知道我现在在做什么愚蠢的事情吗?

最佳答案

河豚 block 是 8-bytes long 。请注意,这正是您要求解密一个 block 时返回的字符数。

必须有更多代码才能获得最终 block ,否则您需要在接下来的 8 个字节上再次调用decrypt_block。

您可能想尝试decrypt_string,而不是调用decrypt_block。

来自测试:

userkey = "A BIG KEY"
bf = Crypt::Blowfish.new(userkey) 
string = "This is a string which is not a multiple of 8 characters long"
encryptedString = bf.encrypt_string(string)
decryptedString = bf.decrypt_string(encryptedString)
assert_equal(string, decryptedString)

关于ruby - 在 ruby​​ 中解密河豚加密字符串仅返回该字符串的 1/2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5474205/

相关文章:

php blowfish 哈希与 crypt() : the CLI result differs from webserver's one

c# - 如何在 C# 中使用 BouncyCaSTLe 进行 Blowfish 单向哈希?

ruby - 在没有 RVM、Rbenv 的情况下全局更新 ruby​​ OSX

ruby - 跳过扩展名为 pdf、zip 的网页,从 Anemone 中爬行

java - 使用java swing加密html文件

debugging - 需要现实检查 : Is my analysis of this VB6 Blowfish bug correct?

ruby-on-rails - 在 Ruby on Rails 中使用无限 float

ruby - 在简短示例中发出调用方法

java - Blowfish 加密问题

C# 转换 XOR crypt 函数