Ciphergeard Package#
Vigenère Cipher#
- class ciphergeard.vigenere.VigenereCipher(keyword: str)#
Bases:
objectThis class implements the Vigenère Cipher, a classic polyalphabetic substitution cipher. It utilizes the algebraic method for encoding and decoding messages. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
- Parameters:
keyword (str) – The keyword to use while encoding/decoding.
Example#
from ciphergeard.vigenere import VigenereCipher keyword = "SECRET" cipher = VigenereCipher(keyword=keyword) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: sxvrgd ev htor plaintext = cipher.decode(ciphertext=ciphertext) # Output: attack at dawn
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- get_index(letter: str)#
Used to return the index of a letter.
- Parameters:
letter (str) – The letter to get the index of.
- Returns:
The index of the specified letter.
- Return type:
int
- match_keyword_length(plaintext: str)#
Used to format the self.keyword to match the length of the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The formatted keyword.
- Return type:
str
- exception ciphergeard.vigenere.VigenereCipherError#
Bases:
Exception
Vigenère Cipher (Beaufort Variant)#
- class ciphergeard.vigenere.beaufort.BeaufortVariant(keyword: str)#
Bases:
VigenereCipherThis class implements the Vigenère Cipher’s Beaufort Variant. It inherits from the standard
VigenereCipherclass and utilizes the algebraic method for encoding and decoding messages. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.The Beaufort variant works differently. It first decodes the previous encoded character using the keyword character. Then, it uses this decoded character to determine the shift for the next character in the plaintext during the encoding process.
- Parameters:
keyword (str) – The keyword to use while encoding/decoding.
Example#
from ciphergeard.vigenere.beaufort import BeaufortVariant keyword = "SECRET" cipher = BeaufortVariant(keyword=keyword) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: iprjyr wr zhej plaintext = cipher.decode(ciphertext=ciphertext) # Output: attack at dawn
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The text to encode.
- Returns:
The encoded text.
- Return type:
str
- exception ciphergeard.vigenere.beaufort.BeaufortVariantError#
Bases:
Exception
Vigenère Cipher (Gronsfeld Variant)#
- class ciphergeard.vigenere.gronsfeld.GronsfeldVariant(key: int)#
Bases:
VigenereCipherThe Vigenère Cipher’s Gronsfeld Variant. It inherits from the standard
VigenereCipherclass and utilizes the algebraic method for encoding and decoding messages. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.Each digit in the specified key (int) must be between 1 and 26.
- Parameters:
keyword (str) – The keyword to use while encoding/decoding.
Example#
from ciphergeard.vigenere.gronsfeld import GronsfeldVariant key = 69421 cipher = GronsfeldVariant(key=key) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: fbwbcp du iizo plaintext = cipher.decode(ciphertext=ciphertext) # Output: attack at dawn
- exception ciphergeard.vigenere.gronsfeld.GronsfeldVariantError#
Bases:
Exception
Vigenère Cipher (Running Key Variant)#
- class ciphergeard.vigenere.running_key.RunningKeyVariant(keywords: list[str], max_lcm: int = None)#
Bases:
VigenereCipherThe Vigenère Cipher’s Running Key Variant. It inherits from the standard
VigenereCipherclass and utilizes the algebraic method for encoding and decoding messages. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.Unlike the standard Vigenère cipher that uses a single keyword, the Running Key variant employs a series of keywords. The keywords are standardized by either repeated or truncated achieved by calculating the LCM of their lengths. The keywords are encoded by each other till a final keyword is obtained.
- Parameters:
keywords (list[str]) – The keywords to use.
max_lcm (int) – The maximum LCM value and can be any natural number, defaults to None.
- Raises:
ValueError – Indicates that there was an error during initialization.
Example#
from ciphergeard.vigenere.running_key import RunningKeyVariant keywords = ["CATS", "ARE", "CUTER", "THAN", "DOGS"] cipher = RunningKeyVariant(keywords=keywords) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: gvfkim kz pkcp plaintext = cipher.decode(ciphertext=ciphertext) # Output: attack at dawn
Affine Cipher#
- class ciphergeard.affine.AffineCipher(a: int, b: int, case_sensitive: bool = False)#
Bases:
objectThe Affine Cipher, a classic monoalphabetic substitution cipher in Python. It utilizes the algebraic method for encoding and decoding messages. The cipher is insensitive to case by default, but can be specified to be case-sensitive. If insensitive, all the characters will be lowercased before processing.
Encoding: E(x) = (ax + b) mod 26 Decoding: D(x) = (1/a)(x - b) mod 26
- Parameters:
a (int) – The multiplicative integer (must be co-prime with 26).
b (int) – The additive integer.
case_sensitive (bool, optional.) – Indicates whether the cipher should be case-sensitive or not, defaults to False.
- Raises:
AffineCipherError – Indicates an error while initializing.
Example#
from ciphergeard.affine import AffineCipher cipher = AffineCipher(a=3, b=5, case_sensitive=True) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: FKKFLJ FK OFTS plaintext = cipher.decode(ciphertext=ciphertext) # Output: ATTACK AT DAWN
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- get_index(letter: str)#
Used to get the index of a letter.
- Parameters:
letter (str) – The letter to get the index of.
- Returns:
The index of the specified letter.
- Return type:
int
- Raises:
AffineCipherError – Indicates that the specified letter was not valid.
- exception ciphergeard.affine.AffineCipherError#
Bases:
Exception
Atbash Cipher#
- class ciphergeard.atbash.AtbashCipher#
Bases:
objectThe Atbash Cipher, a class monoalphabetic substitution cipher. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
It maps the alphabets to their reverse ones. For eg. A <-> Z or S <-> H
Example#
from ciphergeard.atbash import AtbashCipher cipher = AtbashCipher() plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: zggzxp zg wzdm plaintext = cipher.decode(ciphertext=ciphertext) # Output: attack at dawn
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- get_index(letter: str)#
Used to return the index of a letter.
- Parameters:
letter (str) – The letter to get the index of.
- Returns:
The index of the specified letter.
- Return type:
int
- Raises:
AtbashCipherError – Indicates that the specified letter was not valid.
- exception ciphergeard.atbash.AtbashCipherError#
Bases:
Exception
Baconian Cipher#
- class ciphergeard.baconion.BaconianCipher(lookup_table: dict[str])#
Bases:
objectThe Baconian Cipher, a method of steganographic message encoding. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
A reverse dict of the lookup_table is generated for ease-of-access when decoding.
- Parameters:
lookup_table (dict) – The lookup table. Can be generated using the generate_lookup_table function.
Example#
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- generate_lookup_table()#
Used to generate a lookup table.
- Parameters:
multiple_char (bool, optional) – If set to True, it will include the whole english alphabet for the cipher of a letter. Else, only ‘a’ & ‘b’, defaults to False.
- Returns:
The lookup table.
- Return type:
dict
- exception ciphergeard.baconion.BaconianCipherError#
Bases:
Exception
Bifid Cipher#
- class ciphergeard.bifid.BifidCipher(keyword: str, filler_char: str = 'x')#
Bases:
objectThe Bifid Cipher, a cipher which combines the Polybius square with transposition, and uses fractionation to achieve diffusion. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
All odd-length inputs will be suffixed by the specified filler_char. ‘i’ and ‘j’ share a combined position.
- Parameters:
keyword (str) – The keyword to use while encoding.
filler_char (str) – The filler character to suffix the text when of odd-length, defaults to ‘x’.
Example#
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- generate_polybius_square()#
Used to generate a 5x5 Polybius Square.
- Returns:
The Polybius Square.
- Return type:
list
- map_chars(square=None)#
Used to map the position of the English characters in square or self.square.
- Parameters:
square (list) – A different square aside from self.square, defaults to None.
- Returns:
A dict containing the mapped position of all the English characters in the square.
- Return type:
dict[tuple]
- remove_dupes(l: list[str])#
Used to remove duplicates in a list by manually going through each and every element and checking if it already exists. list(set(l)) could’ve been used here.
- Parameters:
l (list[str]) – The list to remove duplicates from.
- Returns:
The list from which all the duplicates are removed.
- Return type:
list[str]
- exception ciphergeard.bifid.BifidCipherError#
Bases:
Exception
Caesar Cipher#
- class ciphergeard.caesar.CaesarCipher(offset: int, case_sensitive: bool = False)#
Bases:
AffineCipherThe Caesar Cipher, one of the simplest and most widely known substitution cipher. It is based on
AffineCipher. The cipher is by default insensitive to case, but can be specified to be case-sensitive. If insensitive, all the characters will be lowercased before processing.
- Parameters:
offset (int) – The offset.
case_sensitive (bool, optional.) – Indicates whether the cipher should be case-sensitive or not, defaults to False.
Example#
from ciphergeard.caesar import CaesarCipher offset = 4 cipher = CaesarCipher(offset=offset, case_sensitive=True) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: EXXEGO EX HEAR plaintext = cipher.decode(ciphertext=ciphertext) # Output: ATTACK AT DAWN
- exception ciphergeard.caesar.CaesarCipherError#
Bases:
Exception
Columnar Transposition Cipher#
- class ciphergeard.columnar_transposition.ColumnarTranspositionCipher(keyword: str)#
Bases:
objectThe Columnar Transposition Cipher in which the message is written out in rows of a fixed length, and then read out again column by column. The cipher is case-sensitive and can process uppercase and lowercase characters separately.
- Parameters:
keyword (str) – The keyword to use while encoding.
Example#
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- exception ciphergeard.columnar_transposition.ColumnarTranspositionCipherError#
Bases:
Exception
Morse Code#
- class ciphergeard.morse.MorseCode#
Bases:
objectMorse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
Example#
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
Playfair Cipher#
- class ciphergeard.playfair.PlayfairCipher(keyword: str, filler_char: str = 'x', table: list = None, char_map: dict = None)#
Bases:
objectThe Playfair cipher, a manual symmetric encryption technique and the first literal digram substitution cipher. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.
All odd-length inputs will be suffixed by the specified filler_char. ‘i’ and ‘j’ share a combined position.
- Parameters:
keyword (str) – The keyword to use while encoding.
filler_char (str) – The filler character to suffix the text when of odd-length, defaults to ‘x’.
table (list, optional.) – The encoding table (if already generated), defaults to None.
char_map (dict, optional.) – The char map for the encoding table (if already generated), defaults to None.
Example#
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
- generate_table()#
Used to generate an encoding table for Playfair Cipher.
- Returns:
The generated encoding table.
- Return type:
list
- map_chars(table=None)#
Used to map the position of the English characters in table or self.table.
- Parameters:
table (list) – A different table aside from self.table, defaults to None.
- Returns:
A dict containing the mapped position of all the English characters in the table.
- Return type:
dict[tuple]
- remove_dupes(l: list[str])#
Used to remove duplicates in a list by manually going through each and every element and checking if it already exists. list(set(l)) could’ve been used here.
- Parameters:
l (list[str]) – The list to remove duplicates from.
- Returns:
The list from which all the duplicates are removed.
- Return type:
list[str]
- exception ciphergeard.playfair.PlayfairCipherError#
Bases:
Exception
Rail Fence Cipher#
- class ciphergeard.rail_fence.RailFenceCipher(rails: int, placeholder: str = '#')#
Bases:
objectThe Rail Fence Cipher, is a classical type of transposition cipher. It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails. The cipher is case-sensitive and can process uppercase and lowercase characters separately.
- Parameters:
rails (int) – The number of rails to use.
placeholder (str, optional) – The character to use as a placeholder when decoding, defaults to ‘#’.
Example#
from ciphergeard.rail_fence import RailFenceCipher cipher = RailFenceCipher(rails=2) plaintext = "Attack at Dawn" ciphertext = cipher.encode(plaintext=plaintext) # Output: Atc tDwtaka an plaintext = cipher.decode(ciphertext=ciphertext) # Output: Attack at Dawn
- decode(ciphertext: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
- Returns:
The decoded text.
- Return type:
str
- encode(plaintext: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
- Returns:
The encoded text.
- Return type:
str
ROT13 Cipher#
- class ciphergeard.rot13.ROT13Cipher(case_sensitive: bool = False)#
Bases:
CaesarCipherThe ROT13 Cipher, a simple letter substitution cipher that replaces a letter with the 13th letter after it. It is a special case of the
CaesarCipherwith the offset set to 13.
- Parameters:
case_sensitive (bool, optional.) – Indicates whether the cipher should be case-sensitive or not, defaults to False.
Example#
from ciphergeard.rot13 import ROT13Cipher cipher = ROT13Cipher(case_sensitive=True) plaintext = "ATTACK AT DAWN" ciphertext = cipher.encode(plaintext=plaintext) # Output: NGGNPX NG QNJA plaintext = cipher.decode(ciphertext=ciphertext) # Output: ATTACK AT DAWN
Vernam Cipher#
- class ciphergeard.vernam.VernamCipher#
Bases:
objectThe Vernam Cipher, is a form of
VigenereCipherbut utilizes a unqiue keyword (equal to the length of the plaintext) everytime when encoding. The cipher is not case-sensitive, so both plaintext and encoded text are converted to lowercase during processing.Example#
- decode(ciphertext: str, keyword: str)#
Used to decode the ciphertext.
- Parameters:
ciphertext (str) – The encoded text to decode.
keyword (str) – The keyword to use.
- Returns:
The decoded text.
- Return type:
str
- Raises:
VernamCipherError – Indicates an error while encoding.
- encode(plaintext: str, keyword: str)#
Used to encode the plaintext.
- Parameters:
plaintext (str) – The plaintext to encode.
keyword (str) – The keyword to use.
- Returns:
The encoded text.
- Return type:
str
- Raises:
VernamCipherError – Indicates an error while encoding.
- generate_keyword(n: int)#
Used to generate a random keyword of n length.
- Parameters:
n (int) – The length of the keyword.
- exception ciphergeard.vernam.VernamCipherError#
Bases:
Exception