Added commands: genrand, genprime

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-10-19 18:23:01 +03:00
parent ec2bbf116b
commit b034158d2f
Signed by: Hesham
GPG Key ID: 74876157D199B09E

14
rsa.py
View File

@ -118,6 +118,12 @@ def main():
if sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4: #rsa is_prime_mr <base> <N> if sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4: #rsa is_prime_mr <base> <N>
isPrime_cmd(1) isPrime_cmd(1)
sys.exit(0) sys.exit(0)
if sys.argv[1] == "genrand" and len(sys.argv) == 3: #rsa genrand <bits>
print(gen_random(int(sys.argv[2])))
sys.exit(0)
if sys.argv[1] == "genprime" and len(sys.argv) == 3: #rsa genprime <bits>
print(getPrime(int(sys.argv[2])))
sys.exit(0)
if sys.argv[1] == "print" and len(sys.argv) == 3: #rsa print <key> if sys.argv[1] == "print" and len(sys.argv) == 3: #rsa print <key>
printKey(readKeyFile(sys.argv[2])) printKey(readKeyFile(sys.argv[2]))
sys.exit(0) sys.exit(0)
@ -190,9 +196,9 @@ def getPrime(bits):
x = int.from_bytes(os.urandom(int(bits/8)), byteOrder) x = int.from_bytes(os.urandom(int(bits/8)), byteOrder)
print('Trying: ', x, end="\n") print('Trying: ', x, end="\n")
if mr.is_prime(x): if mr.is_prime(x):
print("\nprime: ", x) print("\nprime: ", x, '\n')
return x return x
backTrack(x) #backTrack(x)
def isPrime(number): def isPrime(number):
@ -217,6 +223,10 @@ def isPrime(number):
return False return False
return True return True
def gen_random(bits: int):
x = int.from_bytes(os.urandom(int(bits/8)), byteOrder)
return x
def sign(encrypted_msg, key): def sign(encrypted_msg, key):
enc_msg = str(encrypted_msg) enc_msg = str(encrypted_msg)
encrypted_msg_list = enc_msg.split() encrypted_msg_list = enc_msg.split()