Guest User

Untitled

a guest
Apr 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. def encrypt(string, key)
  2. Base64.encode64(aes(key, string)).gsub /\s/, ''
  3. end
  4.  
  5. def decrypt(string, key)
  6. aes_decrypt(key, Base64.decode64(string))
  7. end
  8.  
  9. def aes(key,string)
  10. cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
  11. cipher.encrypt
  12. cipher.key = Digest::SHA256.digest(key)
  13. cipher.iv = initialization_vector = cipher.random_iv
  14. cipher_text = cipher.update(string)
  15. cipher_text << cipher.final
  16. return initialization_vector + cipher_text
  17. end
  18.  
  19. def aes_decrypt(key, encrypted)
  20. cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
  21. cipher.decrypt
  22. cipher.key = Digest::SHA256.digest(key)
  23. cipher.iv = encrypted.slice!(0,16)
  24. d = cipher.update(encrypted)
  25. d << cipher.final
  26. end
Add Comment
Please, Sign In to add comment