Skip to content
/ aes Public
forked from chicks/aes

AES.encrypt and AES.decrypt for Ruby

License

Notifications You must be signed in to change notification settings

drewblas/aes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aes

An AES encrypt/decrypt gem built on top of OpenSSL. Not as quick as FastAES but doesn’t require building native extensions - also supports Base64 encoded input and output.

Usage:

require 'aes'

# Generate a random key
key = AES.key
 => "290c3c5d812a4ba7ce33adf09598a462"

# Encrypt a string.  Default output is base_64 encoded, init_vector and cipher_text are joined with "$"
b64 = AES.encrypt("A super secret message", key)
 => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="

# Same as above but minus the base64 encoding, init_vector and cipher_text are shoved into an array
plain = AES.encrypt("A super secret message", key, {:format => :plain}) #
 => [";\202\222\306\376<\206\343\023\245\312\225\214KAm", 
     "C\343\023\323U~W>\023y\217\341\201\371\352\334\311^\307\352{\020 H(DVw\3224N\223"]

# Generate a random initialization vector
iv = AES.iv(:base_64)
 => "IJjbgbv/OvPIAf4R5qAWyg=="

# Encrypt a string, with a provided key and init_vector.  
b64_iv = AES.encrypt("A super secret message", key, {:iv => iv})
 => "IJjbgbv/OvPIAf4R5qAWyg==$fy0v7JwRX4kyAWflgouQlt9XGmiDKvbQMRHmQ+vy1fA="

AES.decrypt(b64, key)
 => "A super secret message"

AES.decrypt(plain, key, {:format => :plain})
 => "A super secret message" 

# By default data is padded to the nearest 16 bytes block.  To turn
# this off, you may use the :padding => false (or nil) option.
#
# In this mode however, the caller is required to pad the data.  In
# the following example the message is exactly 16 bytes long, so no
# error aries.
msg = AES.encrypt("A secret message", key, {:padding => false})
 => "SnD+WIfEfjZRrl+WAM/9pw==$89sGGZsu973j8Gl6aXC8Uw=="

# Be sure to pass the same padding option when decrypting the
# message, as it will fail if you try to decrypt unpadded data and
# didn't specify :padding => false.
AES.decrypt(msg, key, {:padding => false})
 => "A secret message"

Contributing to aes

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 Carl Hicks. See LICENSE.txt for further details.

About

AES.encrypt and AES.decrypt for Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 100.0%