나는 어떻게 변환하는 복어 인코딩된 바이너리 문자열을 ASCII 루비에서?
-
03-07-2019 - |
문제
나는 것 같이 인코딩하는 몇 가지 일반 텍스트를 사용하여 루비 및 Crypt 라이브러리.나는 다음을 전송하는 이를 암호화(텍스트와 함께 다른 어떤 데이터)를 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)
또한 이 블로그 게시물에 대해 이야기 속도 관심사를 사용하 Crypt 라이브러리,추가 단지 참조용입니다.:)
http://basic70tech.wordpress.com/2007/03/09/blowfish-decryption-in-ruby/
제휴하지 않습니다 StackOverflow