-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_aes.rb
61 lines (53 loc) · 1.29 KB
/
simple_aes.rb
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require "openssl"
require "digest/sha1"
require "base64"
=begin
Author: Gustavo Anatoly F. V. Solís
Demonstration how to use AES to
encrypt and decrypt.
=end
module SimpleAES
IV = "1234567890ABCDFEFGHIJ"
ALGO = "aes-128-cbc"
DEFAULT_KEY_SIZE = 16
class AES
def encrypt(content, key)
aes = OpenSSL::Cipher::Cipher.new(ALGO)
aes.encrypt
aes.key = fix_key_size(key)
aes.iv = IV
data = aes.update(content)
data << aes.final
return Base64.encode64(data)
end
def decrypt(content, key)
aes = OpenSSL::Cipher::Cipher.new(ALGO)
aes.decrypt
aes.key = fix_key_size(key)
aes.padding = 0
aes.iv = IV
decode = Base64.decode64(content)
data = aes.update(decode)
data << aes.final
return data
end
private
def fix_key_size(key)
complement = 0
fixed_key = key
if key.size < DEFAULT_KEY_SIZE then
complement = DEFAULT_KEY_SIZE - key.size
for i in (1..complement)
fixed_key += "0"
end
end
return fixed_key
end
end
end
aes = SimpleAES::AES.new
encrypt = aes.encrypt("Test AES", "my password")
decrypt = aes.decrypt(encrypt, "my password")
puts encrypt
puts "\n\n============================\n\n"
puts decrypt