我们收到了以下公式来加密用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"
我们用C#编写的尝试是:
System.Security.Cryptography.SHA1 sha1 =
new System.Security.Cryptography.SHA1CryptoServiceProvider();
string myOutput = HttpUtility.UrlEncode(
Convert.ToBase64String(
sha1.ComputeHash(
ASCIIEncoding.Default.GetBytes(myInput))));
但是,输出几乎没有相同之处。它甚至没有百分号。任何人都有机会知道我们要怎么做吗?
最佳答案
在您的C#版本中,建议您尝试使用UTF8Encoding
,UnicodeEncoding
或UTF32Encoding
而不是当前的ASCIIEncoding
。
如果您未指定(且未指定),Java的String.getBytes
方法将使用平台的默认编码。该平台的默认编码不太可能是ASCII。
关于c# - 从Java转换为C#:用于重新编码字符串的简单代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2932175/