From 6d9e071aa493062bb17dced5197f17968ecf4b66 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 19 Oct 2022 18:04:44 +0300 Subject: [PATCH] Added is_prime command - Usage: rsa Print if N is prime or not using traditainl method and Miller-Rabin method. Signed-off-by: HeshamTB --- rsa.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) mode change 100755 => 100644 rsa.py diff --git a/rsa.py b/rsa.py old mode 100755 new mode 100644 index 2a0c537..1629339 --- a/rsa.py +++ b/rsa.py @@ -112,6 +112,12 @@ def main(): elif sys.argv[1] == "crack": printHelp() sys.exit(1) + if sys.argv[1] == "is_prime" and len(sys.argv) == 4: #rsa is_prime + isPrime_cmd(0) + sys.exit(0) + if sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4: #rsa is_prime_mr + isPrime_cmd(1) + sys.exit(0) if sys.argv[1] == "print" and len(sys.argv) == 3: #rsa print printKey(readKeyFile(sys.argv[2])) sys.exit(0) @@ -236,6 +242,32 @@ def verify(cipher_list): else: continue else: return None +def isPrime_cmd(func): + number = 0 + base = 0 + try: + base = int(sys.argv[2]) + except ValueError as e: + print(f'Value {sys.argv[2]} is not a valid base (2, 8, 10, 16)', + file=sys.stderr) + + try: + number = int(sys.argv[3], base) + except ValueError as e: + print(f'Value {sys.argv[3]} is not valid for as a base {base} number', + file=sys.stderr) + + if func == 0: + prime = isPrime(number) + elif func == 1: + prime = mr.is_prime(number) + + if prime: + print('Prime') + print(number) + else: + print('Not prime') + def readKeyFile(keyName): key = tuple() with open(keysFolder+keyName, "r") as keyFile: