First Code and Files
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
Randome Genaratet RSA Keys with 4096 Bit
|
||||
Your Private Key and
|
||||
Your Public Key
|
||||
@@ -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-----
|
||||
@@ -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)
|
||||
+67
@@ -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
|
||||
Reference in New Issue
Block a user