
You can simply substitute the values I previously used for message with the contents of a file. To encrypt and decrypt files, you will need to use read and write binary when opening files. SHA256 (), label = None ) ) # Checking the results > original_message b 'encrypt me!' > message = original_message True Encrypting and Decrypting Files SHA256 (), label = None ) ) > original_message = private_key. read (), backend = default_backend () ) # Encrypting and decrypting > from import hashes > from import padding > message = b 'encrypt me!' > encrypted = public_key. read (), password = None, backend = default_backend () ) > with open ( "public_key.pem", "rb" ) as key_file : public_key = serialization. write ( pem ) # Reading the keys back in (for demonstration purposes) > from import default_backend > from import serialization > with open ( "private_key.pem", "rb" ) as key_file : private_key = serialization. SubjectPublicKeyInfo ) > with open ( 'public_key.pem', 'wb' ) as f : f. NoEncryption () ) > with open ( 'private_key.pem', 'wb' ) as f : f. PKCS8, encryption_algorithm = serialization.

private_bytes ( encoding = serialization. public_key () # Storing the keys > from import serialization > pem = private_key. generate_private_key ( public_exponent = 65537, key_size = 2048, backend = default_backend () ) > public_key = private_key. RSA encryption in python Ask Question Asked 12 years, 1 month ago Modified 9 months ago Viewed 4k times 6 I decided to write a simple RSA encryption implementation in Python, but every time I run it it prints the error Inde圎rror: list out of range when it's decrypting and in findkey. # Generating a key > from import default_backend > from import rsa > private_key = rsa.
