比较在Android中使用SHA-1生成的两个哈希字符串
以下是我的Android代码流程: 我使用SHA-1散列来散列用户通过EditText输入的密码。我在这里得到一个散列字符串作为输出 之后,我调用一个SOAP web服务(使用.NET framework创建),该服务使用ASCII编码执行相同的SHA-1哈希,并返回另一个哈希字符串 现在,由于两种情况下的输入字符串相同,因此我的哈希字符串与预期的相同。 见下面的logcat。但是当我比较散列字符串时,我并没有得到预期的结果 以下是我的Android代码,后跟logcat:比较在Android中使用SHA-1生成的两个哈希字符串,android,web-services,hash,ksoap2,sha,Android,Web Services,Hash,Ksoap2,Sha,以下是我的Android代码流程: 我使用SHA-1散列来散列用户通过EditText输入的密码。我在这里得到一个散列字符串作为输出 之后,我调用一个SOAP web服务(使用.NET framework创建),该服务使用ASCII编码执行相同的SHA-1哈希,并返回另一个哈希字符串 现在,由于两种情况下的输入字符串相同,因此我的哈希字符串与预期的相同。 见下面的logcat。但是当我比较散列字符串时,我并没有得到预期的结果 以下是我的Android代码,后跟logcat:
package com.kar.encodePassword;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PaswordencodingActivity extends Activity {
/** Called when the activity is first created. */
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 (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else{
Toast.makeText(PaswordencodingActivity.this, "this is a negative onClick", Toast.LENGTH_LONG).show();
}
}
});
}
private static String convertToHex(byte[] data) throws java.io.IOException
{
System.out.println("data received is" +data);
StringBuffer sb = new StringBuffer();
String hex=null;
hex=Base64.encodeToString(data, 0, data.length, NO_OPTIONS);
for (int i = 0; i < data.length; i++)
{
if (hex.length() == 1)
{
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
public void SHA1(String text) throws IOException
{
MessageDigest mdSha1 = null;
try
{
mdSha1 = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e1) {
Log.e("myapp", "Error initializing SHA1 message digest");
}
mdSha1.update(text.getBytes("iso-8859-1"), 0, text.length());
byte[] data = mdSha1.digest();
hash=convertToHex(data);
System.out.println("data going is" +data);
System.out.println("hash value"+hash);
try
{
result2=call3(password);
if(result2.equalsIgnoreCase(hash.toString()))
System.out.println("success");
} catch (XmlPullParserException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String call3(String pass) throws XmlPullParserException
{
String b="";
SoapObject request = new SoapObject(namespace2, method_name);
request.addProperty("str",pass);
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;
}
}
我认为您将
byte[]
转换为String
的方式是错误的,应该是这样的:
hash = new String(data);
if(result2.equals(hash))
同样,你做的比较是错误的,应该是这样的:
hash = new String(data);
if(result2.equals(hash))
我认为您根本不应该将字节[]
转换为字符串。使用数组.equals(byteArray1,byteArray2)代码>。您的数据是1
和0
s的随机位。将其转换为字符串
可以通过多种方式完成,但没有任何意义。试试看
result2 = result2.trim();
hash = hash.trim();
System.out.println("result2='" + result2 + "'");
System.out.println("hash ='" + hash + "'");
if(result2.equalsIgnoreCase(hash))
System.out.println("success");
如果这没有帮助,请检查实际阵列:
byte[] a = result.toString().getBytes()
byte[] b = hash..getBytes();
如果数组不相等,则字符串的编码不同。如果要对从字符串派生的数据应用哈希算法,则需要使用相同的字符编码来检索字节
text.getBytes("iso-8859-1")
不同的字符集可以以不同的方式表示字节值。很抱歉,我没有得到转换哈希=新字符串(数据)的第一点;你能解释一下吗?此外,我尝试使用equals、equalsIgnoreCase和所有可能的字符串比较方法,但仍然没有得到结果,因为“成功”很好,两种方法都不起作用。现在告诉我如何更改android端代码的编码。我的.net web服务是ASCII btwI尝试在我的android应用程序中使用ASCII和服务器端代码。仍然没有正确的结果:(好的,在我的服务器端我使用ASCII,所以在android端我可以写text.getBytes(“ASCII”)完成后,我该如何比较?我应该像@peceps提到的那样使用equalsIgnoreCase进行比较还是比较字节数组?请看以下内容:。这是否正确?如果我现在使用String.equals,我会得到输出吗?但它不起作用,伙计!!这就是问题所在。。请帮助!!让我们一起来吧