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.ASC IIEncoding.ASCI I.GetBytes(decr ypted);
byte[] rgbKey = System.Text.ASC IIEncoding.ASCI I.GetBytes("123 45678");
byte[] rgbIV = System.Text.ASC IIEncoding.ASCI I.GetBytes("876 54321");
MemoryStream memoryStream = new MemoryStream(10 24);
DESCryptoServic eProvider desCryptoServic eProvider = new
DESCryptoServic eProvider();
CryptoStream cryptoStream = new CryptoStream(me moryStream,
desCryptoServic eProvider.Creat eEncryptor(rgbK ey, rgbIV),
CryptoStreamMod e.Write);
cryptoStream.Wr ite(data, 0, data.Length);
cryptoStream.Fl ushFinalBlock() ;
byte[] result = new byte[(int)memoryStre am.Position];
memoryStream.Po sition = 0;
memoryStream.Re ad(result, 0, result.Length);
cryptoStream.Cl ose();
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********@qbco n.com and we will send you a copy of the Disclaimer.
#!/usr/bin/env python
from Crypto.Cipher import DES
import base64
def base64DESEncryp t(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.encr ypt(plaintext)
base64_cipherte xt = base64.encodest ring(ciphertext )
return base64_cipherte xt
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 base64DESEncryp t('password')
# Original C# method that I'm trying to translate:
#
#public static string Encrypt(string decrypted)
#{
# byte[] data = System.Text.ASC IIEncoding.ASCI I.GetBytes(decr ypted);
# byte[] rgbKey = System.Text.ASC IIEncoding.ASCI I.GetBytes("123 45678");
# byte[] rgbIV = System.Text.ASC IIEncoding.ASCI I.GetBytes("876 54321");
#
# MemoryStream memoryStream = new MemoryStream(10 24);
#
# DESCryptoServic eProvider desCryptoServic eProvider = new DESCryptoServic eProvider();
#
# CryptoStream cryptoStream = new CryptoStream(me moryStream,
# desCryptoServic eProvider.Creat eEncryptor(rgbK ey, rgbIV),
# CryptoStreamMod e.Write);
#
# cryptoStream.Wr ite(data, 0, data.Length);
# cryptoStream.Fl ushFinalBlock() ;
# byte[] result = new byte[(int)memoryStre am.Position];
# memoryStream.Po sition = 0;
# memoryStream.Re ad(result, 0, result.Length);
# cryptoStream.Cl ose();
# return System.Convert. ToBase64String( result);
#}