Decrypt DES by password

  • Thread starter Thomas Dybdahl Ahle
  • Start date
T

Thomas Dybdahl Ahle

Hi, I've got some DES encrypted data, for which I know the password.
The problem is that I have to generate an 8byte key from the password.
I use python-crypto-2.0.1.

I also know, that the C# way to do the decryption is:
PasswordDeriveBytes bytes1 = new
PasswordDeriveBytes("passwordString", null);
byte[] array1 = new byte[8];
byte[] array2 = bytes1.CryptDeriveKey("DES", "MD5", 0, array1);

DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
FileStream fs = new FileStream("fileNameString", FileMode.Open,
FileAccess.Read); CryptoStream cs = new CryptoStream(fs,
provider1.CreateEncryptor(array2, array1), CryptoStreamMode.Read);

Anybody know how to do this in python?
 
P

Paul Rubin

Thomas Dybdahl Ahle said:
byte[] array2 = bytes1.CryptDeriveKey("DES", "MD5", 0, array1);
Anybody know how to do this in python?

I'm not aware of a standard that says how CryptDeriveKey is supposed
to work. Or rather, there are multiple possible standard ways to do
it. If you can find an exact specification, or C# source code that
does it, it will probably be straightforward to reimplement in Python.

If you want to just do something generic and don't need to
interoperate with a C# application that uses CryptDeriveKey, the
following should be ok:

import hmac
password = 'the big sekrit password goes here'

key1 = hmac.HMAC(password, '1').digest()[:8] # get 8 bytes

And if you need additional keys, such as for triple DES:

key2 = hmac.HMAC(password, '2').digest()[:8] # get 8 bytes
key3 = hmac.HMAC(password, '3').digest()[:8] # get 8 bytes

If you want to be fancier you could try PKCS5 KDF2:

http://www.rsasecurity.com/rsalabs/node.asp?id=2127

CryptDeriveKey may in fact be doing something like this.
 
T

Thomas Dybdahl Ahle

Den Mon, 15 May 2006 11:32:47 -0700. skrev Paul Rubin:
Thomas Dybdahl Ahle said:
byte[] array2 = bytes1.CryptDeriveKey("DES", "MD5", 0, array1);
Anybody know how to do this in python?

I'm not aware of a standard that says how CryptDeriveKey is supposed
to work. Or rather, there are multiple possible standard ways to do
it. If you can find an exact specification, or C# source code that
does it, it will probably be straightforward to reimplement in Python.

I tried to find out how the monofolks did it, but also they didn't know
the algorithms. Maybe I should find a windows computer with .net, create
the key and base64 encode it, so that I could take it to the python
program...

But thanks for the excample anyways. Maybe I can use it, if I need to
encrypt something myself, another time.
 
P

Paul Rubin

Thomas Dybdahl Ahle said:
I tried to find out how the monofolks did it, but also they didn't know
the algorithms.

Actually this is almost certainly an entry into the Windows Crypto API.
If you're persistent you might be able to figure it out from the CAPI
does at http://msdn.microsoft.com. Start by typing CryptDeriveKey
into the search box and follow links and do more searches from there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top