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 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 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 decryptMessage(key, message): keyA, keyB = getKeyParts(key) checkKeys(keyA, keyB, 'decrypt') plainText = '' modInverseOfKeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: # decrypt this symbol symIndex = SYMBOLS.find(symbol) plainText += SYMBOLS[(symIndex - keyB) * modInverseOfKeyA % len(SYMBOLS)] else: plainText += symbol # just append this symbol undecrypted return plainText
def decryptMessage(key, message): keyA, keyB = getKeyParts(key) checkKeys(keyA, keyB, 'decrypt') plaintext = '' modInverseOfKeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: #Decrypt the symbol: symbolIndex = SYMBOLS.find(symbol) plaintext = SYMBOLS[(symbolIndex - keyB) * modInverseOfKeyA % len(SYMBOLS)] else: plaintext += symbol # Append the symbol without decrypting return plaintext
def decryptMessage(key, message): keyA, keyB = getKeyParts(key) checkKeys(keyA, keyB, 'decrypt') plaintext = '' modInverseOfKeyA = cryptoMath.findModInverse(keyA, len(SYMBOLS)) for symbol in message: if symbol in SYMBOLS: # Symbol encryption symbolIndex = SYMBOLS.find(symbol) plaintext += SYMBOLS[(symbolIndex - keyB) * modInverseOfKeyA % len(SYMBOLS)] else: plaintext += symbol return plaintext
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 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)