Module: Crypt
- Defined in:
- lib/crypt/blowfish-tables.rb,
lib/crypt/cbc.rb,
lib/crypt/gost.rb,
lib/crypt/idea.rb,
lib/crypt/noise.rb,
lib/crypt/blowfish.rb,
lib/crypt/rijndael.rb,
lib/crypt/rijndael-tables.rb
Overview
The Crypt library is a pure-ruby implementation of a number of popular encryption algorithms. Block cyphers currently available include Blowfish, GOST, IDEA, and Rijndael (AES). Cypher Block Chaining (CBC) has been implemented.
Crypt is written entirely in ruby so deployment is simple - no platform concerns, no library dependencies, nothing to compile.
Performance is what you would expect from a pure-ruby implementation: it is adequate for shorter messages but I wouldn’t try to encrypt my whole disk with it. If performance is critical then you might want to try ezcrypto’s (ezcrypto.rubyforge.org) interface to OpenSSL.
v.2.2.0 released 23 Sep 2015 fixes a defect in IDEA and replaces tests with specs.
require 'crypt/blowfish'
blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
plainBlock = "ABCD1234"
encryptedBlock = blowfish.encrypt_block(plainBlock)
decryptedBlock = blowfish.decrypt_block(encryptedBlock)
string = “This is a string which is not a multiple of 8 characters long”
encryptedString = bf.encrypt_string(string)
decryptedString = bf.decrypt_string(encryptedString)
bf.encrypt_file(‘plain.txt’, ‘crypt.txt’)
bf.decrypt_file('crypt.txt', 'decrypt.txt')
require 'crypt/gost'
# a key shorter than 32 bytes will be padded out with zeroes
gost = Crypt::Gost.new("This is a contrived 32 byte key!")
plainBlock = "ABCD1234"
encryptedBlock = gost.encrypt_block(plainBlock)
decryptedBlock = gost.decrypt_block(encryptedBlock)
require 'crypt/idea'
idea = Crypt::IDEA.new("A key up to 56 bytes long")
plainBlock = "ABCD1234"
encryptedBlock = idea.encrypt_block(plainBlock)
decryptedBlock = idea.decrypt_block(encryptedBlock)
require 'crypt/rijndael'
rijndael = Crypt::Rijndael.new("A key 16, 24, or 32 bytes length")
plainBlock = "ABCDEFGH12345678"
encryptedBlock = rijndael.encrypt_block(plainBlock)
decryptedBlock = rijndael.decrypt_block(encryptedBlock)
Cipher-block chaining (CBC) is supported for each algorithm:
blowfish = Crypt::Blowfish.new("A sample key better than this one")
blowfish.encrypt_file('plain.txt', 'crypt.txt')
blowfish.decrypt_file('crypt.txt', 'plain.txt')
You can also read from a plain text stream, encrypt and write to an encrypted output stream, or treat a string as a stream:
f = File.new('plain.txt', 'r')
g = TCPSocket.new('encrypted.example.com', 2222)
blowfish.encrypt_stream(f, g)
plainText = "This is plain text."
encrypted = blowfish.encrypt_string(plainText)
Defined Under Namespace
Modules: BlowfishTables, CBC, Noise, RijndaelTables Classes: Blowfish, Gost, IDEA, Rijndael