gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:08 27 4
gpt4 key购买 nike

这与我昨天的问题有关(结果很好): 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com