0.01
No release in over 3 years
Low commit activity in last 3 years
Simple encryption and decryption with asymmetric (or public-key) cryptography.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

>= 0
 Project Readme

Gem Version

Cryptosystem

Cryptosystem is a Ruby library facilitating simple encryption and decryption with asymmetric cryptography (or public-key cryptography). At this time, only RSA is supported.

Installation

In your Gemfile, include the cryptosystem gem and then bundle install.

gem 'cryptosystem'

Getting Started

In order to encrypt and decrypt, a public and private key must be generated.

$ openssl genrsa -out private.key
$ openssl rsa -in private.key -pubout > public.pub

Configuration

Cryptosystem must know the path and password to your private key as well as the path to your public key.

# config/initializers/cryptosystem.rb

Cryptosystem::RSA.configure do |config|
  config.password = ENV['secret-password']
  config.private_key_path = 'path/to/private.key'
  config.public_key_path = 'path/to/public.pub'
end

Configuration options may also be passed in or overridden when instantiating a new object.

config = {
  password: ENV['secret-password'],
  private_key_path: 'path/to/private.key',
  public_key_path: 'path/to/public.pub'
}

rsa = Cryptosystem::RSA.new(config)

Encrypting

After generating a key pair and properly configuring Cryptosystem, encryption is straightforward.

rsa = Cryptosystem::RSA.new
rsa.encrypt('secret') # => "JxpuhTpEqRtMLmaSfaq/X6XONkBnMe..."

The encrypted value is Base64 encoded, making it suitable for database storage.

Decrypting

Decrypting is as simple as passing in an encrypted, Base64 encoded value.

rsa = Cryptosystem::RSA.new
encrypted_value = rsa.encrypt('secret') # => "Y8DWJc2/+7TIxdLEolV99XI2sclHuK..."
rsa.decrypt(encrypted_value) # => "secret"