Permalink
Browse files

Add the helper methods for quick enc/dec.

  • Loading branch information...
0 parents commit 6366bb7c9af2c4d310e8ccce44c1fd37f01493af @Gurpartap committed Jul 31, 2012
Showing with 224 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +6 −0 Gemfile
  3. +22 −0 LICENSE
  4. +79 −0 README.md
  5. +2 −0 Rakefile
  6. +16 −0 aescrypt.gemspec
  7. +82 −0 lib/aescrypt.rb
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,6 @@
+# -*- encoding: utf-8 -*-
+
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in aescrypt.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Gurpartap Singh
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
+# AESCrypt
+
+A simple and opinionated AES encrypt / decrypt Ruby gem that just works.
+
+AESCrypt uses the AES-256-CBC cipher and encodes the encrypted data with base64.
+
+A corresponding gem to easily handle AES encrypt/decrypt in Objective-C is available at http://github.com/Gurpartap/AESCrypt-ObjC.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'aescrypt'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install aescrypt
+
+## Usage
+
+ message = "top secret message"
+ password = "p4ssw0rd"
+
+Encrypting
+
+ encrypted_data = AESCrypt.encrypt(message, password)
+
+Decrypting
+
+ message = AESCrypt.decrypt(encrypted_data, password)
+
+## Advanced usage
+
+Encrypting
+
+ encrypted_data = encrypt_data(data, key, iv, cipher_type)
+
+Decrypting
+
+ decrypted_data = decrypt_data(encrypted_data, key, iv, cipher_type)
+
+## Corresponding usage in Objective-C
+
+The AESCrypt Objective-C class 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.
+
+Here's how you would use the AESCrypt Objective-C class:
+
+ NSString *message = "top secret message";
+ NSString *password = @"p4ssw0rd";
+
+Encrypting
+
+ NSString *encryptedData = [AESCrypt encrypt:message password:password];
+
+Decrypting
+
+ NSString *message = [AESCrypt decrypt:encryptedData password:password];
+
+See http://github.com/Gurpartap/AESCrypt-ObjC for more details.
+
+## License
+
+The encrypt_data and decrypt_data methods are Copyright (c) 2007 Brent Sowers and have been included in the gem with prior permission.
+
+Copyright (c) 2012 Gurpartap Singh
+
+See LICENSE for license terms.
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Gurpartap Singh"]
+ gem.email = ["contact@gurpartap.com"]
+ gem.description = "Simple Ruby AES encryption / decryption gem"
+ gem.summary = "A simple and opinionated AES encrypt / decrypt Ruby gem that just works."
+ gem.homepage = "http://github.com/Gurpartap/aescrypt"
+
+ gem.files = `git ls-files`.split("\n")
+ gem.name = "aescrypt"
+ gem.require_paths = ["lib"]
+ gem.version = "1.0.0"
+
+ gem.add_dependency "rake"
+end
@@ -0,0 +1,82 @@
+# -*- encoding: utf-8 -*-
+
+# The encrypt_data and decrypt_data methods are Copyright (c) 2007 Brent Sowers
+# and have been included with prior permission.
+#
+# Copyright (c) 2012 Gurpartap Singh
+#
+# MIT License
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+require 'openssl'
+
+module AESCrypt
+ def encrypt(message, password)
+ Base64.encode64(encrypt_data(message.strip, key_digest(password), nil, "AES-256-CBC"))
+ end
+
+ def decrypt(message, password)
+ base64_decoded = Base64.decode64(message.strip)
+ decrypt_data(base64_decoded, key_digest(password), nil, "AES-256-CBC")
+ end
+
+ def key_digest(password)
+ OpenSSL::Digest::SHA256.new(password).digest
+ end
+
+ # Decrypts a block of data (encrypted_data) given an encryption key
+ # and an initialization vector (iv). Keys, iv's, and the data
+ # returned are all binary strings. Cipher_type should be
+ # "AES-256-CBC", "AES-256-ECB", or any of the cipher types
+ # supported by OpenSSL. Pass nil for the iv if the encryption type
+ # doesn't use iv's (like ECB).
+ #:return: => String
+ #:arg: encrypted_data => String
+ #:arg: key => String
+ #:arg: iv => String
+ #:arg: cipher_type => String
+ def decrypt_data(encrypted_data, key, iv, cipher_type)
+ aes = OpenSSL::Cipher::Cipher.new(cipher_type)
+ aes.decrypt
+ aes.key = key
+ aes.iv = iv if iv != nil
+ aes.update(encrypted_data) + aes.final
+ end
+
+ # Encrypts a block of data given an encryption key and an
+ # initialization vector (iv). Keys, iv's, and the data returned
+ # are all binary strings. Cipher_type should be "AES-256-CBC",
+ # "AES-256-ECB", or any of the cipher types supported by OpenSSL.
+ # Pass nil for the iv if the encryption type doesn't use iv's (like
+ # ECB).
+ #:return: => String
+ #:arg: data => String
+ #:arg: key => String
+ #:arg: iv => String
+ #:arg: cipher_type => String
+ def encrypt_data(data, key, iv, cipher_type)
+ aes = OpenSSL::Cipher::Cipher.new(cipher_type)
+ aes.encrypt
+ aes.key = key
+ aes.iv = iv if iv != nil
+ aes.update(data) + aes.final
+ end
+end

0 comments on commit 6366bb7

Please sign in to comment.