Giter Site home page Giter Site logo

enigma4j's Introduction

Enigma4J (Enigma Emulator for Java)

This project contains an emulation of the Enigma machine using Java 16. Enigma is a cipher device invented in 1918. It became later on popular during World War II.

Enigma is an electric device comprised of a keyboard, 3-5 rotors, an optional plugboard and 26 output lights. Basically, it’s an electric circuit with mechanically rotating wheels that change the circuit wiring between input- and output characters.

The principle of Enigma is having rotors with a specific wiring between an input- and output port which compares well to an encryption key. Input letters are re-routed (scrambled) to an output letter. This happens multiple times in forward- and reverse direction. Additionally, when entering a key and before sending the signal through a rotor, the rotor rotates by one position which leads the machine to encrypt successive same characters to different outputs. Rotors can have notches at certain positions that advance the subsequent rotor (similar to a mileage counter that turns over from 9 to 0).

Finally, there were commercial and military machines. Military machines could have a plug board to re-route individual characters. It is a freely configurable routing matrix between each of the 26 letters allowing to attach 10 patches to alter character routing of 20 of the 26 letters. For example the patching AT would translate A to T and vice versa. Plugboard routing works for either of the entered letters and works in both directions (input, and signal output) which adds further combinations to the overall keyspace leaving the machine with 158,962,555,217,826,360,000 combinations.

A specific behavior (later a weakness) of the machine is that a letter could never encrypt to itself which finally lead to its decryption.

The Enigma machine itself has no dedicated encryption/decryption mode. To encrypt a message, spin up a Enigma instance and enter your message. The receiver spins up the exact same instance with the same initial settings (rotor positions, plug board) and enters the encrypted message to turn it into plain text.

Java Emulator

This emulator consists of an inventory of machines, see inventory.json that can be loaded and instantiated for crypto use.

var inventory = Inventory.load();
var modelI = inventory.getModel(new Inventory.ModelIdentifier("Enigma I"));
var enigma = modelI.createInstance();
var rotorPositions = enigma.getRotorPositions();

assertThat(enigma.process("WURSTSALAT")).isEqualTo("FXYKEONOEX");

enigma.setRotorPositions(rotorPositions);
assertThat(enigma.process("FXYKEONOEX")).isEqualTo("WURSTSALAT");

Individual machines can be configured before their actual usage:

var inventory = Inventory.load();
var modelI = inventory.getModel(new Inventory.ModelIdentifier("Enigma I"));

var enigma = modelI.createInstance(c -> {
    c.rotors((rotorConfiguration, rotorSelector) -> rotorConfiguration
            .withRotors(rotorSelector.getRotors("II", "I", "III")).withPositions(1, 2, 3));
});

assertThat(enigma.process("WURSTSALAT")).isEqualTo("BPLKHVVVKT");

Finally, there’s a EnigmaWriter for stream-processing of textual data:

StringWriter out = new StringWriter();

PrintWriter pw = new PrintWriter(new EnigmaWriter(out, instance));
pw.print("hallowelt");

assertThat(out.toString()).isEqualToIgnoringCase("alsyipmon");

Building from Source

The code can be easily build with the maven wrapper. You also need JDK 16.

 $ ./mvnw clean install

If you want to build with the regular mvn command, you will need Maven v3.5.0 or above.

enigma4j's People

Contributors

mp911de avatar

Stargazers

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

Watchers

 avatar  avatar

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.