C# to Java code converter
989150Feb 5 2013 — edited Feb 5 2013Are there any converters available that converts C# code to Java?
I need to convert the below code into Java
public class DataEncryption
{
public string StringEncode(string value, string key)
{
System.Security.Cryptography.MACTripleDES mac3des = new System.Security.Cryptography.MACTripleDES();
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value)) + '-' + Convert.ToBase64String(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value)));
}
public string StringDecode(string value, string key)
{
string dataValue = "";
string calcHash = "";
string storedHash = "";
if ((value == null))
return dataValue;
System.Security.Cryptography.MACTripleDES mac3des = new System.Security.Cryptography.MACTripleDES();
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
// Try
dataValue = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[0]));
storedHash = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[1]));
calcHash = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));
if (storedHash != calcHash)
{
//Data was corrupted
throw new ArgumentException("Hash value does not match");
//This error is immediately caught below
}
//Catch ex As Exception
// Throw New ArgumentException("Invalid TamperProofString")
//End Try
return dataValue;
}
}
Edited by: 986147 on Feb 5, 2013 1:47 AM