Guest User

Ruby AES Crypt

a guest
Apr 17th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.49 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2.  
  3. # The encrypt_data and decrypt_data methods are Copyright (c) 2007 Brent Sowers
  4. # and have been included with prior permission.
  5. #
  6. # Copyright (c) 2012 Gurpartap Singh
  7. #
  8. # MIT License
  9. #
  10. # Permission is hereby granted, free of charge, to any person obtaining
  11. # a copy of this software and associated documentation files (the
  12. # "Software"), to deal in the Software without restriction, including
  13. # without limitation the rights to use, copy, modify, merge, publish,
  14. # distribute, sublicense, and/or sell copies of the Software, and to
  15. # permit persons to whom the Software is furnished to do so, subject to
  16. # the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be
  19. # included in all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28.  
  29. require 'openssl'
  30. require 'base64'
  31.  
  32. module AESCrypt
  33.   def self.encrypt(message, password, use_iv = false)
  34.     Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-256-CBC", use_iv))
  35.   end
  36.  
  37.   def self.decrypt(message, password, use_iv = false)
  38.     base64_decoded = Base64.decode64(message.to_s.strip)
  39.     self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-256-CBC", use_iv)
  40.   end
  41.  
  42.   def self.key_digest(password)
  43.     OpenSSL::Digest::SHA256.new(password).digest
  44.   end
  45.  
  46.   # Decrypts a block of data (encrypted_data) given an encryption key
  47.   # and an initialization vector (iv).  Keys, iv's, and the data
  48.   # returned are all binary strings.  Cipher_type should be
  49.   # "AES-256-CBC", "AES-256-ECB", or any of the cipher types
  50.   # supported by OpenSSL.  Pass nil for the iv if the encryption type
  51.   # doesn't use iv's (like ECB).
  52.   #:return: => String
  53.   #:arg: encrypted_data => String
  54.   #:arg: key => String
  55.   #:arg: iv => String
  56.   #:arg: cipher_type => String
  57.   def self.decrypt_data(encrypted_data, key, iv, cipher_type, use_iv)
  58.     aes = OpenSSL::Cipher::Cipher.new(cipher_type)
  59.     aes.decrypt
  60.     aes.key = key
  61.    
  62.     if use_iv
  63.       if iv.nil?
  64.         iv = encrypted_data[0..15]
  65.         encrypted_data = encrypted_data[16..-1]
  66.       end
  67.       aes.iv = iv
  68.     end
  69.  
  70.     aes.update(encrypted_data) + aes.final  
  71.   end
  72.  
  73.   # Encrypts a block of data given an encryption key and an
  74.   # initialization vector (iv).  Keys, iv's, and the data returned
  75.   # are all binary strings.  Cipher_type should be "AES-256-CBC",
  76.   # "AES-256-ECB", or any of the cipher types supported by OpenSSL.  
  77.   # Pass nil for the iv if the encryption type doesn't use iv's (like
  78.   # ECB).
  79.   #:return: => String
  80.   #:arg: data => String
  81.   #:arg: key => String
  82.   #:arg: iv => String
  83.   #:arg: cipher_type => String  
  84.   def self.encrypt_data(data, key, iv, cipher_type, use_iv)
  85.     aes = OpenSSL::Cipher::Cipher.new(cipher_type)
  86.     aes.encrypt
  87.     aes.key = key
  88.    
  89.     if use_iv
  90.       if iv.nil?
  91.         iv = aes.random_iv
  92.       end
  93.       aes.iv = iv
  94.       iv + aes.update(data) + aes.final
  95.     else
  96.       aes.update(data) + aes.final
  97.     end
  98.    
  99.   end
  100. end
Add Comment
Please, Sign In to add comment