Added is_prime command

- Usage: rsa <is_prime|is_prime_mr> <base> <N>

	Print if N is prime or not using traditainl method and
	Miller-Rabin method.
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-10-19 18:04:44 +03:00
parent 522e9f3453
commit 6d9e071aa4
Signed by: Hesham
GPG Key ID: 74876157D199B09E

32
rsa.py Executable file → Normal file
View File

@ -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 <base> <N>
isPrime_cmd(0)
sys.exit(0)
if sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4: #rsa is_prime_mr <base> <N>
isPrime_cmd(1)
sys.exit(0)
if sys.argv[1] == "print" and len(sys.argv) == 3: #rsa print <key>
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: