gpt4 book ai didi

android - 即使在 Android 和 .NET 中输入相同的字符串后也会有两个不同的哈希值

转载 作者:行者123 更新时间:2023-11-30 04:27:23 25 4
gpt4 key购买 nike

我有一个 .NET SOAP 网络服务(.asmx),用于 SHA-1 哈希以获取 Base64 哈希字符串。

这是我的网络服务代码:

 public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HashCode(string str)
{
string rethash = "";
try
{

System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] combined = encoder.GetBytes(str);
hash.ComputeHash(combined);
rethash = Convert.ToBase64String(hash.Hash);
}
catch (Exception ex)
{
string strerr = "Error in HashCode : " + ex.Message;
}
return rethash;
}
}

此处为输入字符串“abc”返回的哈希是

qZk+NkcGgWq6PiVxeFDCbJzQ2J0=

现在,再次使用 SHA-1 和 Base64 的 Android 代码是:

public class PaswordencodingActivity extends Activity 
{
private static final String soap_action = "http://tempuri.org/HashCode";
private static final String method_name = "HashCode";
private static final String namespace2 = "http://tempuri.org/";
private static final String url2 = "http://10.0.2.2/checkhash/Service1.asmx";

String password="abc";
public final static int NO_OPTIONS = 0;
String hash;
String result2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final EditText pass=(EditText)findViewById(R.id.editText1);
Button encode=(Button)findViewById(R.id.button1);

encode.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
password=pass.getText().toString();
if(password!=null){
try {
SHA1(password) ;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
Toast.makeText(PaswordencodingActivity.this, "this is a negative onClick", Toast.LENGTH_LONG).show();
}

}
});

}
private static String convertToHex(byte[] bytes) throws java.io.IOException
{
StringBuffer sb = new StringBuffer();
String hex=null;
for (int i = 0; i < bytes.length; i++)
{
hex=Base64.encodeToString(bytes, 0, bytes.length, NO_OPTIONS);
if (hex.length() == 1)
{
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}


public void SHA1(String text)
throws NoSuchAlgorithmException, IOException
{
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
hash=convertToHex(sha1hash);
System.out.println("hash value is"+hash);
try
{
result2 = call3(hash);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(result2.equalsIgnoreCase(hash))
{
System.out.println("success");

}

}

public String call3(String hash) throws XmlPullParserException
{
String b="";
SoapObject request = new SoapObject(namespace2, method_name);
request.addProperty("str",hash);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE android = new HttpTransportSE(url2);
android.debug = true;
try
{

android.call(soap_action, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
Log.i("myapp",result.toString());
System.out.println(" --- response ---- " + result);
b=result.toString();

} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

return b;

}

Android 返回的哈希值是(来自输入字符串“abc”的 logcat)

11-24 13:56:35.179: INFO/myapp(578): 48a4yT8WPFCmkTxMSC9WaEtSxJI=

11-24 13:56:35.179: INFO/System.out(578): --- response ---- 48a4yT8WPFCmkTxMSC9WaEtSxJI=

谁能告诉我代码中出了什么问题?我在某处双重哈希

请帮忙

谢谢

最佳答案

检查编码,确保用于计算散列的实际字节数组相等。在 .NET 中使用 ASCII 编码,在 Android 中使用 iso-8859-1。这不重要,因为 iso-8859-1 是 ASCII 编码的自然扩展,但仍然要检查数组。也可以尝试在 Android 上对 SHA1 摘要使用此函数:

 /**
* Generates SHA-1 digest of the provided data.
*
* @param data the data to digest
* @return SHA-1 digest of the provided data.
*/
public static byte[] sha1Digest(byte[] data) {
MessageDigest mdSha1 = null;
try {
mdSha1 = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e1) {
Log.e(LOG_TAG, "Error initializing SHA1 message digest");
}
mdSha1.update(data);
byte[] sha1hash = mdSha1.digest();
return sha1hash;
}

关于android - 即使在 Android 和 .NET 中输入相同的字符串后也会有两个不同的哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com