我在Java中有一个功能TripleDES解密
public static String decrypt(String key, String data) throws Exception {
Cipher cipher = Cipher.getInstance("TripleDES");
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(key.getBytes(), 0, key.length());
String keymd5 = new BigInteger(1, md5.digest()).toString(16).substring(0, 24);
System.out.println("keymd5 = " + keymd5);
SecretKeySpec keyspec = new SecretKeySpec(keymd5.getBytes(), "TripleDES");
cipher.init(Cipher.DECRYPT_MODE, keyspec);
//BASE64Decoder decoder = new BASE64Decoder();
//byte[] raw = decoder.decodeBuffer(data);
Base64 decoder = new Base64();
byte[] raw = decoder.decode(data.getBytes());
byte[] stringBytes = cipher.doFinal(raw);
String result = new String(stringBytes);
System.out.println("result = " + result);
return result;
}
我尝试将其转换为下面的C#函数
public static string decrypt(string key, string dataen)
{
try
{
byte[] keydata = Encoding.UTF8.GetBytes(key);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(keydata);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("x2"));
}
string md5String = sb.ToString();
byte[] tripleDesKey = Encoding.UTF8.GetBytes(md5String.Substring(0, 24));
TripleDES tripdes = TripleDESCryptoServiceProvider.Create();
tripdes.Mode = CipherMode.ECB;
tripdes.Key = tripleDesKey;
ICryptoTransform ict = tripdes.CreateDecryptor();
byte[] cryptoByte = ict.TransformFinalBlock(Encoding.UTF8.GetBytes(dataen), 0, 8);
string data = Convert.ToBase64String(cryptoByte, 0, cryptoByte.GetLength(0)).Trim();
return Encoding.UTF8.GetString(Convert.FromBase64String(data));
}
catch (Exception ex)
{
return dataen;
}
}
它不起作用,抛出“错误数据”异常
示例:
key =“ g0k9n58y1cl02bc1qv79sot3ihbmtwko”
encryptData =“ JFBUJrlauiNrjVNeNKIrCuXbhFWkreqxJNpsMCspLiiE / QwQ8wuksuK6qpkISD73lhCKrwQx2vpU \ ncmQHK0lAmyP8zXN71SaQe2riKZvy43 {im4VSE4KVK4}
如何将上述Java TripleDES解密功能转换为C# 谢谢!