/Ruby-Encryptor

Encryptor module for Ruby

Primary LanguageRuby

Ruby Encryptor

Encryptor is intended as an easy data encryptor using public/private RSA key and AES-256.

Details

Encryptor uses AES-256 to encrypt the data using a Random Key and IV. The key and IV is encrypted with the public RSA key and stored inside the encrypted data (at the beginning)

The encrypted data is longer than the original by 512 bytes (+ extra 0-16 bytes)

Why not encode the whole data with the RSA Key ?

RSA Encryption is degigned for short messages (perfect for AES-256 Key) but can’t encrypt long message. It would be incredibly too slow if it was possible.

Quick Start

To encrypt original.txt

In your script (replace the keys by yours):

require 'encryptor'
public_key     = File.read('test.pub')
data           = 'Ceci est un message secret'
# Encrypt data with :
encrypted = Encryptor.encrypt_data(data, public_key)
# Save it to be decrypted later :
File.open('encrypted,'wb') {|f| f.write(encrypted)}

To decrypt

In your script (replace the keys and the password by yours):

require 'encryptor'
encrypted     = File.read('encrypted')
private_key   = File.read('test.pem')
password      = 'test'
# Decrypt with :
decrypted = Encryptor.decrypt_data(encrypted, private_key, password)
puts decrypted
# Should display : 'Ceci est un message secret'

Prepare RSA keys

Generate test.pem (private) and test.pub (public)

Under linux shell :

>openssl genrsa -des3 -out test.pem 2048

Generating RSA private key, 2048 bit long modulus
.....................................+++
..............................................................................+++
e is 65537 (0x10001)
Enter pass phrase for test.pem: <type your password>
Verifying - Enter pass phrase for test.pem: <type your password again>

>openssl rsa -in test.pem -out test.pub -outform PEM -pubout

Enter pass phrase for test.pem: <type your password>
writing RSA key