2011-11-25 72 views
1

使用SHA-1產生兩個哈希字符串這裏是我的Android代碼的流程:比較Android中

我使用SHA-1散列散列由通過的EditText用戶輸入的密碼。我在這裏得到一個散列字符串作爲輸出。

之後,我調用一個使用ASCII編碼執行相同SHA-1散列的SOAP Web服務(使用.NET框架創建),並返回另一個散列字符串。

現在由於輸入字符串在兩種情況下都是相同的,所以我的散列字符串與預期相同。 請參閱下面的logcat。但是,當我比較散串,我沒有得到的結果如預期

這裏之後logcat的我的Android代碼:

  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; 

} 
} 

基本上當我試圖比較兩個哈希字符串我沒有得到結果

爲什麼這樣?誰能幫忙?

的logcat:

 11-25 17:09:50.899: INFO/System.out(275): data received is[[email protected] 
     11-25 17:09:50.909: INFO/System.out(275): data going is[[email protected] 
     11-25 17:09:50.909: INFO/System.out(275): hash valueUGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:09:50.909: INFO/System.out(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:10:01.730: INFO/myapp(275): UGGpBypVug3K+/4xONpqv9wkFd8= 
     11-25 17:10:01.730: INFO/System.out(275): --- response ---- UGGpBypVug3K+/4xONpqv9wkFd8= 

回答

1

嘗試

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(); 

如果數組不相等,你有不同的編碼字符串。

+0

罰款都沒有工作嘗試了他們..現在告訴我如何更改編碼在Android端代碼..我.net網絡服務是ASCII btw –

+0

我嘗試在我的Android應用程序和服務器端代碼都使用ASCII ..still沒有適當結果:( –

+0

非常感謝幫助..它工作!:-) –

1

我認爲你在錯誤的方式轉換byte[]String,它應該是這樣的:

hash = new String(data); 

而且你正在做的比較錯誤的,應該是這樣的:

if(result2.equals(hash)) 

在我看來,你不應該轉換byte[]String。使用Arrays.equals(byteArray1,byteArray2);。您的數據是隨機位10 s。將它轉換爲String可以用很多方式完成,但它沒有任何意義。

+0

對不起,我沒有得到您的第一點轉換哈希=新的字符串(數據);你能解釋一下嗎?此外,我嘗試使用equals,equalsIgnoreCase和所有可能的方法進行字符串比較,但仍然沒有得到結果作爲「成功」 –

+0

哪2個字節數組需要根據我的代碼進行比較? –

1

如果對從String派生的數據應用散列算法,則需要使用相同的字符編碼來檢索字節。

text.getBytes("iso-8859-1") 

不同的字符集可以不同地表示字節值。

+0

好的,所以在我的服務器端我使用ASCII,所以在Android端我可以寫text.getBytes(「ASCII」) –

+0

是的,你可以做到這一點。 –

+0

確定後,我該如何比較?我應該使用equalsIgnoreCase進行比較,因爲@peceps提到或比較了字節數組? –