This commit is contained in:
Yan
2017-11-10 14:11:10 +06:30
parent fb64041a3f
commit a70f9a43e6
5 changed files with 18 additions and 12 deletions

View File

@@ -5,12 +5,12 @@ require 'uri'
class AESEncDec {
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
def export_key
def self.export_key
ENV['aes_key'] = cipher.key = cipher.random_key # stores the key in key, and also sets the generated key on the cipher
ENV['aes_iv'] = cipher.iv = cipher.random_iv # stores the iv in iv, and also sets the generated iv on the cipher
end
def encrypt(data)
def self.encrypt(data)
cipher.encrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
@@ -19,13 +19,13 @@ class AESEncDec {
return encrypted
end
def decrypt
def self.decrypt(data)
cipher.decrypt
cipher.key = ENV["aes_key"]
cipher.iv = ENV["aes_iv"]
# Start the decryption
decoded = Base64.urlsafe_decode64(encrypted)
decoded = Base64.urlsafe_decode64(data)
decrypted = cipher.update(decoded) + cipher.final
end
}