在Ruby中如何将Blowfish编码的二进制字符串转换为ASCII? [英] How can I convert a Blowfish encoded binary string to ASCII in Ruby?

查看:214
本文介绍了在Ruby中如何将Blowfish编码的二进制字符串转换为ASCII?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ruby和 Crypt library 对一些纯文本进行编码。然后,我想将此加密文本(以及一些其他数据)作为ASCII文件中的ASCII十六进制字符串传输。

I would like to encode some plain text using Ruby and the Crypt library. I would like to then transmit this encrypted text (along with some other data) as an ASCII hexadecimal string within an XML file.

我有以下代码片段:

require 'rubygems'
require 'crypt/blowfish'

plain = "This is the plain text"
puts plain

blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
enc = blowfish.encrypt_block(plain)
puts enc

哪些输出:

This is the plain text
????;

我相信我需要调用 enc.unpack(),但我不知道解包方法调用需要什么参数。

I believe I need to call enc.unpack() but I'm not sure what parameters are required to the unpack method call.

推荐答案

当你说ASCII十六进制你的意思是它只需要可读ASCII或需要严格十六进制?

When you say "ASCII hexadecimal" do you mean that it merely needs to be readable ASCII or does it need to be strictly hexadecimal?

这是编码二进制数据的两种方法:

Here's two approaches to encoding binary data:

require 'rubygems'
require 'crypt/blowfish'

plain = "This is the plain text"
puts plain

blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
enc = blowfish.encrypt_string(plain)

hexed = ''
enc.each_byte { |c| hexed << '%02x' % c }

puts hexed
# => 9162f6c33729edd44f5d034fb933ec38e774460ccbcf4d451abf4a8ead32b32a

require 'base64'

mimed = Base64.encode64(enc)

puts mimed
# => kWL2wzcp7dRPXQNPuTPsOOd0RgzLz01FGr9Kjq0ysyo=

这篇关于在Ruby中如何将Blowfish编码的二进制字符串转换为ASCII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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