|
| 1 | +import sys, random, cryptomath_module as cryptoMath |
| 2 | + |
| 3 | +SYMBOLS = """ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~""" |
| 4 | + |
| 5 | +def main(): |
| 6 | + message = input('Enter message: ') |
| 7 | + key = int(input('Enter key [2000 - 9000]: ')) |
| 8 | + mode = input('Encrypt/Decrypt [E/D]: ') |
| 9 | + |
| 10 | + if mode.lower().startswith('e'): |
| 11 | + mode = 'encrypt' |
| 12 | + translated = encryptMessage(key, message) |
| 13 | + elif mode.lower().startswith('d'): |
| 14 | + mode = 'decrypt' |
| 15 | + translated = decryptMessage(key, message) |
| 16 | + print('\n%sed text: \n%s' % (mode.title(), translated)) |
| 17 | + |
| 18 | +def getKeyParts(key): |
| 19 | + keyA = key // len(SYMBOLS) |
| 20 | + keyB = key % len(SYMBOLS) |
| 21 | + return (keyA, keyB) |
| 22 | + |
| 23 | +def checkKeys(keyA, keyB, mode): |
| 24 | + if keyA == 1 and mode == 'encrypt': |
| 25 | + sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') |
| 26 | + if keyB == 0 and mode == 'encrypt': |
| 27 | + sys.exit('The affine cipher becomes weak when key A is set to 1. Choose different key') |
| 28 | + if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: |
| 29 | + sys.exit('Key A must be greater than 0 and key B must be between 0 and %s.' % (len(SYMBOLS) - 1)) |
| 30 | + if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1: |
| 31 | + sys.exit('Key A %s and the symbol set size %s are not relatively prime. Choose a different key.' % (keyA, len(SYMBOLS))) |
| 32 | + |
| 33 | +def encryptMessage(key, message): |
| 34 | + ''' |
| 35 | + >>> encryptMessage(4545, 'The affine cipher is a type of monoalphabetic substitution cipher.') |
| 36 | + 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi' |
| 37 | + ''' |
| 38 | + keyA, keyB = getKeyParts(key) |
| 39 | + checkKeys(keyA, keyB, 'encrypt') |
| 40 | + cipherText = '' |
| 41 | + for symbol in message: |
| 42 | + if symbol in SYMBOLS: |
| 43 | + symIndex = SYMBOLS.find(symbol) |
| 44 | + cipherText += SYMBOLS[(symIndex * keyA + keyB) % len(SYMBOLS)] |
| 45 | + else: |
| 46 | + cipherText += symbol |
| 47 | + return cipherText |
| 48 | + |
| 49 | +def decryptMessage(key, message): |
| 50 | + ''' |
| 51 | + >>> decryptMessage(4545, 'VL}p MM{I}p~{HL}Gp{vp pFsH}pxMpyxIx JHL O}F{~pvuOvF{FuF{xIp~{HL}Gi') |
| 52 | + 'The affine cipher is a type of monoalphabetic substitution cipher.' |
| 53 | + ''' |
| 54 | + keyA, keyB = getKeyParts(key) |
| 55 | + checkKeys(keyA, keyB, 'decrypt') |
| 56 | + plainText = '' |
| 57 | + modInverseOfkeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) |
| 58 | + for symbol in message: |
| 59 | + if symbol in SYMBOLS: |
| 60 | + symIndex = SYMBOLS.find(symbol) |
| 61 | + plainText += SYMBOLS[(symIndex - keyB) * modInverseOfkeyA % len(SYMBOLS)] |
| 62 | + else: |
| 63 | + plainText += symbol |
| 64 | + return plainText |
| 65 | + |
| 66 | +def getRandomKey(): |
| 67 | + while True: |
| 68 | + keyA = random.randint(2, len(SYMBOLS)) |
| 69 | + keyB = random.randint(2, len(SYMBOLS)) |
| 70 | + if cryptoMath.gcd(keyA, len(SYMBOLS)) == 1: |
| 71 | + return keyA * len(SYMBOLS) + keyB |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + import doctest |
| 75 | + doctest.testmod() |
| 76 | + main() |
0 commit comments