#6: Implementing Permutations-Chiffre
- Also some refactoring to other ciphers
This commit is contained in:
parent
7f9a4f7adc
commit
ee73aaafa7
|
@ -3,11 +3,11 @@ from utils import AlphabetUtils as au
|
||||||
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
|
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
|
||||||
|
|
||||||
|
|
||||||
def encrypt_text(key: int = 11, cleartext: str = 'BonkRocks'):
|
def encrypt_text(cleartext: str, key: int):
|
||||||
"""
|
"""
|
||||||
Encrypts the given text with the given key
|
Encrypts the given text with the given key
|
||||||
:param key: The key to use. Has to be between 1 and 25 and a coprime to 26.
|
|
||||||
:param cleartext: The text to encrypt
|
:param cleartext: The text to encrypt
|
||||||
|
:param key: The key to use. Has to be between 1 and 25 and a coprime to 26.
|
||||||
:return: The encrypted text
|
:return: The encrypted text
|
||||||
"""
|
"""
|
||||||
# Key has to be coprime to 26
|
# Key has to be coprime to 26
|
||||||
|
@ -24,11 +24,11 @@ def encrypt_text(key: int = 11, cleartext: str = 'BonkRocks'):
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
|
||||||
def decrypt_text(key: int = 11, ciphertext: str = 'lyngfywgq'):
|
def decrypt_text(ciphertext: str, key: int):
|
||||||
"""
|
"""
|
||||||
Decrypts the given ciphertext with the given key
|
Decrypts the given ciphertext with the given key
|
||||||
:param key: The key to use. Has to be between 1 and 25 and a coprime to 26.
|
|
||||||
:param ciphertext: The text to decrypt
|
:param ciphertext: The text to decrypt
|
||||||
|
:param key: The key to use. Has to be between 1 and 25 and a coprime to 26.
|
||||||
:return: The decrypted text
|
:return: The decrypted text
|
||||||
"""
|
"""
|
||||||
# Key has to be coprime to 26
|
# Key has to be coprime to 26
|
||||||
|
@ -61,5 +61,5 @@ def _get_key_reverse(key: int):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(encrypt_text())
|
print(encrypt_text('BonkRocks', 11))
|
||||||
print(decrypt_text())
|
print(decrypt_text('lyngfywgq', 11))
|
||||||
|
|
55
chapter_one/Permutations-Chiffre.py
Normal file
55
chapter_one/Permutations-Chiffre.py
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
import random
|
||||||
|
|
||||||
|
from utils import AlphabetUtils as au
|
||||||
|
|
||||||
|
|
||||||
|
def encrypt_text(cleartext: str, key: [int]):
|
||||||
|
"""
|
||||||
|
Encrypts the given text with the given key
|
||||||
|
:param cleartext: The text to encrypt
|
||||||
|
:param key: The key to use. Has to be a list of indices of the alphabet, so [0...25] shuffled
|
||||||
|
:return: The encrypted text
|
||||||
|
"""
|
||||||
|
resulting = ''
|
||||||
|
|
||||||
|
for char in cleartext:
|
||||||
|
charIndex = au.get_index_of_letter(char)
|
||||||
|
cipherIndex = key.index(charIndex)
|
||||||
|
cipherChar = au.get_letter_at_index(cipherIndex)
|
||||||
|
resulting += cipherChar
|
||||||
|
|
||||||
|
return resulting
|
||||||
|
|
||||||
|
|
||||||
|
def decrypt_text(ciphertext: str, key: [int]):
|
||||||
|
"""
|
||||||
|
Decrypts the given ciphertext with the given key
|
||||||
|
:param ciphertext: The text to decrypt
|
||||||
|
:param key: The key to use. Has to be a list of indices of the alphabet, so [0...25] shuffled
|
||||||
|
:return: The decrypted text
|
||||||
|
"""
|
||||||
|
resulting = ''
|
||||||
|
|
||||||
|
for char in ciphertext:
|
||||||
|
charIndex = au.get_index_of_letter(char)
|
||||||
|
clearIndex = key[charIndex]
|
||||||
|
clearChar = au.get_letter_at_index(clearIndex)
|
||||||
|
resulting += clearChar
|
||||||
|
|
||||||
|
return resulting
|
||||||
|
|
||||||
|
|
||||||
|
def _generate_key():
|
||||||
|
indices = [i for i in range(26)]
|
||||||
|
|
||||||
|
random.shuffle(indices)
|
||||||
|
|
||||||
|
return indices
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
key = _generate_key()
|
||||||
|
print(key)
|
||||||
|
encrypted = encrypt_text('BonkRocks', key)
|
||||||
|
print(encrypted)
|
||||||
|
print(decrypt_text(encrypted, key))
|
|
@ -3,12 +3,12 @@ from utils import AlphabetUtils as au
|
||||||
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
|
ALLOWED_KEYS = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
|
||||||
|
|
||||||
|
|
||||||
def encrypt_text(key1: int = 11, key2: int = 7, cleartext: str = 'BonkRocks'):
|
def encrypt_text(cleartext: str, key1: int, key2: int):
|
||||||
"""
|
"""
|
||||||
Encrypts the given text with the given keys
|
Encrypts the given text with the given keys
|
||||||
|
:param cleartext: The text to encrypt
|
||||||
:param key1: The first key. Has to be between 1 and 25 and a coprime to 26
|
:param key1: The first key. Has to be between 1 and 25 and a coprime to 26
|
||||||
:param key2: The second key. Has to be between 0 and 26
|
:param key2: The second key. Has to be between 0 and 26
|
||||||
:param cleartext: The text to encrypt
|
|
||||||
:return: The encrypted text
|
:return: The encrypted text
|
||||||
"""
|
"""
|
||||||
# key1 has to be coprime to 26
|
# key1 has to be coprime to 26
|
||||||
|
@ -29,12 +29,12 @@ def encrypt_text(key1: int = 11, key2: int = 7, cleartext: str = 'BonkRocks'):
|
||||||
return resulting
|
return resulting
|
||||||
|
|
||||||
|
|
||||||
def decrypt_text(key1: int = 11, key2: int = 7, ciphertext: str = 'sfunmfdnx'):
|
def decrypt_text(ciphertext: str, key1: int, key2: int):
|
||||||
"""
|
"""
|
||||||
Decrypts the given ciphertext with the given keys
|
Decrypts the given ciphertext with the given keys
|
||||||
|
:param ciphertext: The text to decrypt
|
||||||
:param key1: The first key. Has to be between 1 and 25 and a coprime to 26
|
:param key1: The first key. Has to be between 1 and 25 and a coprime to 26
|
||||||
:param key2: The second key. Has to be between 0 and 26
|
:param key2: The second key. Has to be between 0 and 26
|
||||||
:param ciphertext: The text to decrypt
|
|
||||||
:return: The decrypted text
|
:return: The decrypted text
|
||||||
"""
|
"""
|
||||||
# key1 has to be coprime to 26
|
# key1 has to be coprime to 26
|
||||||
|
@ -71,5 +71,5 @@ def _get_key_reverse(key: int):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(encrypt_text())
|
print(encrypt_text('BonkRocks', 11, 7))
|
||||||
print(decrypt_text())
|
print(decrypt_text('sfunmfdnx', 11, 7))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user