我正在尝试做一个示例应用程序,用于测试目的与其他人的开发,并希望在屏幕上打印加密的字符串,并将其放回到解密机制....我只是不似乎找到了这样做的方法......我已经尝试过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
有人救援吗?
谢谢
答案 0 :(得分:0)
经过一番战斗,我终于明白了...我只需要将二进制转换为Hex,然后再转换为二进制......
两个注意事项:要将二进制文件转换为十六进制,可以使用String.unpack,它将返回一个数组。 要将十六进制转换为二进制,首先需要将其构建为数组[“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