Enigma

class enigma.Enigma(model: str = '', umkehrwalzen: Optional[List[MzEnigma.component.Umkehrwalze]] = None, walzen: Optional[List[MzEnigma.component.Walze]] = None, numberOfWalzen: int = 0, steckerbrett: Optional[MzEnigma.component.Steckerbrett] = None, zusatzwalzen: Optional[List[MzEnigma.component.Zusatzwalze]] = None)[source]
Represents an Enigma engine with all available accessories.

If no umkehrwalzen are defined, the encryption is not symmetric any more !

Parameters
  • model – model of the Enigma (required)

  • umkehrwalzen – available objects of type ‘Umkehrwalze’ (if any)

  • walzen – available objects of type ‘Walze’ (at least 1 required)

  • numberOfWalzen – number of objects of type ‘Walze’ to be inserted

  • steckerbrett – object of type ‘Steckerbrett’, if any

  • zusatzwalzen – available objects of type ‘Zusatzwalze’, if any

  • notify – notification function (e.g. print)

property alphabet: str
Getter

Returns the alphabet of the engine

Setter

None

property model: str
Getter

Returns the name of the engine

Setter

None

property numberOfWalzen: int
Getter

Returns a number of Walzen built-in at the engine’s operation

Setter

None

property steckerbrett: Optional[int]
Getter

Returns the steckerbrett if any

Setter

None

property umkehrwalzen: List[MzEnigma.component.Umkehrwalze]
Getter

Returns a list of Umkehrwalzen

Setter

None

static validCribPositions(encodedSpruch: str = '', crib: str = '') List[int][source]

This function delivers all positions where no crib letter encrypts as itself

Parameters
  • encodedSpruch – message to be decoded

  • crib – substring to be examined

Returns

list of valid positions relative to the encodedSpruch

property walzen: List[MzEnigma.component.Walze]
Getter

Returns a list of Walzen

Setter

None

property zusatzwalzen: Optional[List[MzEnigma.component.Zusatzwalze]]
Getter

Returns a list of Zusatzwalzen if any

Setter

None

class enigma.SpruchScoring(language: str, normalize: bool = True, logarithmic: bool = False)[source]

Toolbox for scoring of a spruch. In the background, this object holds dictionary with character n-grams. By default, the dictionary with the longest n-gram is used.

Parameters
  • language – language to be used for ngram scoring (required)

  • normalize – normalize ngram values with respect to maxScore

  • logarithmic – 10*math.log10(ngramDict[score]/minScore)

