EnigmaEngine Coverage Status Circle CI

This is a ruby gem that simulates the Enigma Machine. The Enigma Machine is a cipher mechanism created by Arthur Scherbius a German Engineer. The machine is capable of transcribing text into coded information as a means of encryption. As of its time during World War II, it was the most sophisticated means of encryption. During the war, both the Allies and the Axis countries were looking for a new way to encrypt messages - a way that would result in complete security this was when the Enigma was born. By the end of World War II Alan Turing with the help of is friends where able to crack the enigma which helped Britain and their allies during the war. More on the Enigma.

How the encryption works

The inner working of an enigma machine has three rotors, each rotors has wrapped around it the 26 letters. When a key is pressed it moves the rotor ahead one alphabet. The other rotors kick off after the first has completed a revolution and so for the third. When this is done, text in plain english becomes gibberish. Before you start encrypting you and your recipient must agree upon a key which can be set on the machine. Find out more about the enigma machine by watching this video.

How the Enigma Machine works

Though, my implementation of the Enigma Machine is slightly different from that of World War II but here is the catch

The rotors: they are denoted by five digit numbers e.g. 97239

  • The first two digits of the key is the "A" rotation (97)
  • The second and third digits are the "B" rotation (72)
  • The third and fourth digits are the "C" rotation (23)
  • The fourth and firth digits are the "D" rotation (39)

The offsets: the date of message transmission is also factored into the encryption

  • Date is in the format DDMMYY e.g. 020315
  • Square the numeric form you get 412699225 pick the last four digits
  • The first digit is the "A" offset (9)
  • The second digit is the "B" offset (2)
  • The third digit is the "C" offset (2)
  • The fourth digit is the "D" offset (5)

Encrypting a message

  • Four characters are encrypted at a time
  • The first character is rotated forward by the "A" rotation plus the "A offset"
  • The second character is rotated forward by the "B" rotation plus the "B offset"
  • The third character is rotated forward by the "C" rotation plus the "C offset"
  • The fourth character is rotated forward by the "D" rotation plus the "D offset"

Decrypting a message

The offsets and keys can be calculated by the same methods above. Then each character is rotated backwards instead of forwards.

Cracking a message When the key is not known, the offsets can still be calculated from the message date. We believe that each enemy message ends with the characters "..end..". Use that to determine when you’ve correctly guessed the key.

Installation

Add this line to your application's Gemfile:

gem 'enigma_engine'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enigma_engine

Usage

The tool is used from the command line like so:

View Help

enigma help To see help file

Encrypting a message

enigma encrypt [message.txt] [encrypted.txt] created encrypted.txt with key 82648 and date 090216

That will take the plaintext file message.txt and create an encrypted file encrypted.txt.

Decrypting a message Then, if we know the key, we can decrypt like so:

enigma decrypt [encrypted.txt] [decrypted.txt] [82648] [030415] created ‘decrypted.txt with key 82648 and date 030415

Cracking a message But if we don’t know the key, we can try to crack it with just the date like so:

enigma crack [decrypted.txt] [cracked.txt] [030415] created ‘cracked.txt with key 82648 and date 030415

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/andela-sdamian/EnigmaEngine. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.