1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
def aes_encrypt(clear_text, key, padding=1)
cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
cipher.encrypt
cipher.padding = padding
cipher.key = key
cipher.iv = "\x00" * cipher.iv_len
edata = cipher.update(clear_text)
edata << cipher.final
return edata
end
def aes_decrypt(enc_text, key, padding=1)
decipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
decipher.decrypt
decipher.padding = padding
decipher.key = key
decipher.iv = "\x00" * decipher.iv_len
data = decipher.update(enc_text)
data << decipher.final
return data
end
def self.normalize(content)
newlen = content.length - (content.length % 16)
has_rand_block = newlen != content.length
content = content[0..(newlen -1)]
return content, has_rand_block
end
...
resp = Base64.strict_decode64(content)
# align to the multiple of 16
resp, has_rand = normalize(resp)
# decrypt the message
message = aes_decrypt(resp, key, 0) |
Partager