Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/codext/crypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
from .rot import *
from .scytale import *
from .shift import *
from .trithemius import *
from .xor import *

60 changes: 60 additions & 0 deletions src/codext/crypto/trithemius.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: UTF-8 -*-
"""Trithemius Cipher Codec - trithemius content encoding.

The Trithemius cipher is a polyalphabetic encryption method invented by the German
abbot Johannes Trithemius. It applies a sequence of progressive Caesar shifts (0, 1,
2, ...) to each successive letter in the plaintext, leaving non-alphabetic characters
unchanged. It is equivalent to a Vigenère cipher with the key "ABCDEFGHIJKLMNOPQRSTUVWXYZ...".

This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)

Reference: https://www.dcode.fr/trithemius-cipher
"""
from string import ascii_lowercase as LC, ascii_uppercase as UC

from ..__common__ import *


__examples__ = {
'enc(trithemius|trithemius-cipher)': {'this is a test': "tikv mx g ambd"},
'enc(trithemius)': {
'': "",
'HELLO': "HFNOS",
'12345!@#$': "12345!@#$",
},
'enc-dec(trithemius)': ["Hello, World!", "@random"],
}
__guess__ = ["trithemius"]


def _trithemius(text, decode=False):
r, pos = "", 0
for c in ensure_str(text):
if c in LC:
r += LC[(LC.index(c) + (-pos if decode else pos)) % 26]
pos += 1
elif c in UC:
r += UC[(UC.index(c) + (-pos if decode else pos)) % 26]
pos += 1
else:
r += c
return r


def trithemius_encode(text, errors="strict"):
r = _trithemius(ensure_str(text))
return r, len(text)


def trithemius_decode(text, errors="strict"):
r = _trithemius(ensure_str(text), decode=True)
return r, len(text)


add("trithemius", trithemius_encode, trithemius_decode,
r"trithemius(?:[-_]cipher)?$",
entropy=lambda e: e, printables_rate=lambda pr: pr, transitive=True)
Loading