A
Andreas Pauley
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 (e-mail address removed) 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);
#}
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 (e-mail address removed) 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);
#}