0

I've just written a few lines of code as example of file encryption/decryption using asymmetric crypthography (RSA). Nevertheless, I always receive the "RSA key format is not supported" error as it imports the public key. I think it's something related to encoding, but I've already tried to import that as "string" instead of "bytes" as well as replacing new lines with "\n". The keys are in PEM format.

from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
def encrypt(plain_content, file_publickey):
 with open(file_publickey, 'rb') as f:
 print(f.read())
 public_key = RSA.importKey(f.read())
 
 rsa_cipher = PKCS1.OAEP.new(public_key)
 return rsa_cipher.encrypt(plain_content.encode())
 
def decrypt(encrypted_content, file_privatekey):
 with open(file_privatekey, 'rb') as f:
 print(f.read())
 private_key = RSA.importKey(f.read())
 
 rsa_cipher = PKCS1_OAEP.new(private_key)
 return rsa_cipher.decrypt(encrypted_content)
 
def main():
 key = RSA.generate(4096)
 with open('public_key.pem', 'wb') as f:
 f.write(key.publickey().exportKey('PEM'))
 with open('private_key.pem', 'wb') as f:
 f.write(key.exportKey('PEM'))
 try:
 with open('test.pdf','rb') as fin:
 cipher_content = encrypt(fin.read(), 'public_key.pem')
 with open('test_enc.pdf','rw') as fout:
 fout.write(cipher_content)
 
 with open('test_enc.pdf','rb') as fin:
 decrypted_content = decrypt(fin.read(), 'private_key.pem')
 with open('test_dec.pdf','rb') as fout:
 fout.write(decrypted_content)
 
 except Exception as e:
 print(e)
 
if __name__ == "__main__":
 main()

This is the last public key generated by the code (I paste it below as example):

-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArMQMiEVSQk+TJMEBmVh1
DknyDELcS0Q3K0qtolUuIh5ZQzGkmQ3O7JCQmuhUtEOVBBWiyIAxyjvoLsTeOEEg
DUZ8Pn/UBV0rVMhZNFzVrquaHNE142VOHO3RKNjt8kp+FnfNzmlXnOXRRTKF/81W
Mo4315vZIHuhiLwfzH1oamKJJl4FRWe6b0Gofk6NE/pnCkywewXC+MIy/CvQhbqa
eTBUhERepa7DxOXQrpqSDwPn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yqEFIMBC8QjfqvwprnJMCQglXLx4dbThiJ+w8w01u/nJtS\ngxnkj4HkHN1GWidsIAPPedCubQS3KxEZMPIW9O+HXXq0RdvY0Zz/rOJzsuY6RY0B\nvX6L8buwbkemXRmM+NC+d9rL0Ak+LrZ+nL4N7/N1v08oD47xUH5WTYKEX2UCif/F\nKVggkFk8f3NePn6a7jOrzy8CAwEAAQ==\n-----END PUBLIC KEY-----'
RSA key format is not supported
asked Apr 8, 2024 at 7:53
4
  • 1
    You call f.read() twice for the key, once for output and once for import. With the second f.read(), the pointer is at the end of the file and nothing more is read, see Why can't I call read() twice on an open file?. Commented Apr 8, 2024 at 12:18
  • In addition, with RSA only a small amount of data can be encrypted (key size minus a padding-dependent value). For larger amounts of data, symmetric encryption or hybrid encryption (e.g. AES + RSA) is required. Commented Apr 8, 2024 at 12:24
  • Also check your file modes (some are not correct). Commented Apr 8, 2024 at 12:33
  • Thank you @Topaco, the issue was the "double" f.read(), I didn't know that. Commented Apr 8, 2024 at 13:33

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.