Merge branch 'master' into file-sys
This commit is contained in:
commit
0cbbc77976
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,3 +1,8 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
keys/
|
keys/
|
||||||
test-file
|
test-file
|
||||||
|
|
||||||
|
#pyinstaller files
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.spec
|
||||||
|
23
README.md
Normal file
23
README.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
# hesham-rsa
|
||||||
|
A simple program written in python to implement RSA public encryption. Used as Course EE-305 project KAU.
|
||||||
|
# Installation
|
||||||
|
There is no packaging or installation. Works in current dir (portable) for now.
|
||||||
|
|
||||||
|
git clone https://apollo-server.ddns.net/gitea/Hesham/hesham-rsa.git
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
./rsa.py
|
||||||
|
or
|
||||||
|
|
||||||
|
python rsa.py
|
||||||
|
|
||||||
|
## generating keys
|
||||||
|
to generate a key pair with and ID
|
||||||
|
|
||||||
|
./rsa.py [keysize] [keyID]
|
||||||
|
All generated keys are {working dir}/saves/ in keys folder
|
||||||
|
## encrypting
|
||||||
|
|
||||||
|
./rsa.py encrypt "[message]" [RecieverKeyID] [SenderSignetureKeyID]
|
44
rsa.py
Executable file → Normal file
44
rsa.py
Executable file → Normal file
@ -15,6 +15,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import MillerRabin as mr
|
import MillerRabin as mr
|
||||||
|
|
||||||
|
VERSION="1.2.1"
|
||||||
keysFolder = "keys/"
|
keysFolder = "keys/"
|
||||||
byteOrder = "little"
|
byteOrder = "little"
|
||||||
N=0
|
N=0
|
||||||
@ -28,7 +29,7 @@ FILE_HEADER = "---------- START OF RSA ENCRYPTED MESSAGE ----------"
|
|||||||
FILE_FOOTER = "---------- END OF RSA ENCRYPTED MESSAGE ----------"
|
FILE_FOOTER = "---------- END OF RSA ENCRYPTED MESSAGE ----------"
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
print("hesham-rsa version ", VERSION)
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if sys.argv[1] == "gen": ##rsa gen <keysize> <keyname>
|
if sys.argv[1] == "gen": ##rsa gen <keysize> <keyname>
|
||||||
keyFileName = sys.argv[3]
|
keyFileName = sys.argv[3]
|
||||||
@ -41,11 +42,11 @@ def main():
|
|||||||
saveKeyFile(key, keyFileName)
|
saveKeyFile(key, keyFileName)
|
||||||
except IOError:
|
except IOError:
|
||||||
print("could not write file")
|
print("could not write file")
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex)
|
print(ex)
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "encrypt": ##rsa encrypt <message> <key> <signer>
|
if sys.argv[1] == "encrypt": ##rsa encrypt <message> <key> <signer>
|
||||||
if len(sys.argv) == 5:
|
if len(sys.argv) == 5:
|
||||||
msg = sys.argv[2]
|
msg = sys.argv[2]
|
||||||
@ -67,7 +68,7 @@ def main():
|
|||||||
print("Not enough arguments")
|
print("Not enough arguments")
|
||||||
print("rsa encrypt <message> <key> <signer>")
|
print("rsa encrypt <message> <key> <signer>")
|
||||||
#Make help function
|
#Make help function
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "decrypt": ##rsa decrypt "<cipher>" <key>
|
if sys.argv[1] == "decrypt": ##rsa decrypt "<cipher>" <key>
|
||||||
if len(sys.argv) == 4:
|
if len(sys.argv) == 4:
|
||||||
cipher = sys.argv[2]
|
cipher = sys.argv[2]
|
||||||
@ -79,22 +80,26 @@ def main():
|
|||||||
for cipher_word in cipher_list:
|
for cipher_word in cipher_list:
|
||||||
msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N]))
|
msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N]))
|
||||||
if sig == None:
|
if sig == None:
|
||||||
print("Unknown signature!")
|
print("\033[91mUnknown signature! \u2717" + "\033[0m")
|
||||||
else:
|
else:
|
||||||
print("Signed by: ", sig)
|
print("Signed by: \033[92m " + sig + " \u2713\033[0m")
|
||||||
print("Decrypted message: \n", msg_decrypted)
|
print("Decrypted message: \n", msg_decrypted)
|
||||||
else:
|
else:
|
||||||
print("Not enough arguments")
|
print("Not enough arguments")
|
||||||
print("rsa decrypt \"<cipher>\" <key>")
|
print("rsa decrypt \"<cipher>\" <keyid>")
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if sys.argv[1] == "list":
|
if sys.argv[1] == "list":
|
||||||
listKeys()
|
listKeys()
|
||||||
exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "export": #rsa export <key>
|
if sys.argv[1] == "export": #rsa export <key>
|
||||||
key_file_name = sys.argv[2]
|
if len(sys.argv) == 3:
|
||||||
exportKey(key_file_name)
|
key_file_name = sys.argv[2]
|
||||||
exit(0)
|
exportKey(key_file_name)
|
||||||
|
sys.exit(0)
|
||||||
|
else:
|
||||||
|
print("Not enough arguments")
|
||||||
|
print("rsa export <keyid>")
|
||||||
if sys.argv[1] == "crack": #rsa crack <key>
|
if sys.argv[1] == "crack": #rsa crack <key>
|
||||||
keyName = sys.argv[2]
|
keyName = sys.argv[2]
|
||||||
cracked_key = crackKey2(keyName)
|
cracked_key = crackKey2(keyName)
|
||||||
@ -106,7 +111,7 @@ def main():
|
|||||||
|
|
||||||
#No command exit code
|
#No command exit code
|
||||||
printHelp()
|
printHelp()
|
||||||
exit(127)
|
sys.exit(127)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -140,10 +145,10 @@ def encrypt(message, publicKey):
|
|||||||
msg_text = message
|
msg_text = message
|
||||||
n = publicKey[N]
|
n = publicKey[N]
|
||||||
e = publicKey[E]
|
e = publicKey[E]
|
||||||
print("using n: {0}, e: {1}".format(n, e))
|
#print("using n: {0}, e: {1}".format(n, e))
|
||||||
|
|
||||||
msg_number_form = int.from_bytes(msg_text.encode(), byteOrder)
|
msg_number_form = int.from_bytes(msg_text.encode(), byteOrder)
|
||||||
print("Word: %s or %d" % (msg_text, msg_number_form))
|
#print("Word: %s or %d" % (msg_text, msg_number_form))
|
||||||
|
|
||||||
msg_encrypted_number_form = pow(msg_number_form, e, n) # c = msg^e mod n
|
msg_encrypted_number_form = pow(msg_number_form, e, n) # c = msg^e mod n
|
||||||
return msg_encrypted_number_form
|
return msg_encrypted_number_form
|
||||||
@ -156,7 +161,8 @@ def decrypt(cipher, privateKey, n):
|
|||||||
try:
|
try:
|
||||||
msg_decrypted = str(msg_decrypted.to_bytes(msg_decrypted.bit_length(), byteOrder).decode()).strip()
|
msg_decrypted = str(msg_decrypted.to_bytes(msg_decrypted.bit_length(), byteOrder).decode()).strip()
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
print("Cant decrypt properly")
|
#print("decrypt: Cant decrypt properly")
|
||||||
|
return ""
|
||||||
return msg_decrypted
|
return msg_decrypted
|
||||||
|
|
||||||
def getPrime(bits):
|
def getPrime(bits):
|
||||||
@ -203,7 +209,6 @@ def sign(encrypted_msg, key):
|
|||||||
return signed_msg.strip()
|
return signed_msg.strip()
|
||||||
|
|
||||||
def verify(cipher_list):
|
def verify(cipher_list):
|
||||||
sig = "Unknown"
|
|
||||||
local_keys = os.listdir(keysFolder)
|
local_keys = os.listdir(keysFolder)
|
||||||
cipher_list.reverse() #To get last word using index 0
|
cipher_list.reverse() #To get last word using index 0
|
||||||
encrypted_sig = cipher_list[0]
|
encrypted_sig = cipher_list[0]
|
||||||
@ -215,7 +220,8 @@ def verify(cipher_list):
|
|||||||
sig = str(decrypt(int(encrypted_sig, 16), key[E], key[N]))
|
sig = str(decrypt(int(encrypted_sig, 16), key[E], key[N]))
|
||||||
if "sig:" in sig:
|
if "sig:" in sig:
|
||||||
return sig.replace("sig:","")
|
return sig.replace("sig:","")
|
||||||
else: return sig
|
else: continue
|
||||||
|
else: return None
|
||||||
|
|
||||||
def readKeyFile(keyName):
|
def readKeyFile(keyName):
|
||||||
key = tuple()
|
key = tuple()
|
||||||
|
Loading…
Reference in New Issue
Block a user