views:

1112

answers:

1

Using PyCrypto (although I've tried this in ObjC with OpenSSL bindings as well) :

from Crypto.Cipher import DES
import base64
obj=DES.new('abcdefgh', DES.MODE_ECB)
plain="Guido van Rossum is a space alien.XXXXXX"
ciph=obj.encrypt(plain)
enc=base64.b64encode(ciph)
#print ciph
print enc

outputs a base64 encoded value of :

ESzjTnGMRFnfVOJwQfqtyXOI8yzAatioyufiSdE1dx02McNkZ2IvBg==

If you were in the interpreter, ciph will give you

'\x11,\xe3Nq\x8cDY\xdfT\xe2pA\xfa\xad\xc9s\x88\xf3,\xc0j\xd8\xa8\xca\xe7\xe2I\xd15w\x1d61\xc3dgb/\x06'

Easy enough. I should be able to pipe this output to OpenSSL and decode it :

I test to make sure that the b64 decode works -

python enctest.py | openssl enc -base64 -d
+ python enctest.py
+ openssl enc -base64 -d
,?Nq?DY?T?pA???s??,?jب???I?5w61?dgb/

Not pretty, but you can see that it got decoded fine, "dgb" and "Nq" are still there.

But go for the full thing :

python enctest.py | openssl enc -base64 -d | openssl enc -nosalt -des-ecb -d -pass pass:abcdefgh
+ python enctest.py
+ openssl enc -nosalt -des-ecb -d -pass pass:abcdefgh
+ openssl enc -base64 -d
bad decrypt
15621:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:461:
j?7????vc]???LE?m³??q?

What am I doing wrong? I've tried using -k abcdefgh -iv 0000000000000000 or typing in the password interactively - same problem.

+3  A: 

echo ESzjTnGMRFnfVOJwQfqtyXOI8yzAatioyufiSdE1dx02McNkZ2IvBg== | openssl enc -nopad -a -des-ecb -K 6162636465666768 -iv 0 -p -d

6162636465666768 is the ASCII "abcdefgh" written out in hexadecimal.

But note that DES in ECB mode is probably not a good way to encode passwords and also is not the "DES crypt" you may have heard of being used on Unix systems. (For passwords, it is usually better to use a hard-to-reverse algorithm (checking the password by regenerating the result instead decrypting the stored password). Even if you do need to be able to decrypt these encrypted passwords, single-DES and especially ECB are poor choices as far as confidentiality is concerned.)

woggle
For sure, I was going to be using Blowfish CBC, I just grabbed the sample DES code for a quick test =)Thanks for the help!
Rizwan Kassim