This discussion is archived
1 Reply Latest reply: Feb 5, 2013 7:44 AM by sabre150 RSS

C# to Java code converter

989150 Newbie
Currently Being Moderated
Are 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
  • 1. Re: C# to Java code converter
    sabre150 Expert
    Currently Being Moderated
    986147 wrote:
    Are there any converters available that converts C# code to Java?
    I would doubt very much if you would find a converter that would handle all those C# crypto libraries.

    Manually converting that C# code to Java would not take long; probably much less time than trying to find a converter to do the job. The JCE has all the functionality except for the Base64 encoding which can be performed using the 'apache commons codec' library. MACTripleDES is just a CBC-MAC (using zeros pading) which is easy to implement using the JCE. You will have to do some trivial byte manipulation to convert the 16 bytes of the MD5 hash to the 24 byte key needed by 3DES in the JCE.

    P.S. You do realise that the data is not actually encrypted. It is just Base64 encoded and a MAC appended. An encoded value with MAC cannot be forged without having the key but it can easily be decoded.

    Edited by: sabre150 on Feb 5, 2013 3:28 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points