2

i want to encrypt a file with public key in python, Asymmetric methode im really use cryptography biblio and PyCrypto but all algorithm encrypt short text not big file i need solution for my problem , in cryptography biblio i use hazmat module . i converted my file to string format , so this is result :

'Data too long for key size. Encrypt less data or use a ValueError: Data too long for key size. Encrypt less data or use a larger key size.'

def Encrypt(file,public_key):
    encrypted = public_key.encrypt(
        file,
        padding.OAEP(
            mgf=padding.MGF1(algorithm=hashes.SHA256()),
            algorithm=hashes.SHA256(),
            label=None
        )
    )
    f = open('myEncreptedFile.txt', 'wb')
    f.write(encrypted)
    f.close()
SyntaxVoid
  • 2,501
  • 2
  • 15
  • 23
  • 1
    asymmetric crypto should not be used this way! if you need to encrypt a large amount of data. you create a symmetric 'session key', encrypt the data (symmetrically) with that key and encrypt that session key with you asymmetric crypto system. that is not only way faster but also way more secure. https://en.wikipedia.org/wiki/Hybrid_cryptosystem – hiro protagonist Apr 15 '19 at 17:04
  • ok, you use optimal padding (OAEP). i retract my comment about it being less secure... i stand by the rest of my comment though. – hiro protagonist Apr 15 '19 at 17:06
  • Is this homework? Why do you want to encrypt a file with Asymmetric encryption? What is your real aim? do you want to send this file or just store? – kelalaka Apr 16 '19 at 17:47
  • kelalaka : i need to use this method for my learning project (Master PFE ) . i need to transfer identity file in network max (5mb) – Samed Hamma Apr 26 '19 at 18:26

0 Answers0