How can I mcrypt 128 CFB to Ruby?

↘锁芯ラ 提交于 2019-12-05 10:56:21
Maarten Bodewes

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.

MZaragoza

take a look at https://github.com/kingpong/ruby-mcrypt

in your gem file add

gem "ruby-mcrypt", :lib => "mcrypt"

Usage

crypto = Mcrypt.new(:twofish, :cbc, MY_KEY, MY_IV, :pkcs)

# encryption and decryption in one step
ciphertext = crypto.encrypt(plaintext)
plaintext  = crypto.decrypt(ciphertext)

# encrypt in smaller steps
while chunk = $stdin.read(4096)
  $stdout << crypto.encrypt_more(chunk)
end
$stdout << crypto.encrypt_finish

# or decrypt:
while chunk = $stdin.read(4096)
  $stdout << crypto.decrypt_more(chunk)
end
$stdout << crypto.decrypt_finish

you can also check out https://stackoverflow.com/a/21489711/1380867

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!