mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 05:21:09 +00:00
6acd7fb5ce
* Wrap lines that go beyond GiHub Editor * flake8 --count --select=E501 --max-line-length=127 * updating DIRECTORY.md * Update strassen_matrix_multiplication.py * fixup! Format Python code with psf/black push * Update decision_tree.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
import os
|
|
import random
|
|
import sys
|
|
|
|
import cryptomath_module as cryptoMath
|
|
import rabin_miller as rabinMiller
|
|
|
|
|
|
def main():
|
|
print("Making key files...")
|
|
makeKeyFiles("rsa", 1024)
|
|
print("Key files generation successful.")
|
|
|
|
|
|
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 makeKeyFiles(name, keySize):
|
|
if os.path.exists("%s_pubkey.txt" % (name)) or os.path.exists(
|
|
"%s_privkey.txt" % (name)
|
|
):
|
|
print("\nWARNING:")
|
|
print(
|
|
'"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n'
|
|
"Use a different name or delete these files and re-run this program."
|
|
% (name, name)
|
|
)
|
|
sys.exit()
|
|
|
|
publicKey, privateKey = generateKey(keySize)
|
|
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
|
with open("%s_pubkey.txt" % name, "w") as out_file:
|
|
out_file.write("{},{},{}".format(keySize, publicKey[0], publicKey[1]))
|
|
|
|
print("Writing private key to file %s_privkey.txt..." % name)
|
|
with open("%s_privkey.txt" % name, "w") as out_file:
|
|
out_file.write("{},{},{}".format(keySize, privateKey[0], privateKey[1]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|