Python Translation of C# DES Encryption

Andreas Pauley andreasp+python at qbcon.com
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?

Thanks,
Andreas

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    
DESCryptoServiceProvider();

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

    cryptoStream.Write(data, 0, data.Length);
    cryptoStream.FlushFinalBlock();
    byte[] result = new byte[(int)memoryStream.Position];
    memoryStream.Position = 0;
    memoryStream.Read(result, 0, result.Length);
    cryptoStream.Close();
    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: http://www.qbcon.com/disclaimer
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 qbcon.com and we will send you a copy of the Disclaimer.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: des-encrypt.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20060512/1965b7ce/attachment.ksh>


More information about the Python-list mailing list