Python Translation of C# DES Encryption

Andreas Pauley andreasp+python at
Fri May 12 05:00:23 EDT 2006

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?


The C# method:

public static string Encrypt(string decrypted)
    byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(decrypted);
    byte[] rgbKey = System.Text.ASCIIEncoding.ASCII.GetBytes("12345678");
    byte[] rgbIV  = System.Text.ASCIIEncoding.ASCII.GetBytes("87654321");

    MemoryStream memoryStream = new MemoryStream(1024);

    DESCryptoServiceProvider desCryptoServiceProvider = new    

    CryptoStream cryptoStream = new CryptoStream(memoryStream,
        desCryptoServiceProvider.CreateEncryptor(rgbKey, rgbIV),

    cryptoStream.Write(data, 0, data.Length);
    byte[] result = new byte[(int)memoryStream.Position];
    memoryStream.Position = 0;
    memoryStream.Read(result, 0, result.Length);
    return System.Convert.ToBase64String(result);

This e-mail contains official information from Quality Business Consultants (Pty) Ltd and is intended for use by the addressee only.
Important notice: Important restrictions, qualifications and disclaimers ("the Disclaimer") apply to this email.
To read this click on the following address:
The Disclaimer forms part of the content of this email in terms of section 11 of the Electronic Communications and Transactions Act, 25 of 2002. If you are unable to access the Disclaimer, send a blank e-mail to disclaimer at and we will send you a copy of the Disclaimer.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
URL: <>

More information about the Python-list mailing list