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;
Security.addProvider(new cryptix.provider.Cryptix());
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(Hex.fromString(keyString));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init( Cipher.ENCRYPT_MODE, key, sr );
byte[] data = Hex.fromString(plainData);
byte encryptedData[] = cipher.doFinal(data);
BigInteger bCiph = new BigInteger(encryptedData);
strEncryptedData = Hex.dumpString(getMagnitude(bCiph));
strEnryptedData = encryptedData.trim();
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.ASCII.GetBytes(key));
des.IV = new byte[des.BlockSize / 8];
return des;
}
public byte[] Encryption(string PlainText, string key)
{
TripleDES des = CreateDES(key);
LogManager.logDebug("in TripleDESUtil des = " + des.ToString(), "COMMAND_DEBUG");
ICryptoTransform ct = des.CreateEncryptor();
byte[] input = Encoding.ASCII.GetBytes(PlainText);
return ct.TransformFinalBlock(input, 0, input.Length);
}
///////// c#: end///////////////
regards,
Sadique