mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Fixed warning string for Key B = 0 (#1639)
* Fixed warning string for Key B = 0 * Update affine_cipher.py * Update affine_cipher.py * decrypt_message(encrypt_message()) Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
c67776da59
commit
1d9266eca0
|
@ -1,56 +1,63 @@
|
||||||
import sys, random, cryptomath_module as cryptoMath
|
import random
|
||||||
|
import sys
|
||||||
|
|
||||||
SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
|
import cryptomath_module as cryptomath
|
||||||
|
|
||||||
|
SYMBOLS = (r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"""
|
||||||
|
r"""abcdefghijklmnopqrstuvwxyz{|}~""")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
message = input("Enter message: ")
|
"""
|
||||||
key = int(input("Enter key [2000 - 9000]: "))
|
>>> key = get_random_key()
|
||||||
mode = input("Encrypt/Decrypt [E/D]: ")
|
>>> msg = "This is a test!"
|
||||||
|
>>> decrypt_message(key, encrypt_message(key, msg)) == msg
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
message = input("Enter message: ").strip()
|
||||||
|
key = int(input("Enter key [2000 - 9000]: ").strip())
|
||||||
|
mode = input("Encrypt/Decrypt [E/D]: ").strip().lower()
|
||||||
|
|
||||||
if mode.lower().startswith("e"):
|
if mode.startswith("e"):
|
||||||
mode = "encrypt"
|
mode = "encrypt"
|
||||||
translated = encryptMessage(key, message)
|
translated = encrypt_message(key, message)
|
||||||
elif mode.lower().startswith("d"):
|
elif mode.startswith("d"):
|
||||||
mode = "decrypt"
|
mode = "decrypt"
|
||||||
translated = decryptMessage(key, message)
|
translated = decrypt_message(key, message)
|
||||||
print("\n%sed text: \n%s" % (mode.title(), translated))
|
print(f"\n{mode.title()}ed text: \n{translated}")
|
||||||
|
|
||||||
|
|
||||||
def getKeyParts(key):
|
def check_keys(keyA, keyB, mode):
|
||||||
keyA = key // len(SYMBOLS)
|
if mode == "encrypt":
|
||||||
keyB = key % len(SYMBOLS)
|
if keyA == 1:
|
||||||
return (keyA, keyB)
|
sys.exit(
|
||||||
|
"The affine cipher becomes weak when key "
|
||||||
|
"A is set to 1. Choose different key"
|
||||||
def checkKeys(keyA, keyB, mode):
|
)
|
||||||
if keyA == 1 and mode == "encrypt":
|
if keyB == 0:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"The affine cipher becomes weak when key A is set to 1. Choose different key"
|
"The affine cipher becomes weak when key "
|
||||||
)
|
"B is set to 0. Choose different key"
|
||||||
if keyB == 0 and mode == "encrypt":
|
)
|
||||||
sys.exit(
|
|
||||||
"The affine cipher becomes weak when key A is set to 1. Choose different key"
|
|
||||||
)
|
|
||||||
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
|
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"Key A must be greater than 0 and key B must be between 0 and %s."
|
"Key A must be greater than 0 and key B must "
|
||||||
% (len(SYMBOLS) - 1)
|
f"be between 0 and {len(SYMBOLS) - 1}."
|
||||||
)
|
)
|
||||||
if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1:
|
if cryptomath.gcd(keyA, len(SYMBOLS)) != 1:
|
||||||
sys.exit(
|
sys.exit(
|
||||||
"Key A %s and the symbol set size %s are not relatively prime. Choose a different key."
|
f"Key A {keyA} and the symbol set size {len(SYMBOLS)} "
|
||||||
% (keyA, len(SYMBOLS))
|
"are not relatively prime. Choose a different key."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def encryptMessage(key, message):
|
def encrypt_message(key: int, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> encryptMessage(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.')
|
>>> encrypt_message(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.')
|
||||||
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
|
'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi'
|
||||||
"""
|
"""
|
||||||
keyA, keyB = getKeyParts(key)
|
keyA, keyB = divmod(key, len(SYMBOLS))
|
||||||
checkKeys(keyA, keyB, "encrypt")
|
check_keys(keyA, keyB, "encrypt")
|
||||||
cipherText = ""
|
cipherText = ""
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in SYMBOLS:
|
if symbol in SYMBOLS:
|
||||||
|
@ -61,15 +68,15 @@ def encryptMessage(key, message):
|
||||||
return cipherText
|
return cipherText
|
||||||
|
|
||||||
|
|
||||||
def decryptMessage(key, message):
|
def decrypt_message(key: int, message: str) -> str:
|
||||||
"""
|
"""
|
||||||
>>> decryptMessage(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi')
|
>>> decrypt_message(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi')
|
||||||
'The affine cipher is a type of monoalphabetic substitution cipher.'
|
'The affine cipher is a type of monoalphabetic substitution cipher.'
|
||||||
"""
|
"""
|
||||||
keyA, keyB = getKeyParts(key)
|
keyA, keyB = divmod(key, len(SYMBOLS))
|
||||||
checkKeys(keyA, keyB, "decrypt")
|
check_keys(keyA, keyB, "decrypt")
|
||||||
plainText = ""
|
plainText = ""
|
||||||
modInverseOfkeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS))
|
modInverseOfkeyA = cryptomath.findModInverse(keyA, len(SYMBOLS))
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in SYMBOLS:
|
if symbol in SYMBOLS:
|
||||||
symIndex = SYMBOLS.find(symbol)
|
symIndex = SYMBOLS.find(symbol)
|
||||||
|
@ -79,11 +86,11 @@ def decryptMessage(key, message):
|
||||||
return plainText
|
return plainText
|
||||||
|
|
||||||
|
|
||||||
def getRandomKey():
|
def get_random_key():
|
||||||
while True:
|
while True:
|
||||||
keyA = random.randint(2, len(SYMBOLS))
|
keyA = random.randint(2, len(SYMBOLS))
|
||||||
keyB = random.randint(2, len(SYMBOLS))
|
keyB = random.randint(2, len(SYMBOLS))
|
||||||
if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1:
|
if cryptomath.gcd(keyA, len(SYMBOLS)) == 1:
|
||||||
return keyA * len(SYMBOLS) + keyB
|
return keyA * len(SYMBOLS) + keyB
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user