Pycrypto Possible To Check If File Already Aes Encrypted?
Solution 1:
The most often used solution is to write some "magic" string at the beginning of the encrypted file followed by the encrypted content. If that string is found when reading the file, further encryption is refused. For decription it is read to veryfiy that this is a file we encrypted, but otherwise it is ignored.
Imagine you're using "MyCrYpT" as the magic (although it doesn't matter what you use as long as it is reasonably unique.
magic = "MyCrYpT"# writing the encrypted filewithopen(out_filename, 'wb') as outfile:
outfile.write(magic) # write the identifier.
outfile.write(struct.pack('<Q', filesize)) # file size
outfile.write(iv)
# et cetera
Now, when reading the file, we read all the data, and then check if it is ours. Then we discard the magic and process the rest.
withopen(in_filename, 'rb') as infile:
data = infile.read()
if data[:len(magic)] != magic:
raise ValueError('Not an encrypted file')
filedata = data[len(magic):]
# Proces the file data
Solution 2:
Unless you have some kind of magical header that can be detected (for example, on Linux, LUKS encrypted disk image has a header block for added features, but DM-Crypt does not) it would be difficult to detect whether the input string is encrypted or not.
Post a Comment for "Pycrypto Possible To Check If File Already Aes Encrypted?"