在ruby中用Blowfish加密字符串返回比php中相同的进程短的字符串 [英] Encrypting a string with Blowfish in ruby returns a shorter string than the same process in php

查看:143
本文介绍了在ruby中用Blowfish加密字符串返回比php中相同的进程短的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是令我困惑的。



当我尝试使用以下输入用Blowfish加密字符串:
key =some key
input =input string



我得到以下结果:

  ruby​​:[79af8c8ee9220bde ] 
php:79af8c8ee9220bdec2d1c9cfca7b13c6

我将从php应用程序接收字符串我需要让这两个同步,但我不明白为什么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(主):001:0>需要'rubygems'
=> true
irb(main):002:0>需要'crypt / blowfish'
=> true
irb(main):003:0> input =input string
=> input string
irb(main):004:0> key =some key
=> 一些关键
irb(main):005:0> blowfish = Crypt :: Blowfish.new(key)
=> #< Crypt :: Blowfish:0xb74b10c4 @sBoxes = [[3156471959,1769696695,1443271708,181204541,
...... 1894848609],@ key =some key>
irb(main):006:0> blowfish.encrypt_block(input)
=> y\257\214\216\351\\v\336
irb(main):007:0> blowfish.encrypt_block(input).unpack(H * )
=> [79af8c8ee9220bde]


解决方案

p>假设 Crypt_Blowfish 使用 mcrypt 或者行为就像它,你遇到一个填充问题,特别是字符串被正确填充为空字节,直到它是长的作为块大小的倍数从PHP交互式shell:

  php> $ bf = mcrypt_module_open('blowfish', '','ecb',''); 
php> $ iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($ bf),MCRYPT_DEV_RANDOM);
php> $ key ='some key';
php> mcrypt_generic_init($ bf,$ key,$ iv);
php> echo mcrypt_enc_get_block_size($ td);
8
php> echo bin2hex(mcrypt_generic($ bf, 'input string'));
79af8c8ee9220bdec2d1c9cfca7b13c6
php> echo bin2hex(mcrypt_generic($ td,input string\0\0\0\0));
79af8c8ee9220bdec2d1c9cfca7b13c6

似乎没有一个明显的方法来更改填充模式 mcrypt 中,而我不知道谁写了你正在使用的图书馆。在模块的文档中检查一个填充模式。



任何运气,您只需设置Ruby的填充模式,或者只是在Ruby的一边填空字符串。 / p>

This is confusing me.

When I try and use the following inputs to encrypt a string with Blowfish: key = "some key" input = "input string"

I get the following results:

ruby: ["79af8c8ee9220bde"]
php: 79af8c8ee9220bdec2d1c9cfca7b13c6

I am going to be receiving strings from a php application so I need to get these two to sync up but I don't understand why the php string would be longer. What am I missing?

php code:

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 code:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'crypt/blowfish'
=> true
irb(main):003:0> input = "input string"
=> "input string"
irb(main):004:0> key = "some key"
=> "some key"
irb(main):005:0> blowfish = Crypt::Blowfish.new(key)
=> #<Crypt::Blowfish:0xb74b10c4 @sBoxes=[[3156471959, 1769696695, 1443271708, 181204541, 
...... 1894848609], @key="some key">
irb(main):006:0> blowfish.encrypt_block(input)
=> "y\257\214\216\351\"\v\336"
irb(main):007:0> blowfish.encrypt_block(input).unpack("H*")
=> ["79af8c8ee9220bde"]

解决方案

Assuming that Crypt_Blowfish either uses mcrypt or acts just like it, you're encountering a padding issue. In particular, the string is being right-padded with null bytes until it's as long as a multiple of the block size. From the PHP interactive shell:

php > $bf = mcrypt_module_open('blowfish', '', 'ecb', '');
php > $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($bf), MCRYPT_DEV_RANDOM);
php > $key = 'some key';
php > mcrypt_generic_init($bf, $key, $iv);
php > echo mcrypt_enc_get_block_size($td);
8
php > echo bin2hex(mcrypt_generic($bf, 'input string'));
79af8c8ee9220bdec2d1c9cfca7b13c6
php > echo bin2hex(mcrypt_generic($td, "input string\0\0\0\0"));
79af8c8ee9220bdec2d1c9cfca7b13c6

There doesn't seem to be an obvious way to change the padding mode in mcrypt, and I don't know who wrote the library you're using. Check for a padding mode in the module's documentation.

With any luck, you can just set Ruby's padding mode instead, or simply null-pad the string on Ruby's side.

这篇关于在ruby中用Blowfish加密字符串返回比php中相同的进程短的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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