يشرح في الدقيقة 20
التشفير وفك التشفير
الاول:- انشاء مفتاح خاص ومفتاح عام
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
#encryptedpass = "myverystrongpassword"
Generate an RSA Keys
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
print(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
))
public_key = private_key.public_key()
Save the RSA key in PEM format
with open("alikey.pem", "wb") as f:
f.write(private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
)
print()
print(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
))
Save the Public key in PEM format
with open("bob-pub.pem", "wb") as f:
f.write(public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
)
الثاني :- تشفير مفتاح عام برسالة مشفرة
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.primitives.serialization import load_pem_public_key
#encryptedpass = "myverystrongpassword"
plaintextMessage = b'Hello Yasser'
alicePubKey = load_pem_public_key(open('bob-pub.pem', 'rb').read(),default_backend())
ciphertext = alicePubKey.encrypt(
plaintextMessage,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(ciphertext.hex())
الثالث:- فك التشفير بواسطة المفتاح الخاص واظهار الرسالة المشفرة
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
with open("alikey.pem", "rb") as key_file:
alicePrivKey = serialization.load_pem_private_key(
key_file.read(),
password=None,
)
#alicePrivKey = load_pem_private_key(open('rsakey.pem', 'rb').read(),encryptedpass,default_backend())
x=bytes.fromhex('72451cfb9850058ef155d747f3a3adc6a3c8f5f2e55b8c68b33678e1fcad9e619fd41dcc5c830f001b4dbc5efbb3189cd82821e902b3d226851f230e7b4e8fc847ace113a19d823570c9a5c6a1f3d9058758009749bd32843afec8bc622fa0cb0f74d6281ac61e033af621664c7fc8cda9d3cdac9f35190a4f63f968f28f56fa0f9bb405cd375656dcef853daa55b1e0cc2fda52f8143224ffc9b9d8f66f4b52684290e70c463a5f7d5e0cfeae5b0d614f2f0192329d18f20e8f6514a612bb1b12bc9e7c29d99e8394dc1c27fbf7c2f0f62a283ac146d98ca112bf6f2413fc0212579976ebae1130179d7c9a534dd03da6c7f3a5577eea3a09cf1091d61984e2')
d = alicePrivKey.decrypt(
x,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(d)