def keyMaker(p, q, k): #Checking if inputs are prime if cryptoMath.gcd(p, q) != 1: return None rsaModulus = p * q euilersToitent = (p - 1) * (q - 1) e = 1 for i in range(euilersToitent - 1, 1, -1): if cryptoMath.gcd(i, rsaModulus) == 1: e = i break # Public Key = rsaModulus and e d = (k * euilersToitent + 1) // e return [[e, rsaModulus], [d, rsaModulus]]
def generateKey(keySize): # Creates a public/private key pair with keys that are keySize bits in # size. This function may take a while to run. # Step 1: Create two prime numbers, p and q. Calculate n = p * q. print('Generating p prime...') p = rabinMiller.generateLargePrime(keySize) print('Generating q prime...') q = rabinMiller.generateLargePrime(keySize) n = p * q # Step 2: Create a number e that is relatively prime to (p-1)*(q-1). print('Generating e that is relatively prime to (p-1)*(q-1)...') while True: # Keep trying random numbers for e until one is valid. e = random.randrange(2 ** (keySize - 1), 2 ** (keySize)) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break # Step 3: Calculate d, the mod inverse of e. print('Calculating d that is mod inverse of e...') d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) print('Public key:', publicKey) print('Private key:', privateKey) return (publicKey, privateKey)
def hackAffine(message): print 'Hacking...' # Python programs can be stopped at any time by pressing # Ctrl-C (on Windows) or Ctrl-D (on Mac and Linux) print '(Press Ctrl-C or Ctrl-D to quit at any time.)' # brute-force by looping through every possible key 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 'Tried Key %s: ... (%s)' % (key, decryptedText[:40]) if detectEnglish.isEnglish(decryptedText): # Check with the user if the decrypted key has been found. print '' print 'Possible encryption hack:' print 'Key: %s' % (key) print 'Decrypted message: ' + decryptedText[:200] print '' print 'Enter D for done, or just press Enter to continue hacking:' response = raw_input('> ') if response.strip().upper().startswith('D'): return decryptedText return None
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
def generateKey(self): # Creates a public/private key pair with keys that are keySize bits in # size. This function may take a while to run. # Step 1: Create two prime numbers, p and q. Calculate n = p * q. print('Generating p prime...') p = rabinMiller.generateLargePrime(self.keySize) print('Generating q prime...') q = rabinMiller.generateLargePrime(self.keySize) n = p * q # Step 2: Create a number e that is relatively prime to (p-1)*(q-1). print('Generating e that is relatively prime to (p-1)*(q-1)...') while True: # Keep trying random numbers for e until one is valid. e = random.randrange(2**(self.keySize - 1), 2**(self.keySize)) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break # Step 3: Calculate d, the mod inverse of e. print('Calculating d that is mod inverse of e...') d = cryptoMath.modinv(e, (p - 1) * (q - 1)) self.publicKey = (n, e) self.privateKey = (n, d) print('Public key:', self.publicKey) print('Private key:', self.privateKey) print()
def generateKey(keySize): p = 0 q = 0 print('Generating p prime...') while p == 1: p = primeNum.generaeLargePrime(keySize) q = primeNum.generaeLargePrime(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) print('Public key:', publicKey) print('Private key:', privateKey) return(publicKey, privateKey)
def hackAffine(message): print 'Hacking...' # Python programs can be stopped at any time by pressing # Ctrl-C (on Windows) or Ctrl-D (on Mac and Linux) print '(Press Ctrl-C or Ctrl-D to quit at any time.)' # brute-force by looping through every possible key 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 'Tried Key %s: ... (%s)' % (key, decryptedText[:40]) if detectEnglish.isEnglish(decryptedText): # Check with the user if the decrypted key has been found. print '' print 'Possible encryption hack:' print 'Key: %s' % (key) print 'Decrypted message: ' + decryptedText[:200] print '' print 'Enter D for done, or just press Enter to continue hacking:' response = raw_input('> ') if response.strip().upper().startswith('D'): return decryptedText return None
def checkKeys(keyA, keyB, mode): if keyA == 1 and mode == 'encrypt': sys.exit('The affine cipher becomes incredibly weak when key A is set to 1. Choose a different key.') if keyB == 0 and mode == 'encrypt': sys.exit('The affine cipher becomes incredibly weak when key B is set to 0. Choose a 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 main(): multi = MultiplicativeCipher(104729,13) cipherText = multi.encrypt("this is a super awesome year '2000' message!!!") print(cipherText) for i in range(95): for j in range(95): if cryptoMath.gcd(i,95) == 1: multi.setKeys(i,j) decoded = multi.decrypt(cipherText) print(decoded)
def main(): multi = MultiplicativeCipher(104729, 13) cipherText = multi.encrypt( "this is a super awesome year '2000' message!!!") print(cipherText) for i in range(95): for j in range(95): if cryptoMath.gcd(i, 95) == 1: multi.setKeys(i, j) decoded = multi.decrypt(cipherText) print(decoded)
def checkKeys(keyA, keyB, mode): if keyA == 1 and mode == 'encrypt': sys.exit('Cipher is weak if key A is 1. Choose a different key.') if keyB == 0 and mode == 'encrypt': sys.exit('Cipher is weak if key B is 0. Choose a 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. \n Choose a different Key.' % (keyA, len(SYMBOLS)))
def generateKey(keySize): p = 0 q = 0 while p == q: p = primeNum.generateLargePrime(keySize) q = primeNum.generateLargePrime(keySize) n = p * q while True: e = random.randrange(2**(keySize - 1), 2**keySize) if cryptoMath.gcd(e, (p - 1) * (q - 1)) == 1: break d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) return publicKey, privateKey
def checkKeys(keyA, keyB, mode): if keyA == 1 and mode == 'encrypt': sys.exit( 'The affine cipher becomes incredibly weak when key A is set to 1. Choose a different key.' ) if keyB == 0 and mode == 'encrypt': sys.exit( 'The affine cipher becomes incredibly weak when key B is set to 0. Choose a 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 createKey(keysize): p = 0 q = 0 e = 0 d = 0 print('finding p, q, and n') while q == p: q = primeNum.generateLargePrime(keysize=1024) p = primeNum.generateLargePrime(keysize=1024) n = p * q print('finding e') while cryptoMath.gcd(e, (p - 1) * (q - 1)) != 1: e = random.randrange(2**(keysize - 1), 2**(keysize)) print('finding d') d = cryptoMath.findModInverse(e, (p - 1) * (q - 1)) publicKey = (n, e) privateKey = (n, d) return (publicKey, privateKey)
def hackAffine(message): print('Hacking...') 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('Tried key %s... (%s)' % (key, decryptedText[:40])) if detectEnglish.isEnglish(decryptedText): print() print('Possible encryption hack:') print('Key: %s' % (key)) print('Decrypted message: ' + decryptedText[:200]) response = input('>') if response.strip().upper().startswith('D'): return decryptedText return None
# This program proves that the keyspace of the affine cipher is limited # to len(SYMBOLS) ^ 2. import affineCipher, cryptoMath message = 'Make things as simple as possible, but not simpler.' for keyA in range(2, 100): key = keyA * len(affineCipher.SYMBOLS) + 1 if cryptoMath.gcd(keyA, len(affineCipher.SYMBOLS)) == 1: print(keyA, affineCipher.encryptMessage(key, message))