a small library to easily encrypt/decrypt with RSA public/private key pair.
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -out public.pem -pubout -outform PEM
git clone https://github.com/vspaz/rsa-encrypt-decrypt-python.git
cd rsa-encrypt-decrypt-python
pip3 install .
from cryptolib import serializers
def encrypt_message(message_to_encrypt: str) -> bytes:
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxGDcSAjiHKP9v2ITR+Bj
Qmt9Tx2zW08ZyrjOxPew+Gxl2m5zJyoP8sicZV81BeMNFkMg6q7sMtRXHhX1nFiT
ql5HBIqhZohYlN3LIXK2bdPWpDttrOFXfsSbZ4Wqy3XhXBhiPNn3kkkRv1N5L/IY
cdrxwqaqvTlJzOeQnDsd3+AmkYstuD4rgElOFkcUawtF7lKIYYFi42cYkJo51UD4
60mYieBezP6dZhFZB56pZ2rV8cQUNrUQy2llpj+PxX/yhGnYI88ij0FST0gI2l4U
sjtwXVB1Y2SxqrhNMdBU7W6ZA8WUQQidr4MBxEFoujsLjaCl8LMsbEpAAilKezwu
bQIDAQAB
-----END PUBLIC KEY-----
"""
encoder = serializers.Encoder(public_key=public_key)
encrypted_message = encoder.encrypt(unencrypted_text=message_to_encrypt)
return encoder.to_base_85(serializable=encrypted_message)
def decrypt_message(message_to_decrypt: bytes) -> str:
private_key = """-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAxGDcSAjiHKP9v2ITR+BjQmt9Tx2zW08ZyrjOxPew+Gxl2m5z
JyoP8sicZV81BeMNFkMg6q7sMtRXHhX1nFiTql5HBIqhZohYlN3LIXK2bdPWpDtt
rOFXfsSbZ4Wqy3XhXBhiPNn3kkkRv1N5L/IYcdrxwqaqvTlJzOeQnDsd3+AmkYst
uD4rgElOFkcUawtF7lKIYYFi42cYkJo51UD460mYieBezP6dZhFZB56pZ2rV8cQU
NrUQy2llpj+PxX/yhGnYI88ij0FST0gI2l4UsjtwXVB1Y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3bEHHCnTHW9n3+mwOaVs3uLLQynVRzBHT8zGudbyvFZwk9A+aZ
5xENAoGBALO842ymiZmFYiv9CIdfBGFokbokQMci+4cJm4wWzxEiDjAzSglRHAei
/+oGBiPm8mKmx/dcU408x4PK76JlfduuoXuzE9jEmx46kwU4jGDS1GZYkwjGVPY8
8UmZ7fFkjNFJH0Rh5y+tmoFyou3FsWzL2lpd1mIryAH2LR3PGE/t
-----END RSA PRIVATE KEY-----"""
decoder = serializers.Decoder(private_key=private_key)
base85_decoded_message = decoder.from_base_85(deserializable=message_to_decrypt)
return decoder.decrypt(encrypted_text=base85_decoded_message).decode()
def main():
initial_message = "some text goes here"
encrypted_message = encrypt_message(message_to_encrypt=initial_message)
decrypted_message = decrypt_message(message_to_decrypt=encrypted_message)
assert decrypted_message == initial_message
if __name__ == "__main__":
main()
NOTE: please refer to:
- rsa-encrypt-decrypt-golang for the Go lib
- rsa-encrypt-decrypt-rust for the Rust lib