├── .gitignore ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── aescrypt.gemspec └── lib └── aescrypt.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | source 'https://rubygems.org' 4 | 5 | # Specify your gem's dependencies in aescrypt.gemspec 6 | gemspec 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Gurpartap Singh 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AESCrypt - Simple AES encryption / decryption for Ruby 2 | 3 | AESCrypt is a simple to use, opinionated AES encryption / decryption Ruby gem that just works. 4 | 5 | AESCrypt uses the AES-256-CBC cipher and encodes the encrypted data with Base64. 6 | 7 | A corresponding gem to easily handle AES encryption / decryption in Objective-C is available at http://github.com/Gurpartap/AESCrypt-ObjC. 8 | 9 | ## Installation 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | gem 'aescrypt' 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | Or install it yourself as: 20 | 21 | $ gem install aescrypt 22 | 23 | ## Usage 24 | 25 | message = "top secret message" 26 | password = "p4ssw0rd" 27 | 28 | Encrypting 29 | 30 | encrypted_data = AESCrypt.encrypt(message, password) 31 | 32 | Decrypting 33 | 34 | message = AESCrypt.decrypt(encrypted_data, password) 35 | 36 | ## Advanced usage 37 | 38 | Encrypting 39 | 40 | encrypted_data = encrypt_data(data, key, iv, cipher_type) 41 | 42 | Decrypting 43 | 44 | decrypted_data = decrypt_data(encrypted_data, key, iv, cipher_type) 45 | 46 | ## Corresponding usage in Objective-C 47 | 48 | The AESCrypt Objective-C class, available at https://github.com/Gurpartap/AESCrypt-ObjC, understands what you're talking about in your Ruby code. The purpose of the Ruby gem and Objective-C class is to have something that works out of the box across the server (Ruby) and client (Objective-C). However, a standard encryption technique is implemented, which ensures that you can handle the data with any AES compatible library available across the web. So, you're not locked-in. 49 | 50 | Here's how you would use the AESCrypt Objective-C class: 51 | 52 | NSString *message = @"top secret message"; 53 | NSString *password = @"p4ssw0rd"; 54 | 55 | Encrypting 56 | 57 | NSString *encryptedData = [AESCrypt encrypt:message password:password]; 58 | 59 | Decrypting 60 | 61 | NSString *message = [AESCrypt decrypt:encryptedData password:password]; 62 | 63 | See the Objective-C class README at http://github.com/Gurpartap/AESCrypt-ObjC for more details. 64 | 65 | ## License 66 | 67 | Copyright (c) 2012 Gurpartap Singh 68 | 69 | The encrypt_data and decrypt_data methods are Copyright (c) 2007 Brent Sowers and have been included in the gem with prior permission. Thanks Brent! :) 70 | 71 | See LICENSE for license terms. 72 | 73 | ## Contributing 74 | 75 | 1. Fork it 76 | 2. Create your feature branch (`git checkout -b my-new-feature`) 77 | 3. Commit your changes (`git commit -am 'Added some feature'`) 78 | 4. Push to the branch (`git push origin my-new-feature`) 79 | 5. Create new Pull Request 80 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require "bundler/gem_tasks" 3 | -------------------------------------------------------------------------------- /aescrypt.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | Gem::Specification.new do |gem| 4 | gem.authors = ["Gurpartap Singh"] 5 | gem.email = ["contact@gurpartap.com"] 6 | gem.description = "Simple AES encryption / decryption for Ruby" 7 | gem.summary = "AESCrypt is a simple to use, opinionated AES encryption / decryption Ruby gem that just works." 8 | gem.homepage = "http://github.com/Gurpartap/aescrypt" 9 | 10 | gem.files = `git ls-files`.split("\n") 11 | gem.name = "aescrypt" 12 | gem.require_paths = ["lib"] 13 | gem.version = "1.0.1" 14 | 15 | gem.add_development_dependency "rake" 16 | end 17 | -------------------------------------------------------------------------------- /lib/aescrypt.rb: -------------------------------------------------------------------------------- 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) 34 | Base64.encode64(self.encrypt_data(message.to_s.strip, self.key_digest(password), nil, "AES-256-CBC")) 35 | end 36 | 37 | def self.decrypt(message, password) 38 | base64_decoded = Base64.decode64(message.to_s.strip) 39 | self.decrypt_data(base64_decoded, self.key_digest(password), nil, "AES-256-CBC") 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) 58 | aes = OpenSSL::Cipher::Cipher.new(cipher_type) 59 | aes.decrypt 60 | aes.key = key 61 | aes.iv = iv if iv != nil 62 | aes.update(encrypted_data) + aes.final 63 | end 64 | 65 | # Encrypts a block of data given an encryption key and an 66 | # initialization vector (iv). Keys, iv's, and the data returned 67 | # are all binary strings. Cipher_type should be "AES-256-CBC", 68 | # "AES-256-ECB", or any of the cipher types supported by OpenSSL. 69 | # Pass nil for the iv if the encryption type doesn't use iv's (like 70 | # ECB). 71 | #:return: => String 72 | #:arg: data => String 73 | #:arg: key => String 74 | #:arg: iv => String 75 | #:arg: cipher_type => String 76 | def self.encrypt_data(data, key, iv, cipher_type) 77 | aes = OpenSSL::Cipher::Cipher.new(cipher_type) 78 | aes.encrypt 79 | aes.key = key 80 | aes.iv = iv if iv != nil 81 | aes.update(data) + aes.final 82 | end 83 | end 84 | --------------------------------------------------------------------------------