def checkKeys(keyA, keyB, mode): 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') if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: sys.exit('Key A must be greater than 0 and key B must be between 0 and %s.' % (len(SYMBOLS) - 1)) if cryptoMath.gcd(keyA, len(SYMBOLS)) != 1: sys.exit('Key A %s and the symbol set size %s are not relatively prime. Choose a different key.' % (keyA, len(SYMBOLS)))
def check_keys(keyA, keyB, mode): if mode == "encrypt": if keyA == 1: sys.exit("The affine cipher becomes weak when key " "A is set to 1. Choose different key") if keyB == 0: sys.exit("The affine cipher becomes weak when key " "B is set to 0. Choose different key") if keyA < 0 or keyB < 0 or keyB > len(SYMBOLS) - 1: sys.exit("Key A must be greater than 0 and key B must " f"be between 0 and {len(SYMBOLS) - 1}.") if cryptomath.gcd(keyA, len(SYMBOLS)) != 1: sys.exit(f"Key A {keyA} and the symbol set size {len(SYMBOLS)} " "are not relatively prime. Choose a different key.")
def generateKey(keySize): print('Generating prime p...') p = rabinMiller.generateLargePrime(keySize) print('Generating prime q...') q = rabinMiller.generateLargePrime(keySize) n = p * q print('Generating e that is relatively prime to (p - 1) * (q - 1)...') while True: e = random.randrange(2 ** (keySize - 1), 2 ** (keySize)) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break print('Calculating d that is mod inverse of e...') d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) return (publicKey, privateKey)
def hackAffine(message): print('Hacking...\n[Press CTRL + C or CTRL + D to quit at any time]') for key in range(len(affineCipher.SYMBOLS) ** 2): keyA = affineCipher.getKeyParts(key)[0] if cryptoMath.gcd(keyA, len(affineCipher.SYMBOLS)) != 1: continue decryptedText = affineCipher.decryptMessage(key, message) if not SILENT_MODE: print('Key #%s: %s...' % (key, decryptedText[:40])) if detectEnglish.isEnglish(decryptedText): print('\nPossible encryption hack:') print('Key #%s: %s...' % (key, decryptedText[:40])) print("Enter 'D' for done, or just press 'Enter' to continue hacking:") response = input('> ') if response.strip().upper().startswith('D'): return decryptedText return None
# Affine Key Test # Source: http://inventwithpython.com/hacking (BSD Licensed) # Modified by: Harshil Darji (github.com/H-Darji) # This program proves that the keyspace of the affine cipher is limited to len(SYMBOLS) ^ 2. import affine_cipher as affineCipher, cryptomath_module as cryptoMath message = 'Make things as simple as possible, but not simpler.' print('Message: %s\nEncryption using key,' % message) for keyA in range(2, 100): key = keyA * len(affineCipher.SYMBOLS) + 1 if cryptoMath.gcd(keyA, len(affineCipher.SYMBOLS)) == 1: print('%s | %s' % (keyA, affineCipher.encryptMessage(key, message)))
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