0
我正在嘗試做一個示例應用程序,用於測試目的vs其他人的開發,並且想要在屏幕上打印加密的字符串,並將其放回到解密機制。 ...我似乎沒有找到辦法做到這一點...我已經嘗試過base64並解包,並覺得這是方式,但我沒有到達那裏。使用ruby和openssl加密和解密示例
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
任何人都可以拯救?
謝謝