كيف يمكنني تحويل السلسلة الثنائية السمكة المنتفخة المشفرة إلى ASCII في روبي؟
-
03-07-2019 - |
سؤال
وأود أن ترميز بعض نص عادي باستخدام روبي و القبو مكتبة . أود ثم لنقل هذا النص المشفر (جنبا إلى جنب مع غيرها من البيانات)، وسلسلة ASCII عشري في ملف XML.
ولدي التعليمات البرمجية المتكررة التالية:
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()
ولكن لست متأكدا ما المعلمات المطلوبة لاستدعاء الأسلوب فك.
المحلول
وعندما تقول "عشري ASCII" هل يعني أنه يحتاج فقط إلى أن تكون ASCII قراءة أو أنها لا تحتاج إلى أن تكون عشري بدقة؟
إليك طريقتان لترميز البيانات الثنائية:
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=
نصائح أخرى
إذا كنت تستخدم decrypt_string
ونظيرتها encrypt_string
ذلك النواتج ذلك بسهولة جدا. :)
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)
p blowfish.decrypt_string(enc)
وكما وجدت هذه مشاركة المدونة التي تتحدث عن اهتمامات سرعة باستخدام مكتبة القبو، واضاف عادل للرجوع إليها. :)
<لأ href = "HTTP: // basic70tech.wordpress.com/2007/03/09/blowfish-decryption-in-ruby/ "يختلط =" نوفولو noreferrer "> http://basic70tech.wordpress.com/2007/03/09/blowfish-decryption-in- روبي /