-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathende.rb
44 lines (37 loc) · 897 Bytes
/
ende.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
# Amit Pandya
require "base64"
require "openssl"
@endekey = '124jkhjsahdffhajhfjdsakhfjkh234h'
def encrypt(string)
Base64.encode64(enstr(string)).gsub /\s/, ''
end
def decrypt(string)
destr(Base64.decode64(string))
end
def cipher_val(endekeyval)
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
if endekeyval == "en"
cipher.encrypt
else
cipher.decrypt
end
cipher.key = Digest::SHA256.digest(@endekey)
return cipher
end
def enstr(string)
cipher = cipher_val("en")
cipher.iv = initialization_vector = cipher.random_iv
cipher_text = cipher.update(string)
cipher_text << cipher.final
return initialization_vector + cipher_text
end
def destr(encrypted)
cipher = cipher_val("de")
cipher.iv = encrypted.slice!(0,16)
de = cipher.update(encrypted)
de << cipher.final
end
$msg = "hi there"
$enc_string = encrypt($msg)
print $enc_string,"\n"
print decrypt($enc_string), "\n"