I want to encrypt file with simple AES encryption,here is my python3 source code.
TurboTax is one of the most trendsetting software programs, which is primarily focused on computing the taxes and returns. It lets the user e-file their returns, be it of individual or a company, quite hassle-freely. In fact, it searches over more than 350 tax deductions to let the user enjoy maximum benefits. Moreover, each and. First I got the following message: error code 2318 – Unable to find Unable to find Windows Installer. The second time I tried to do this again I got: Error Code.
It works fine for some files,encounter error info for some files such as below:
encrypt_file('qwertyqwertyqwer','/tmp/test1' , out_filename=None, chunksize=64*1024)
No error info,works fine.
encrypt_file('qwertyqwertyqwer','/tmp/test2' , out_filename=None, chunksize=64*1024)
How to fix my encrypt_file function?
Do as
t.m.adam
say ,to fix as
To try with some file.
scrapy
scrapyscrapy
2 Answers
The main issue with your code is that you're using strings. AES works with binary data, and if you were using PyCryptodome this code would raise a TypeError:
Pycrypto accepts strings, but encodes them to bytes internally, so it doesn't make sense to decode your bytes to string because it will be encoded back to bytes. Also, it encodes with ASCII (tested with PyCrypto v2.6.1, Python v2.7) and so, this code for example:
would raise a UnicodeEncodeError:
You should always use bytes when encrypting or decrypting data. Then you can decode the plaintext to string, if it is text.
The next issue is your padding method. It produces a string and so you're getting a TypeError when you try to apply it to the plaintext, which should be bytes. You can fix this if you pad with bytes,
but it would be best to use the PKCS7 padding (currently you're using zero padding, but with a space instead of a zero byte).
PyCryptodome provides padding functions, but it seems you're using PyCrypto. In this case, you could implement PKCS7 padding, or better yet copy PyCryptodome's padding functions.
The
pad
and unpad
functions were copied from Crypto.Util.Padding
and modified to use only PKCS7 padding. Note that when using PKCS7 padding it is important to pad the last chunk, even if its size is a multiple of the block size, otherwise you won't be able to unpad correctly. Applying those changes to the
encrypt_file
function, and the matching
decrypt_file
function, This code is Python2/Python3 compatible, and it should work either with PyCryptodome or with PyCrypto.
However, if you're using PyCrypto, I recommend updating to PyCryptodome. PyCryptodome is a fork of PyCrypto and it exposes the same API (so you won't have to change your code too much), plus some extra features: padding functions, Authenticated Encryption algorithms, KDFs etc. On the other hand, PyCrypto is not being maintained anymore and also, some versions suffer from a heap-based buffer overflow vulnerability: CVE-2013-7459.
t.m.adamt.m.adam11.8k33 gold badges1616 silver badges3737 bronze badges
In addition to the accepted answer, I believe showing multiple implementations of simple AES encryption can be useful for readers/new learners:
And illustrating examples of the above's usage:
Sean PiankaSean Pianka97811 gold badge1313 silver badges3030 bronze badges
Not the answer you're looking for? Browse other questions tagged pythonpython-3.xencryption or ask your own question.
I have a simple VBA routine I have used before but seem to keep returning Critical_Error 65535 ..this is my routine..it fails at the save part post generating the values only excel file ..any pointers please?
YowE3K22.7k88 gold badges2222 silver badges3535 bronze badges
Andy DonaldsonAndy Donaldson
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.