term-output #4

Merged
Hesham merged 4 commits from term-output into master 2020-09-06 06:50:26 +02:00
Showing only changes of commit 66560b5f26 - Show all commits

159
rsa.py
View File

@ -15,7 +15,7 @@ import os
import sys import sys
import MillerRabin as mr import MillerRabin as mr
VERSION="1.2.1" VERSION="1.2.2"
keysFolder = "keys/" keysFolder = "keys/"
byteOrder = "little" byteOrder = "little"
N=0 N=0
@ -28,82 +28,87 @@ ID=6
def main(): def main():
print("hesham-rsa version ", VERSION) print("hesham-rsa version ", VERSION)
if len(sys.argv) > 1: if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen <keysize> <keyname>
if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen <keysize> <keyname> keyFileName = sys.argv[3]
keyFileName = sys.argv[3] key = generateKeys(keyFileName, int(sys.argv[2]))
key = generateKeys(keyFileName, int(sys.argv[2])) print("e: ", key[E])
print("e: ", key[E]) print("n: ", key[N])
print("n: ", key[N]) print("d: ", key[D])
print("d: ", key[D]) printKey(key)
printKey(key) try:
try: saveKeyFile(key, keyFileName)
saveKeyFile(key, keyFileName) except IOError:
except IOError: print("could not write file")
print("could not write file") sys.exit(1)
sys.exit(1) except Exception as ex:
except Exception as ex: print(ex)
print(ex) sys.exit(1)
sys.exit(1) sys.exit(0)
sys.exit(0) if sys.argv[1] == "encrypt" and len(sys.argv) == 5: ##rsa encrypt <message> <key> <signer>
if sys.argv[1] == "encrypt": ##rsa encrypt <message> <key> <signer> msg = sys.argv[2]
if len(sys.argv) == 5: msg_list = msg.split()
msg = sys.argv[2] keyName = sys.argv[3]
msg_list = msg.split() signing_key_name = sys.argv[4]
keyName = sys.argv[3] key = readKeyFile(keyName)
signing_key_name = sys.argv[4] signing_key = readKeyFile(signing_key_name)
key = readKeyFile(keyName) key_public = (key[N], key[E])
signing_key = readKeyFile(signing_key_name) msg_encrypted = ""
key_public = (key[N], key[E]) for word in msg_list:
msg_encrypted = "" msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public))
for word in msg_list: #msg_encrypted = encrypt(msg, key_public)
msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public)) print("Encrypted msg: \n", msg_encrypted)
#msg_encrypted = encrypt(msg, key_public) print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message.
print("Encrypted msg: \n", msg_encrypted) sys.exit(0)
print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message. elif sys.argv[1] == "encrypt":
else: print("Not enough arguments")
print("Not enough arguments") print("rsa encrypt <message> <key> <signer>")
print("rsa encrypt <message> <key> <signer>") sys.exit(1)
#Make help function if sys.argv[1] == "decrypt" and len(sys.argv) == 4: ##rsa decrypt "<cipher>" <key>
sys.exit(0) cipher = sys.argv[2]
if sys.argv[1] == "decrypt": ##rsa decrypt "<cipher>" <key> cipher_list = cipher.split()
if len(sys.argv) == 4: sig = verify(cipher_list)
cipher = sys.argv[2] del cipher_list[-1]
cipher_list = cipher.split() msg_decrypted = ""
sig = verify(cipher_list) key = readKeyFile(sys.argv[3])
del cipher_list[-1] for cipher_word in cipher_list:
msg_decrypted = "" msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N]))
key = readKeyFile(sys.argv[3]) if sig == None:
for cipher_word in cipher_list: print("\033[91mUnknown signature! \u2717" + "\033[0m")
msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N])) else:
if sig == None: print("Signed by: \033[92m " + sig + " \u2713\033[0m")
print("\033[91mUnknown signature! \u2717" + "\033[0m") print("Decrypted message: \n", msg_decrypted)
else: sys.exit(0)
print("Signed by: \033[92m " + sig + " \u2713\033[0m") elif sys.argv[1] == "decrypt":
print("Decrypted message: \n", msg_decrypted) print("Not enough arguments")
else: print("rsa decrypt \"<cipher>\" <keyid>")
print("Not enough arguments") sys.exit(1)
print("rsa decrypt \"<cipher>\" <keyid>") if sys.argv[1] == "list":
sys.exit(0) listKeys()
sys.exit(0)
if sys.argv[1] == "list": if sys.argv[1] == "export" and len(sys.argv) == 3: #rsa export <key>
listKeys() key_file_name = sys.argv[2]
sys.exit(0) exportKey(key_file_name)
if sys.argv[1] == "export": #rsa export <key> sys.exit(0)
if len(sys.argv) == 3: elif sys.argv[1] == "export":
key_file_name = sys.argv[2] printHelp()
exportKey(key_file_name) sys.exit(1)
sys.exit(0) if sys.argv[1] == "crack" and len(sys.argv) == 3: #rsa crack <key>
else: keyName = sys.argv[2]
print("Not enough arguments") cracked_key = crackKey2(keyName)
print("rsa export <keyid>") printKey(cracked_key)
if sys.argv[1] == "crack": #rsa crack <key> sys.exit(0)
keyName = sys.argv[2] elif sys.argv[1] == "crack":
cracked_key = crackKey2(keyName) printHelp()
printKey(cracked_key) sys.exit(1)
if sys.argv[1] == "print": #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]))
if sys.argv[1] == "help": sys.exit(0)
printHelp() elif sys.argv[1] == "print":
printHelp()
sys.exit(1)
if sys.argv[1] == "help":
printHelp()
sys.exit(0)
#No command exit code #No command exit code
printHelp() printHelp()