From b33b4224277d8fd6dc36685ad289cc34b20372ac Mon Sep 17 00:00:00 2001 From: daniel156161 Date: Fri, 11 Dec 2020 16:24:27 +0100 Subject: [PATCH] First Code and Files --- Keys/readme.txt | 3 ++ PubKeys/daniel156161.pem | 14 +++++++++ msg_encypt_decrypt.py | 39 +++++++++++++++++++++++ mykeydef.py | 67 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 Keys/readme.txt create mode 100644 PubKeys/daniel156161.pem create mode 100644 msg_encypt_decrypt.py create mode 100644 mykeydef.py diff --git a/Keys/readme.txt b/Keys/readme.txt new file mode 100644 index 0000000..19dfb8d --- /dev/null +++ b/Keys/readme.txt @@ -0,0 +1,3 @@ +Randome Genaratet RSA Keys with 4096 Bit +Your Private Key and +Your Public Key diff --git a/PubKeys/daniel156161.pem b/PubKeys/daniel156161.pem new file mode 100644 index 0000000..b6fe56a --- /dev/null +++ b/PubKeys/daniel156161.pem @@ -0,0 +1,14 @@ +-----BEGIN PUBLIC KEY----- +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAyApJMTsyxFdRvYPbf+Uw +pbPj7Ip/cJH2OeBOmpLaPCAyMecKEDKEY0s8JrCuKYyhTI+qFeNNrrair9HRgv/3 +1Ukx7ULemCbC4JsWnWl724b9FqAYwxjaXBfJh9sm/qUdyPA85FoPVv33ob2mBUrq +S27phm0Xu9+bCVtEPDG66g+01YJPoay8aSVx1NyCZMlBfmf5X8odNO6WcNDhwA6N +hgQgGcNn6oy5yOw8mYY0F13Scck08UroZTtoP/BC0VhQg93dRg+869yKFJIfif2A +iPD73C+nCdARa7S5FfDP+5/Trfzw7KtraqA9WGvVltMaXu3YRuffglXbhI7pODzL +AN9HXZx9ygiur9nsQJin9PPvsqLvkaa30KlUbwPeMa7FgMQ6bMPJA2ieQHl7rGMm +6qqg/XgNBzGiKwubwan/rdvdw+gsvlxJByVbsCX1Lr9Ejm3aX19yydK18BIPqQ05 +u3KCfUba2zXLrTP87zZMGSYe+nlKnzOP19GcETR2U+75w7zpgYZT4srjraBE/qjA +3U8K3GTRbG/a/cI0w1BM1gRRhpBSF3dHcTUyqdLlvu1fRS0MF5sdeN3jKcmPD6gE +iWMMtWs5aOVhbg5M4/TksgV5Wi3xPrqXn3HfY0UsoMlt+PW1l07Cq/pkvhu7XkVW +XCvpR8I131QKsx+cbxNB8BECAwEAAQ== +-----END PUBLIC KEY----- diff --git a/msg_encypt_decrypt.py b/msg_encypt_decrypt.py new file mode 100644 index 0000000..a844ee6 --- /dev/null +++ b/msg_encypt_decrypt.py @@ -0,0 +1,39 @@ +import mykeydef as key +from time import sleep +import binascii +import string +import os + +private_key = 'Keys/private_key.pem' +public_key = 'Keys/public_key.pem' + +#Make Key if not exist +if not os.path.exists(private_key): + if not os.path.exists(public_key): + key.genkeys() + +#INPUT +text=input("Input Text/Encrypted Text: ") +if all(c in string.hexdigits for c in text) == True: + #Decrypt + keys=key.loadkeys(private_key,public_key) + text = binascii.unhexlify(text) + print("") + print(key.mdecode(text,keys[1]).decode()) +else: + user = input("Username: ") + user_public_key ='PubKeys/'+user+'.pem' + + if os.path.exists(user_public_key): + print("") + keys=key.loadkeys(private_key,user_public_key) + else: + print("User key Not Found!!!") + print("Use Own Private Key") + print("") + keys=key.loadkeys(private_key,public_key) + pass + #Encrypt + text = key.mencode(text,keys[0]) + print(binascii.hexlify(text).decode()) +sleep(10) \ No newline at end of file diff --git a/mykeydef.py b/mykeydef.py new file mode 100644 index 0000000..57e95ea --- /dev/null +++ b/mykeydef.py @@ -0,0 +1,67 @@ +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.asymmetric import padding + +def genkeys(): +#Gen Key Pair + private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=4096, + backend=default_backend() + ) + public_key = private_key.public_key() +#Store Private Key + pem = private_key.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption() + ) + with open('Keys/private_key.pem', 'wb') as f: + f.write(pem) +#Store Public Key + pem = public_key.public_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PublicFormat.SubjectPublicKeyInfo + ) + with open('Keys/public_key.pem', 'wb') as f: + f.write(pem) + return + +def loadkeys(private_key, public_key): + with open(private_key, "rb") as key_file: + private_key = serialization.load_pem_private_key( + key_file.read(), + password=None, + backend=default_backend() + ) + with open(public_key, "rb") as key_file: + public_key = serialization.load_pem_public_key( + key_file.read(), + backend=default_backend() + ) + return public_key,private_key + +def mencode(message, public_key): + message = message.encode() + encrypted = public_key.encrypt( + message, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None + ) + ) + return encrypted + +def mdecode(encrypted, private_key): + original_message = private_key.decrypt( + encrypted, + padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None + ) + ) + return original_message \ No newline at end of file