Click here to Skip to main content
15,171,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Folks, I am in a dead trouble trying to replicate an encryption algorithm in C# same as the below in java. Here plainData is 16 char hex value as a string (ex:048054CC6DED88AD) to be encrypted and keyString is a 16 char hex key in string format (ex: 07D3251ACE1FF48A. I even have migrated Hex and BigInteger classes to C#. I need the encrypted output to be a 16 char hex value in string. Please Help......

///////// java: start ///////////////

String strEncryptedData;
//Add Cryptix provider
Security.addProvider(new cryptix.provider.Cryptix());
// The DES algorithm requires a trusted source of random bits
SecureRandom sr = new SecureRandom();
// Create a DES key object specification from the raw data
DESKeySpec dks = new DESKeySpec(Hex.fromString(keyString));
// Create a key factory and use it to turn the DESKeySpec into
// a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// Initialize the cipher with the key
cipher.init( Cipher.ENCRYPT_MODE, key, sr );
byte[] data = Hex.fromString(plainData);
// The actual encryption step
byte encryptedData[] = cipher.doFinal(data);

//Convert the encrypted bytes big int
BigInteger bCiph = new BigInteger(encryptedData);
//Dump the byte array to output string
strEncryptedData = Hex.dumpString(getMagnitude(bCiph));
strEnryptedData = encryptedData.trim();

//DES operation in DECRYPT mode: for decryption
dks = new DESKeySpec(Hex.fromString(keyString));
key = keyFactory.generateSecret(dks);
cipher.init( Cipher.DECRYPT_MODE, key, sr );
byte encryptedData1[] = cipher.doFinal(encryptedData);

///////// java: end ///////////////

I have done quite a hit and trial on the below C# code to replicate the above java code as it is but I am not getting it as desired.

///////// c#: start ///////////////

static TripleDES CreateDES(string key)
	System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
	TripleDES des = new TripleDESCryptoServiceProvider();
	des.Padding = PaddingMode.None;
	des.Mode = CipherMode.ECB;
	//des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
	//des.Key = md5.ComputeHash(Convert.FromBase64String(key));
	//des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
	des.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(key));
	des.IV = new byte[des.BlockSize / 8];
	return des;
public byte[] Encryption(string PlainText, string key)
	//TripleDES des = CreateDES(key);
	TripleDES des = CreateDES(key);
	LogManager.logDebug("in TripleDESUtil des = " + des.ToString(), "COMMAND_DEBUG");
	ICryptoTransform ct = des.CreateEncryptor();
	//byte[] input = Encoding.Unicode.GetBytes(PlainText);
	//byte[] input = Convert.FromBase64String(PlainText);
	//byte[] input = Encoding.UTF8.GetBytes(PlainText);
	byte[] input = Encoding.ASCII.GetBytes(PlainText);
	return ct.TransformFinalBlock(input, 0, input.Length);

///////// c#: end///////////////

Updated 8-Mar-10 7:21am


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900