Translate from Java to C#: simple code to re-encode a string
- by Dr. Zim
We were sent this formula to encrypt a string written in Java:
String myInput = "test1234";
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] myD = md.digest(myInput.getBytes());
BASE64Encoder en64 = new BASE64Encoder();
String myOutput = new String (
Java.net.URLEncoder.encode( en64.encode(myD)));
// myOutput becomes "F009U%2Bx99bVTGwS3cQdHf%2BJcpCo%3D"
Our attempt at writing this in C# is:
System.Security.Cryptography.SHA1 sha1 =
new System.Security.Cryptography.SHA1CryptoServiceProvider();
string myOutput = HttpUtility.UrlEncode(
Convert.ToBase64String(
sha1.ComputeHash(
ASCIIEncoding.Default.GetBytes(myInput))));
However the output is no where near the same. It doesn't even have percent signs in it. Any chance anyone would know where we are going wrong?