Last active
April 11, 2023 06:45
-
-
Save akmamun/1487270197bac2b6602ccbb57b930898 to your computer and use it in GitHub Desktop.
Public/Privet key encryption decryption with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives import hashes, serialization | |
from cryptography.hazmat.primitives.asymmetric import rsa, padding | |
KEY_SIZE = 2048 | |
PASSWORD = b'1234546' # bytes of string | |
PRIVET_KEY_ENCODE = b'LS0tLS1CRUdJTiBFTkNSWVBURUQgUFJJVkFURSBLRVktLS0tLQpNSUlGSFRCWEJna3Foa2lHOXcw\nQkJRMHdTakFwQmdrcWhraUc5dzBCQlF3d0hBUUl4VEhpRWl6RUZ1VUNBZ2dBCk1Bd0dDQ3FHU0li\nM0RRSUpCUUF3SFFZSllJWklBV1VEQkFFcUJCREhWREs0NHp1enRDOVZGNlRoZ0pIMkJJSUUKd05E\nckR0MWpRaVBPWEFMaHhLVFJIRW9nVE9OS25tN1R0MVJ2b2hvc2FqWEdKYTByUUJxRXJFZlFBUjA4\naHJRdwp1anJFM3g1aUw3eGNRQmV2eWczMnYyWHBBYXBoczZKRjJoNklibUxabE0rU29mMmpMbVJv\ndzNOZ0x4ODg4c3ZRCm94V25WZXpGMCtZY0VIWHZKd3NvbkRmOFEvRHA3b08vdi9ndm1MMkpqcVl5\nZVh0LzlEanMyRVpRQzVQbTJIekcKR0dLSVBaRk9lWDdCWkcybFp0Q2RPWDZEcmJXdk9STjkwbmxF\nc1ZsOVVhOWlPVVk1OTNreFYwS3M0QUhUekJOQwpjcFhUaHp6R1dOckp4R08wMjV2TnZUcTNqQUZ0\ndXdyMXowV2NpNmZsdktHQUxIVkxya2JadEhYU2kzK1lLSWlkCkJqUXcyRkh2OURkY25hZ1NCSFYz\nZnhPNm14S1Jad0JXcGVpRDlzSEVjQnZvZnJJOUNKUFJ6RGVGRndNVUJKZG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2pPVGIy\nUnFyM0Z3TklNYkVDNGNwOS9VcHZ2UW42a1NsQUtmMncyd2prY0E2Z0wzWHVicUhkcVBDcmZjcG4x\nKwpmT1RJdWZZY3ZkQ1hZVlhkeExRU1locnZYU1c4RjFuVWxFbzJKaHE3VGx0b0dweFRLb1dRUm9W\nR1l1NDdyS2szClEzdlljYldZVisvNys4M3MyYk1aaXhpWGJhSlcrVVpJRlo2YmV4ZEEwRHpHS2V4\nSjBUcHdETkM0QTcwWlJuRnkKWVFJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==\n' | |
key = rsa.generate_private_key( | |
backend=default_backend(), | |
public_exponent=65537, | |
key_size=2048 | |
) | |
private_pem = key.private_bytes( | |
encoding=serialization.Encoding.PEM, | |
format=serialization.PrivateFormat.PKCS8, | |
encryption_algorithm=serialization.NoEncryption() | |
) | |
public_key = key.public_key() | |
privet_pem = public_key.public_bytes( | |
encoding=serialization.Encoding.PEM, | |
format=serialization.PublicFormat.SubjectPublicKeyInfo | |
) | |
message = "data" | |
public_key = serialization.load_pem_public_key( | |
base64.decodebytes(PUBLIC_KEY_ENCODE), | |
backend=default_backend() | |
) | |
privet_key = serialization.load_pem_private_key( | |
base64.decodebytes(PRIVET_KEY_ENCODE), | |
password=PASSWORD, | |
backend=default_backend() | |
) | |
ciphertext = public_key.encrypt(message.encode(), padding.OAEP( | |
mgf=padding.MGF1(algorithm=hashes.SHA256()), | |
algorithm=hashes.SHA256(), | |
label=None | |
)) | |
plaintext = privet_key.decrypt( | |
ciphertext, | |
padding.OAEP( | |
mgf=padding.MGF1(algorithm=hashes.SHA256()), | |
algorithm=hashes.SHA256(), | |
label=None | |
) | |
).decode() | |
print(plaintext) |
Author
akmamun
commented
Apr 11, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment