- Interface exit codes
	- Clean up
This commit is contained in:
HeshamTB 2020-04-19 15:45:51 +03:00
parent 611a6c45ea
commit e5a66e4d29
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 65 additions and 56 deletions

0
__init__.py Normal file
View File

View File

@ -1,4 +1,4 @@
#!/usr/bin/python3
s = 'test message hello awdawd' s = 'test message hello awdawd'
print(s) print(s)
@ -37,4 +37,4 @@ sig_enc = en.encrypt(sig,(n,e))
print(sig_enc) print(sig_enc)
sig_un = en.decrypt(sig_enc,d,n) sig_un = en.decrypt(sig_enc,d,n)
print(sig_un) print(sig_un)
print(key) print(key)

117
rsa.py
View File

@ -27,58 +27,66 @@ ID=6
def main(): def main():
if sys.argv[1] == "gen": ##rsa gen <keysize> <keyname> if len(sys.argv) > 1:
keyFileName = sys.argv[3] if sys.argv[1] == "gen": ##rsa gen <keysize> <keyname>
key = generateKeys(keyFileName, int(sys.argv[2])) keyFileName = sys.argv[3]
printKey(key) key = generateKeys(keyFileName, int(sys.argv[2]))
try: printKey(key)
saveKeyFile(key, keyFileName) try:
except IOError: saveKeyFile(key, keyFileName)
print("could not write file") except IOError:
exit(1) print("could not write file")
except Exception as ex: exit(1)
print(ex) except Exception as ex:
exit(1) print(ex)
if sys.argv[1] == "encrypt": ##rsa encrypt <message> <key> <signer> exit(1)
if len(sys.argv) == 5: exit(0)
msg = sys.argv[2] if sys.argv[1] == "encrypt": ##rsa encrypt <message> <key> <signer>
msg_list = msg.split() if len(sys.argv) == 5:
keyName = sys.argv[3] msg = sys.argv[2]
signing_key_name = sys.argv[4] msg_list = msg.split()
key = readKeyFile(keyName) keyName = sys.argv[3]
signing_key = readKeyFile(signing_key_name) signing_key_name = sys.argv[4]
key_public = (key[N], key[E]) key = readKeyFile(keyName)
msg_encrypted = "" signing_key = readKeyFile(signing_key_name)
for word in msg_list: key_public = (key[N], key[E])
msg_encrypted = msg_encrypted + " " + str(encrypt(word, key_public)) msg_encrypted = ""
#msg_encrypted = encrypt(msg, key_public) for word in msg_list:
print("Encrypted msg: \n", msg_encrypted) msg_encrypted = msg_encrypted + " " + str(encrypt(word, key_public))
print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message. #msg_encrypted = encrypt(msg, key_public)
else: print("Encrypted msg: \n", msg_encrypted)
print("Not enough arguments") print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message.
print("rsa encrypt <message> <key> <signer>") else:
#Make help function print("Not enough arguments")
if sys.argv[1] == "decrypt": ##rsa decrypt "<cipher>" <key> print("rsa encrypt <message> <key> <signer>")
if len(sys.argv) == 4: #Make help function
cipher = sys.argv[2] exit(0)
cipher_list = cipher.split() if sys.argv[1] == "decrypt": ##rsa decrypt "<cipher>" <key>
sig = verify(cipher_list) if len(sys.argv) == 4:
del cipher_list[-1] cipher = sys.argv[2]
msg_decrypted = "" cipher_list = cipher.split()
key = readKeyFile(sys.argv[3]) sig = verify(cipher_list)
for cipher_word in cipher_list: del cipher_list[-1]
msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word),key[D],key[N])) msg_decrypted = ""
print("Signed by: ", sig) key = readKeyFile(sys.argv[3])
print("Decrypted message: \n", msg_decrypted) for cipher_word in cipher_list:
else: msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word),key[D],key[N]))
print("Not enough arguments") print("Signed by: ", sig)
print("rsa decrypt \"<cipher>\" <key>") print("Decrypted message: \n", msg_decrypted)
else:
print("Not enough arguments")
print("rsa decrypt \"<cipher>\" <key>")
exit(0)
if sys.argv[1] == "list": if sys.argv[1] == "list":
listKeys() listKeys()
if sys.argv[1] == "export": #rsa export <key> exit(0)
key_file_name = sys.argv[2] if sys.argv[1] == "export": #rsa export <key>
exportKey(key_file_name) key_file_name = sys.argv[2]
exportKey(key_file_name)
exit(0)
#No command exit code
exit(127)
@ -219,8 +227,9 @@ def printKey(key):
n = key[N] n = key[N]
e = key[E] e = key[E]
d = key[D] d = key[D]
id = key[ID]
print("----------------------------------------------"+ print("----------------------------------------------"+
"\nID: {}".format(key[ID]) + "\nID: {}".format(id) +
"\n{}-BIT KEY".format(n.bit_length())+ "\n{}-BIT KEY".format(n.bit_length())+
"\nPUBLIC PART:"+ "\nPUBLIC PART:"+
"\n{0}/{1}".format(hex(n), hex(e))+ "\n{0}/{1}".format(hex(n), hex(e))+
@ -247,8 +256,8 @@ def listKeys():
def exportKey(keyFileName): def exportKey(keyFileName):
key = readKeyFile(keyFileName) key = readKeyFile(keyFileName)
public_key = (key[N], key[E],0,0,0,0,key[ID]) public_key = (key[N], key[E], 0, 0, 0, 0, key[ID])
saveKeyFile(public_key, key[ID]+"-public") saveKeyFile(public_key, key[ID]+"-public")
print("Saved public form of key {}".format(key[ID])) print("Saved public form of key {} in keys folder".format(key[ID]))
if __name__ == "__main__": if __name__ == "__main__":
main() main()