Python Translation of C# DES Encryption

Edward Elliott nobody at 127.0.0.1
Fri May 12 20:20:49 CEST 2006


Andreas Pauley wrote:

> Hi all,
> 
> I'm trying to implement a Python equivalent of a C# method that encrypts
> a string.
> My Python attempt is in the attached file, but does not return the same
> value as the C# method (see below).
> 
> Any hints?

I can't tell from the code you posted what a DESCryptoServiceProvider does,
but I think you're using two different block cipher modes.  The C/C# code
uses an IV, which means it's probably not using ECB mode.  Meanwhile your
Python code uses ECB mode, which doesn't even use an IV (the one you
provided is probably just ignored).

You need to find the exact algorithm the DESCryptoServiceProvider uses.  
CBC and CTR are common modes with IVs.  Besides mode of operation, there's
the issue of how it pads the data (in the cipher, not base64).  There could
also be differences in things like the key scheduling routines, but that's
less likely.  Until you know exactly what the original code does, your only
option is trial and error.

-- 
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net



More information about the Python-list mailing list