Python/ciphers/affine_cipher.py

95 lines
2.9 KiB
Python
Raw Normal View History

2016-08-07 15:48:46 +00:00
import sys, random, cryptomath_module as cryptoMath
SYMBOLS = r""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
2016-08-07 15:48:46 +00:00
2019-10-05 05:14:13 +00:00
2016-08-07 15:48:46 +00:00
def main():
2019-10-05 05:14:13 +00:00
message = input("Enter message: ")
key = int(input("Enter key [2000 - 9000]: "))
mode = input("Encrypt/Decrypt [E/D]: ")
if mode.lower().startswith("e"):
mode = "encrypt"
translated = encryptMessage(key, message)
elif mode.lower().startswith("d"):
mode = "decrypt"
translated = decryptMessage(key, message)
print("\n%sed text: \n%s" % (mode.title(), translated))
2016-08-07 15:48:46 +00:00
def getKeyParts(key):
keyA = key // len(SYMBOLS)
keyB = key % len(SYMBOLS)
return (keyA, keyB)
2019-10-05 05:14:13 +00:00
2016-08-07 15:48:46 +00:00
def checkKeys(keyA, keyB, mode):
2019-10-05 05:14:13 +00:00
if keyA == 1 and mode == "encrypt":
sys.exit(
"The affine cipher becomes weak when key A is set to 1. 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"
)
2016-08-07 15:48:46 +00:00
if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1:
2019-10-05 05:14:13 +00:00
sys.exit(
"Key A must be greater than 0 and key B must be between 0 and %s."
% (len(SYMBOLS) - 1)
)
2016-08-07 15:48:46 +00:00
if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1:
2019-10-05 05:14:13 +00:00
sys.exit(
"Key A %s and the symbol set size %s are not relatively prime. Choose a different key."
% (keyA, len(SYMBOLS))
)
2016-08-07 15:48:46 +00:00
def encryptMessage(key, message):
2019-10-05 05:14:13 +00:00
"""
2016-08-07 15:48:46 +00:00
>>> encryptMessage(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'
2019-10-05 05:14:13 +00:00
"""
2016-08-07 15:48:46 +00:00
keyA, keyB = getKeyParts(key)
2019-10-05 05:14:13 +00:00
checkKeys(keyA, keyB, "encrypt")
cipherText = ""
2016-08-07 15:48:46 +00:00
for symbol in message:
if symbol in SYMBOLS:
symIndex = SYMBOLS.find(symbol)
cipherText += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)]
else:
cipherText += symbol
return cipherText
2019-10-05 05:14:13 +00:00
2016-08-07 15:48:46 +00:00
def decryptMessage(key, message):
2019-10-05 05:14:13 +00:00
"""
2016-08-07 15:48:46 +00:00
>>> decryptMessage(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.'
2019-10-05 05:14:13 +00:00
"""
2016-08-07 15:48:46 +00:00
keyA, keyB = getKeyParts(key)
2019-10-05 05:14:13 +00:00
checkKeys(keyA, keyB, "decrypt")
plainText = ""
2016-08-07 15:48:46 +00:00
modInverseOfkeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS))
for symbol in message:
if symbol in SYMBOLS:
symIndex = SYMBOLS.find(symbol)
plainText += SYMBOLS[(symIndex - keyB) * modInverseOfkeyA % len(SYMBOLS)]
else:
plainText += symbol
return plainText
2019-10-05 05:14:13 +00:00
2016-08-07 15:48:46 +00:00
def getRandomKey():
while True:
keyA = random.randint(2, len(SYMBOLS))
keyB = random.randint(2, len(SYMBOLS))
if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1:
return keyA * len(SYMBOLS) + keyB
2016-08-07 15:48:46 +00:00
2019-10-05 05:14:13 +00:00
if __name__ == "__main__":
2016-08-07 15:48:46 +00:00
import doctest
2019-10-05 05:14:13 +00:00
2016-08-07 15:48:46 +00:00
doctest.testmod()
main()