Decrypt DES by password

Discussion in 'Python' started by Thomas Dybdahl Ahle, May 15, 2006.

  1. 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?

    --
    Programmers should realize their critical importance and responsibility in a
    world gone digital. They are in many ways similar to the priests and monks of
    Europe's Dark Ages; they are the only ones with the training and insight
    to read and interpret the "scripture" of this age.
     
    Thomas Dybdahl Ahle, May 15, 2006
    #1
    1. Advertising

  2. Thomas Dybdahl Ahle

    Paul Rubin Guest

    Thomas Dybdahl Ahle <> writes:
    > 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.
     
    Paul Rubin, May 15, 2006
    #2
    1. Advertising

  3. Den Mon, 15 May 2006 11:32:47 -0700. skrev Paul Rubin:
    > Thomas Dybdahl Ahle <> writes:
    >> 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.

    --
    Programmers should realize their critical importance and responsibility in a
    world gone digital. They are in many ways similar to the priests and monks of
    Europe's Dark Ages; they are the only ones with the training and insight
    to read and interpret the "scripture" of this age.
     
    Thomas Dybdahl Ahle, May 16, 2006
    #3
  4. Thomas Dybdahl Ahle

    Paul Rubin Guest

    Thomas Dybdahl Ahle <> writes:
    > > I'm not aware of a standard that says how CryptDeriveKey is supposed
    > > to work....

    > 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.
     
    Paul Rubin, May 16, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Augusto Rocha

    How do I decrypt a SHA stored password?

    Augusto Rocha, Feb 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    15,658
    Karl Jensen
    Feb 19, 2004
  2. AAaron123
    Replies:
    2
    Views:
    2,362
    AAaron123
    Jan 16, 2009
  3. AAaron123
    Replies:
    1
    Views:
    1,417
    Oriane
    Jan 16, 2009
  4. Lech Migdal

    encrypt with TripleDES, decrypt with DES, doesnt work ?

    Lech Migdal, Apr 7, 2004, in forum: ASP .Net Security
    Replies:
    1
    Views:
    223
    Hernan de Lahitte
    Apr 7, 2004
  5. Replies:
    1
    Views:
    459
    Daniel Martin
    Jun 16, 2007
Loading...

Share This Page