3

For some sensitive data I decided to store it AES-encrypted on disc. I've implemented the encryption using PyCrypto.

Furthermore, the data is important, and the stored encrypted data will be my only copy of it (backups aside), so I looked for some means of retrieving the data without using PyCrypto to have a fallback given the possibility that PyCrypto is not longer available to me (for whatever reason that may be).

I thought mcrypt could be an option.

This is my test case to get some ciphertext written:

import Crypto.Cipher.AES
import sys

pwd  = 'qwertzuiopasdfgh'
mode = Crypto.Cipher.AES.MODE_CBC
aes  = Crypto.Cipher.AES.new( pwd, mode )
text = 'asdfghjklyxcvbnm'
sys.stdout.write( aes.encrypt( text ) )

I redirected the output to a file out.nc and tried decryption by

mcrypt -d -b -k qwertzuiopasdfgh -a rijndael-128 -m CBC out.nc

but the resulting file out has zero bytes size, unfortunately.

I hope there is a combination of options to mcrypt to make this work…

mkluwe
  • 3,823
  • 2
  • 28
  • 45
  • 1
    What happens if you write the result of aes.encrypt(text) to a file, rather than stdout? Use diff to make sure they're the same. Piping ciphertext to stdout is weird because it'll never be printable data. And if you're unlucky, redirecting it won't even work. – ojrac Mar 25 '10 at 00:04
  • Redirecting won't even work? Strange thought, at a first glance. Do you have any references about why that may be the case? Nevertheless, I just tried writing to a file directly. That yields the same file. – mkluwe Mar 26 '10 at 07:27

2 Answers2

1

I think the problem may lay in the fact that you don't supply an IV for CBC mode and without an IV maybe mCrypt and PyCrypto handle it differently by using different default IVs. I have seen some implementations (phpseclib for instance) use and IV of 16 null bytes by default. mcrypt might not do this.

Eric Johnson
  • 202
  • 1
  • 3
  • 11
-1

Why is it important to be able to recover without PyCrypto? You can simply fire up a VM with the old OS and the old release of PyCrypto, export your data, and re-encrypt with a different algorithm and implementation.

vy32
  • 28,461
  • 37
  • 122
  • 246
  • 3
    Well, let's assume that PyCrypto is no longer supported by my Linux Distribution, the associated web site is switched off and I can't get the source to compile on my system. Bad situation for my valuable but encrypted data. Additionally, I'd like to verify that my encryption worked as I expected up front. I've seen that there are a good number of combinations to do AES, so I cannot take that for granted. Regarding other solutions: Yes, OpenSSL looks like a decent candidate. I've yet to find a python binding that gives access to the encryption capabilities. – mkluwe Mar 26 '10 at 08:24
  • sure. That's why you need to keep a copy of your decryption tool. You don't need a Python fun OpenSSL; you can use the command line interface and Popen. – vy32 Mar 26 '10 at 17:27
  • I have to migrate some data from a >15 year old database. It has values encrypted with PyCrypto, AES, ECB. Can't get it to decrypt with anything, I'll need the original source code (and some long dead Linux to run it). AES is a standard thing, why do people do non-standard stuff (like obsfucate some key derivation mechanism in some compiled AES.so?). Half the answers on PyCrypto on stack overflow just teach people to do some weird stuff. – oxygen Jan 28 '19 at 17:14
  • Okay, if PyCrypto is no longer supported by your Linux distribution, your best bet is to run an old Linux distribution in a VM, decrypt the data, and move it into a more standard format. – vy32 Jan 29 '19 at 05:29