AdditiveCipher#
The AdditiveCipher class implements the Additive Cipher, a type of monoalphabetic substitution cipher that shifts each character by a addding with a specified integer key.
- class AdditiveCipher(key: int)#
Creates a new AdditiveCipher instance with the specified key.
- Parameters:
key (int) – The integer by which each letter in the plaintext is shifted. Valid values are between 0 and 25.
Attention
AdditiveCipher is a basic symmetric cipher, which should be used ONLY for educational purposes, and NOT in production. Proceed accordingly.
Introduction#
The Additive Cipher (also known as the Caesar Cipher) is one of the oldest known encryption techniques. Each character in the plaintext is shifted by a fixed number of positions based on a given key. Although easy to implement, this cipher is insecure for practical use since it is easily broken by frequency analysis.
Mathematical Details#
In the Additive Cipher:
Each letter in the alphabet is mapped to a numeric value.
- The encryption formula is:
- \[c = (p + k) \mod 26\]
- where:
\(c\) is the ciphertext letter,
\(p\) is the plaintext letter,
\(k\) is the key (shift amount).
- Decryption reverses this shift with the formula:
- \[p = (c - k) \mod 26\]
Usage#
# Example usage of Additive Cipher to encrypt and decrypt a message
from cryptosystems import AdditiveCipher
cipher = AdditiveCipher(3)
ciphertext = cipher.encrypt("Hello World")
print(ciphertext) # 'Khosk Zruog'
plaintext = cipher.decrypt(ciphertext)
print(plaintext) # 'Hello World'
Methods#
Notes#
Limitations: The Additive Cipher operates on the letters of the alphabet. It does not encrypt spaces, punctuation, or numbers. The key must be an integer between 0 and 25.
Security: The Additive Cipher is not secure for practical. The Additive Cipher is vulnerable to frequency analysis, making it unsuitable for secure communications.
Application: Suitable for educational purposes or very basic encoding.