npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@enigmaciphy/engine

v0.0.8

Published

An implementation of the enigma machine

Downloads

4

Readme

This is an implementation of the enigma machine encryption algorithm. The cipher heavily relies on substitution.

Rotor

It substitutes an alphabet letter, representing the wiring of the rotor, the offset of the ring may be positioned in a different position of the alphabet. A letter may go through substitution throughout many rotors. Each time a letter is substited the rotor turns to the next letter. Due to it's "turning" nature, a sequence of the same character will produce a different ciphertext for each turn.

Rotor Notch

The point at which the next rotor position should be moved forward.

Rotor processing representation

We will assume the position AAA and rings AAA as well as the following wiring alphabet.

Position          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Alphabet          A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
Shuffled Alphabet B  D  F  H  J  L  C  P  R  T  X  V  Z  N  Y  E  I  W  G  A  K  M  U  S  Q  O

So we initially have the following mapping A=B, B=D, C=F, D=H, [...].

Rotation

Before processing anything the machine will rotate from 0 to 1.

Wiring shift

The mapping will happen from position 1 (A) outputing D.

                     ⬇️
Position          0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Alphabet          A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
Shuffled Alphabet B  D  F  H  J  L  C  P  R  T  X  V  Z  N  Y  E  I  W  G  A  K  M  U  S  Q  O

Rotor's position reverse shift

The output D from the wiring shift, which is at alphabet position 3 will be subtracted by the rotor's position, 3 - 1 = 2, thus the final output will be the letter C, which is located at position 2.

Reflector

Works like a bi-directional map. The reflector is usually fixed, meaning it does not rotates as oposed to the rotors.

A maps B then
B maps A

Plugboard

Maps a letter to another letter for its substitution, same property seem by the reflector. The major benefit is the possibility of mapping letters as needed, thus adding another layer of complexity to the cipher. The plugboard is applied before anything as well as after everything, meaning it is the first substitution and also the last, happening twice.

A maps B then
B maps A

Usage:

npm install @enigmaciphy/engine
// -- ENCRYPTING
import { Cipher, CipherJSON } from '@enigmaciphy/engine';

const configuration: CipherJSON = {
	alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
	plugboard: { wiring: 'AQRIJFHGDEWLTNSXBCOMZVKPYU' },
	entry: { wiring: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' },
	rotors: [
		{ wiring: 'EKMFLGDQVZNTOWYHXUSPAIBRCJ', notches: ['Q'] },
		{ wiring: 'AJDKSIRUXBLHWTMCQGZNPYFVOE', notches: ['E'] },
		{ wiring: 'BDFHJLCPRTXVZNYEIWGAKMUSQO', notches: ['V'] },
	],
	reflector: { wiring: 'YRUHQSLDPXNGOKMIEBFZCWVJAT' },
	chargroup: 5,
};
let cipher = Cipher.fromJSON(configuration);

cipher.encrypt('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
// XPJUP VYBRA QAJNY VAIXO UUWXO VVPDM LKVEK BHQIL DMAKH YL

// -- DECRYPTING

// To decrypt the cipher is instantiated again so the rotors will be on their initial position.
cipher = Cipher.fromJSON(configuration);

cipher.encrypt('XPJUP VYBRA QAJNY VAIXO UUWXO VVPDM LKVEK BHQIL DMAKH YL');
// LOREM IPSUM DOLOR SITAM ETCON SECTE TURAD IPISC INGEL IT

The code output has been tested against the following existing machines:

  • Universal Enigma: http://people.physik.hu-berlin.de/~palloks/js/enigma/enigma-u_v25_en.html
  • Cryptii enigma machine: https://cryptii.com/pipes/enigma-machine

Note: This application currently does not support ring settings.

Sources:

  • Hackaday - THE ENIGMA ENIGMA: HOW THE ENIGMA MACHINE WORKED: https://hackaday.com/2017/08/22/the-enigma-enigma-how-the-enigma-machine-worked/
  • CryptoMuseum - Enigma wiring: https://www.cryptomuseum.com/crypto/enigma/wiring.htm
  • Enigma rotation example: https://crypto.stackexchange.com/a/71233/81146
  • A1: Enigma https://www.cs.cornell.edu/courses/cs3110/2018sp/a1/a1.html
  • Detailed working through of Enigma encipherment: https://www.codesandciphers.org.uk/enigma/example1.htm

Debug

VSCode example F1 > Debug: Toggle Auto Attach > Always > inside vscode own terminal > node --inspect ./node_modules/.bin/jest src/Configuration/Rotor/Rotor.spec.ts