Git Product home page Git Product logo

ultra's Introduction

ultra Build Status crates.io docs.rs License

ultra is a Rust implementation of the Enigma machine that includes the ability to decrypt ciphertext.

Installation

ultra can be installed from crates.io using Cargo:

$ cargo install ultra

Usage

Encrypt a message with rotors 1-4-2, key setting D-O-G, and ring setting C-A-T:

$ ultra --rotor=142 --key=DOG --ring=CAT "The quick brown fox jumps over the lazy dog."
Ntz ntqlz jmwll art bbnow wzqk keq ievk lzo.

Encrypt a message using random Enigma settings:

$ ultra --randomize "The quick brown fox jumps over the lazy dog."
Kxj mcwzf oqgmz pwr vnfqq iwhv wcr qqgt lgd.
> Rotors: 5-2-3 / Key: A-A-G / Ring: N-W-Q / Plugs: CG EZ HW IJ MP TY

Attempt to decrypt a piece of ciphertext:

$ ultra --decrypt "$(cat ciphertext.txt)"
...

Decryption relies on a combination of index of coincidence, bigram, and quadgram frequencies to infer the original Enigma machine settings, and as a result, it is quite likely that messages shorter than 500 characters will not come anywhere close to being decrypted correctly.

References

The original version of this project was based on James Lyons' articles about the Enigma machine (see this blog post for a brief overview). As of version 0.6.0, the decryption algorithm was updated, inspired by this Computerphile video.

License

ultra is licensed under the MIT License.

ultra's People

Contributors

ikeviny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ultra's Issues

Experiment with various fitness functions

Another task to do on stream? ๐Ÿ‘€

  • Refactor CLI to take fitness function at each of the 3 stages of decryption.
    • Maybe a separate binary? Since we'll want to output fitness data at various stages.
  • Set up test runner to randomize a bunch of texts and store rotor/key/ring value.
  • Plot performance over each combination of fitness functions.

Implement decryption of plugboard-encrypted messages

Feeling inspired to write some more cryptanalysis code in Rust after seeing this Computerphile video and finishing reading The Code Book. This is a rough list of TODOs to organize the process, since I might feel like coding this on stream...

  • Update project to build on latest stable rust (bump dependencies).
  • Implement Plugboard::unplugged to get currently unused plugboard characters.
  • Add ability for plugboard randomization via --randomize.
  • Find import bigram/trigram data, similar to existing quadgram data.
  • Create Fitness trait.
  • Implement bigram and trigram fitness functions.
  • Refactor quadgram code into fitness module.
  • Implement index of coincidence fitness function.
  • Rewrite main decryption loop (see https://github.com/mikepound/enigma/blob/main/src/com/mikepound/Main.java)
    • Uses IoC for rotor config, bigrams for ring settings, and quadgrams for plugboard breaking.
    • Should probably implement as a new method and a new CLI flag if it will be a lot slower.
  • Experiment with trying different fitness functions from the above, possibly in parallel.
    • Come up with some weighted "overall fitness" function that accumulates the results of all existing ones.
  • Improve decryption ergonomics to re-insert original whitespace & punctuation.
  • Implement Display trait on Enigma (will want debug messages for printing the top N candidates at each step).
    • Something like Rotors: I-II-III / Key: 4-5-6 / Ring: 7-8-9 / Plug: WX YZ
    • Implement Display for Plugboard (non-trivial due to currently only storing explicit mapping in the struct).
      • Just store the explicit pairs as part of the struct; will be useful for implementing Plugboard::unplugged.
  • Move setting randomization to Enigma::random() constructor.
  • Add methods to Enigma to get settings from current configuration (useful to reverse-engineer key/ring for Display).
  • Just return an Enigma instance from decrypt() methods.
  • Find more passages for end-to-end decryption tests, seed and randomize many decryptions.
  • Make test macro for fairly close decryption (>0.95%).
  • Add integration tests for randomly encrypted messages.
  • Update README.
  • Update release notes for 0.6.0.
  • Add decrypt example in docstring.
  • Benchmark decryption and decryption submethods.
  • Attempt to optimize parallelized loops.
  • Refactor decryption into 3 explicit steps to make it easier to pass in fitness functions.

References:

Try optimizing fitness functions

This might be faster as an explicit loop where the outgoing character is modulo'd off the accumulated index, the index is multiplied by 26, then the incoming character is added in.

ultra/src/fitness.rs

Lines 66 to 69 in 5ac5ef0

char_indices.windows(n)
.map(|w| w.iter().fold(0, |acc, x| 26 * acc + x))
.map(|i| ngrams[i])
.sum()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.