Translate from Java to C#: simple code to re-encode a string

Posted by Dr. Zim on Stack Overflow See other posts from Stack Overflow or by Dr. Zim
Published on 2010-05-28T20:17:04Z Indexed on 2010/05/28 20:32 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about java