473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Python Translation of C# DES Encryption

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 di********@qbcon.com and we will send you a copy of the Disclaimer.
#!/usr/bin/env python

from Crypto.Cipher import DES
import base64

def base64DESEncrypt(plaintext):

# Set the key to be used:
rgbKey = "12345678"

# Set the Initial Value (IV) to be used:
rgbIV = "87654321"

des_object=DES.new(rgbKey, DES.MODE_ECB)
des_object.IV = rgbIV

ciphertext = des_object.encrypt(plaintext)
base64_ciphertext = base64.encodestring(ciphertext)

return base64_ciphertext

if __name__ == '__main__':
# The length of the string has to be a multiple of 8, for simplicity (for now).
# The C# code does not have this limitation, so I'd have to look at
# that as well.
print base64DESEncrypt('password')
# Original C# method that I'm trying to translate:
#
#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);
#}

May 12 '06 #1
4 4973
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
May 12 '06 #2
On Fri, 12 May 2006 11:00:23 +0200, Andreas Pauley <an*************@qbcon.com> 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?

Thanks,
Andreas


Check out PyDES I have had great success in decrypting strings that have been encrypted with .NET
TripleDES. Although it does not appear to encrypt the string to the same value it does work
perfectly decrypting and encrypting in a way compatible with thwe C# methods.

http://twhiteman.netfirms.com/des.html

Regards,
Gary.


May 12 '06 #3
Gary Doades <gp*@gpdnet.co.uk> writes:
http://twhiteman.netfirms.com/des.html


I'm not sure if that's the same DES lib that I used in a project last
year but as I remember, the lib I used had a bug in 3DES CBC mode that
made it give answers incompatible with the NIST test vectors. It took
considerable head scratching to figure out that the library was
causing the problem. I ended up switching to a different library.
Anyway, be careful. The library that I used did handle the 1DES CBC
and 3DES ECB correctly, fwiw.
May 13 '06 #4
Edward Elliott wrote:
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.

I read some of the C# documentation on DESCryptoServiceProvider, but it
was completely useless for
my purpose. The documentation basically only tells you how to use the
class, not what it does.

I managed to *decrypt* a string encrypted by the C# method using CBC
mode, as suggested by Gary (thanks).
The python CBC encryption results in a different encrypted string, but
decrypting both these strings
returns the original plaintext value.

As for padding, the C# method uses characters '\x01' to '\x07' for padding.
If there are 3 padding characters needed (eg with a password of 5
chars), then three '\x03' chars will be used.
Similarly, a 2-char password will be padded with '\x06' * 6

Regards,
Andreas Pauley

__________________________________________________ ___________________________________________
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 di********@qbcon.com and we will send you a copy of the Disclaimer.

May 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Orner | last post by:
Hello, I'm working on a project that consists of a Java application on the desktop, and a server with Python CGI code. I'm trying to find an encryption algorithm, which would let me send...
22
by: Kamilche | last post by:
I've looked at a few alternatives for encryption with Python, and didn't come up anything very speedy. I've written an encryption algorithm in pure Python that can process 22 megs of data a...
6
by: Stephen Ferg | last post by:
I need to translate some Perl scripts into Python. When I went looking for a tool that would help automate the translation, I was rather surprised that I couldn't find anything. BridgeKeeper,...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
50
by: Gosi | last post by:
It is quite easy to call J from Python http://groups.google.com/group/J-Programming/browse_thread/thread/5e84b75667f5f64e
3
by: galadhad | last post by:
Okay, here goes. I'm new to Python (and relatively new to programming/scripting in general - I understand basics, but complex concepts are still beyond my limited knowledge). I am working on a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.