static indexOfCoincidence(spruch: str, validChars: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') float[source]
index of coincidence of a spruch,

i.e. relative frequency of characters independent of the underlying language

Parameters
  • spruch – spruch to be scored (required)

  • validChars – alphabet of the spruch

Returns

index of coincidence

newNgramScore(spruch: str, currentScore: float, validChars: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') float[source]
Compares the N-gram score of a spruch with the currentScore.

Uses simulated if enabled.

Parameters
  • spruch – spruch to be scored (required)

  • currentScore – current score (required)

  • validChars – alphabet of the spruch

Returns

N-gram score

ngramScore(spruch: str, numberOfChars: int = 0, validChars: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') float[source]

N-gram score of a spruch

Parameters
  • spruch – spruch to be scored (required)

  • numberOfChars – n-gram dictionary to be used (default: longest n-grams)

  • validChars – alphabet of the spruch

Returns

N-gram score

setSATemperature(spruch: str, numberOfChars: int = 0) float[source]

Enables the temperature for simulated annealing (used by newNgramScore)

Parameters
  • spruch – spruch of typical length to be scored (required)

  • numberOfChars – n-gram dictionary to be used (default: longest n-grams)

class enigma.Tagesschluessel(enigma: Optional[enigma.Enigma] = None, umkehrwalze: Optional[MzEnigma.component.Umkehrwalze] = None, walzen: Optional[List[MzEnigma.component.Walze]] = None, tagesWalzenStellungen: Optional[str] = None, steckerbrett: Optional[MzEnigma.component.Steckerbrett] = None, zusatzwalze: Optional[MzEnigma.component.Zusatzwalze] = None, blank: str = '', notify: Optional[Callable[[str], None]] = None)[source]

Represents the Tagesschluessel of an Enigma

Parameters
  • enigma – model of the Enigma (required)

  • umkehrwalze – ‘Umkehrwalze’ (if undefined, randomly selected)

  • walzen – List of type ‘Walze’ (if undefined, randomly selecte(if undefined, randomly selected)

  • zusatzwalze – ‘Zusatzwalze’ (if undefined, randomly selected)

  • blank – replacement character for a blank

  • notify – notification function (e.g. print)

property alphabet: str
Getter

Returns the alphabet of the engine

Setter

None

chain() List[MzEnigma.component.Component][source]

Creates a chain of visited components

Returns

chain list

classmethod changeWalzen(currentTagesschluessel: enigma.Tagesschluessel, umkehrwalze: Optional[MzEnigma.component.Umkehrwalze] = None, walzen: Optional[List[MzEnigma.component.Walze]] = None, tagesWalzenStellungen: Optional[str] = None, zusatzwalze: Optional[MzEnigma.component.Zusatzwalze] = None) None[source]

Changes certain elements of a Tagesschluessel

Parameters
  • currentTagesschluessel – current Tagesschluessel (required)

  • tagesWalzenStellungen – changed Tageswalzenstellungen to be used (if undefined, randomly selected)

  • umkehrwalze – ‘Umkehrwalze’ (if undefined, used from currentTagesschluessel)

  • walzen – List of type ‘Walze’ (if undefined, used from currentTagesschluessel)

  • zusatzwalze – ‘Zusatzwalze’ (if undefined, used from currentTagesschluessel)

Returns

Tagesschluessel object

debugEncode(c: str) str[source]

Shows the transformation of a character after each component

Parameters

c – single character

Returns

text built up from the characters after each component

decode(encodedSpruch: str, useSpruchWalzenStellungen: bool = False) str[source]
Runs backward through the enigma

If useSpruchWalzenStellungen, - the spruchWalzenStellung is expected twice at the beginning - the spruch is then decoded using the spruchWalzenStellungen otherwise the spruch is decoded using the tagesWalzenStellungen

Parameters
  • encodedSpruch – message to be decoded

  • useSpruchWalzenStellungen – see above

Returns

decoded spruch

encode(spruch: str, spruchWalzenStellungen: Optional[str] = None) str[source]
Runs forward through the enigma

If the spruchWalzenStellungen is defined, - it is encoded twice using the tagesWalzenStellungen - the spruch is then encoded using the spruchWalzenStellungen otherwise the spruch is encoded using the tagesWalzenStellungen

Parameters
  • spruch – message to be encoded

  • spruchWalzenStellungen – see above

Returns

encoded spruch

encodeMatrix(msgLen: int) List[List[str]][source]

Runs forward through the enigma using all characters of the alphabet

Parameters

msgLen – length of messages

Returns

list of encoded messages

findDoublets(first: int = 0, second: int = 3, all: bool = True) Union[List[List[str]], List[str]][source]

Find doublets of any character at fixed positions

Parameters
  • first – first position

  • second – second position (> first)

Returns

IF ALL it returns a list of doublets for every position ELSE it returns a list of doublets

findPatterns(substringList: List[str] = [''], encodedSpruchList: List[str] = []) Dict[str, Set[str]][source]

Find a list of substringList for any tagesWalzenStellungen at any positions

Parameters
  • substringList – list of substrings

  • encodedSpruchList – list of messages to be searched for substrings

Returns

dict(substring, set of tagesWalzenStellungen)

frequencyDict(spruch: str, decode: bool = True) Dict[int, Set[Tuple[str, Set[str]]]][source]

Creates a frequency dictionary of a message

Parameters
  • spruch – message to be analysed (required)

  • decode – use decoced message instead of the original message

Returns

Dict[frequency, Set[Tuple[char, Tuple[occurrency]]