本文介绍了如何将128 CFB加密到Ruby?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要交换一个隐藏请求和答案的PHP API。在我身边,我在rails 4.0.0(ruby 2.0),我不能使它工作。



我已经阅读了关于这个主题的很多答案,并试图了解mcrypt的工作原理,例如,但没有成功。我仍然无法解密加密的PHP或加密PHP可以解密的东西



你能帮我看看我做错了什么?



PHP代码

  $ secretKey =1234567891234567; 
$ encrypt = urlencode(base64_encode(mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
md5($ secretKey))
$ cleartext,
MCRYPT_MODE_CFB,
$ secretKey
)));

$ input = urldecode($ input);
$ decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
md5($ secretKey),
base64_decode($ input),
MCRYPT_MODE_CFB,
$ secretKey);

Ruby代码


$ b $
密码= OpenSSL :: Cipher :: AES.new(256,:CFB)
密码。
cipher.key = Digest :: MD5.hexdigest(1234567891234567)
cipher.iv =1234567891234567
encrypted = cipher.update(params.to_query)+ cipher.final

CGI.escape(Base64.strict_encode64(加密))
end

def self.decode(answer)
decrypted = Base64.decode64(CGI。 unescape(answer))

解密= OpenSSL :: Cipher :: AES.new(256,:CFB)
decipher.decrypt
decipher.key = Digest :: MD5。 hexdigest(1234567891234567)
decipher.iv =1234567891234567
decoded = decipher.update(decryptpted)+ decipher.final
end
/ pre>

解决方案

你必须使用'ncfb' code> PHP代码中的MCRYPT_MODE_CFB 。 PHP默认为8位反馈,而不是整个块大小的反馈。



或者您可以指定:CFB8 在Ruby中与PHP兼容。在OpenSSL文档中阅读CFB的文档后,我猜这个。



非常感谢,我才发现,因为我知道我在寻找什么。 >

I need to exchange with a PHP API which crypts the requests and answers. On my side I am in rails 4.0.0 (ruby 2.0) and I cannot make it work.

I have read a lot of answers on this subject and have tried to understand how mcrypt works, e.g. http://www.chilkatsoft.com/p/php_aes.asp, but without success. I still cannot decrypt the encrypted from PHP or encrypt something that the PHP can decrypt

Could you help me please and see what I am doing wrong?

PHP code:

$secretKey = "1234567891234567";
$encrypt = urlencode( base64_encode( mcrypt_encrypt(
             MCRYPT_RIJNDAEL_128,
             md5($secretKey),
             $cleartext,
             MCRYPT_MODE_CFB,
             $secretKey
           ) ) );

$input = urldecode($input);
$decrypt = mcrypt_decrypt( MCRYPT_RIJNDAEL_128,
                           md5($secretKey),
                           base64_decode($input),
                           MCRYPT_MODE_CFB,
                           $secretKey );

Ruby code:

def self.encode(params = {})
  cipher = OpenSSL::Cipher::AES.new(256, :CFB)
  cipher.encrypt
  cipher.key = Digest::MD5.hexdigest("1234567891234567")
  cipher.iv = "1234567891234567"
  encrypted = cipher.update(params.to_query) + cipher.final

  CGI.escape(Base64.strict_encode64(encrypted))
end

def self.decode(answer)
  decrypted = Base64.decode64(CGI.unescape(answer))

  decipher = OpenSSL::Cipher::AES.new(256, :CFB)
  decipher.decrypt
  decipher.key = Digest::MD5.hexdigest("1234567891234567")
  decipher.iv = "1234567891234567"
  decoded = decipher.update(decrypted) + decipher.final
end
解决方案

You have to use 'ncfb' instead of MCRYPT_MODE_CFB in the PHP code. PHP defaults to an 8 bit feed back instead of a feed back of the full block size.

Alternatively you can specify :CFB8 to be compatible with PHP in Ruby. This one I guessed after reading the documentation for CFB in the OpenSSL documentation.

Many thanks to this Q/A on IT security which I only found because I knew what I was looking for.

这篇关于如何将128 CFB加密到Ruby?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:22