0

私はテスト目的と他の人の開発のためにサンプルアプリを実行しようとしています.暗号化された文字列を画面に出力し、それを復号化メカニズムに戻したいと思います....私はそうではないようです.これを行う方法を見つけています...私はbase64を試して解凍し、これがその方法だと感じましたが、そこに到達していません.

require 'openssl'
require 'base64'

def ask(prompt)
    loop do
        print prompt, ' '
        $stdout.flush
        s = gets
        return s.chomp
    end    
end

def aes(m,k,t)
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k)
aes.update(t) << aes.final
end

def encrypt(key, text)
aes(:encrypt, key, text)
end

def decrypt(key, text)
aes(:decrypt, key, text)
end


def my_decrypt
 @crypted = ask("Crypted data: ") 
 decrypted = decrypt("12345678911131511192123252729412",@crypted) 
 print decrypted
end

def my_encrypt
 @decrypted = ask("Data to encrypt: ") 
 crypted = encrypt("12345678911131511192123252729412",@decrypted) 
 print crypted
end


option=ask("Option 1 - Encrypt, 2 decrypt")
case option
    when "1" then my_encrypt
    when "2" then my_decrypt
    else print "Option not valid"
end

誰か助けて?

ありがとうございました

4

1 に答える 1

0

いくつかの戦いの後、私はついにそれを手に入れました...バイナリを16進数に変換してからバイナリに戻すだけです...

2 つの注意点: バイナリを 16 進数に変換するには、配列を返すString.unpackを使用できます。16 進数を 2 進数に変換するには、まず配列 ["anystringhere"] として構築し、 Array.packを使用してバイナリにパックする必要があります。

これが結果のコードです

require 'openssl'
require 'base64'

def ask(prompt)
  loop do
    print prompt, ' '
    $stdout.flush
    s = gets
    return s.chomp
  end
end

def aes(m,k,t)
  (aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = Digest::SHA256.digest(k)
  aes.update(t) << aes.final
end

def encrypt(key, text)
  aes(:encrypt, key, text)
end

def decrypt(key, text)
  aes(:decrypt, key, text)
end


def my_decrypt
  @crypted = ask("Crypted data: ")
  decrypted = decrypt("12345678911131517192123252729313",[@crypted].pack('H*'))
  print decrypted
end

def my_encrypt
  @decrypted = ask("Data to encrypt: ")
  crypted = encrypt("12345678911131517192123252729313",@decrypted)
  print u=crypted.unpack('H*')
end


option=ask("Option 1 - Encrypt, 2 decrypt")
case option
when "1" then my_encrypt
when "2" then my_decrypt
else print "Option not valid"
end
于 2012-07-20T17:53:38.497 に答える