English 中文(简体)
两种不同的散射数值即使在安乐斯和安伯也一样。 NET
原标题:Two different hash values even after entering same string in Android and .NET

我有一个SAP网络网络服务器,供SHA-1的洗衣,以便获得第64号基地的洗衣。

我的网络服务守则如下:

 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和基地64:

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;   
    
}

甲型再回散面值(从用于输入的原体中)

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

页: 1

难道有人会说,我的法典中有什么错误? 我在某个地方重复

请帮助

增 编

最佳回答

Check the encoding, make sure that the actual byte arrays that are used to compute the hash are equal. In .NET you use ASCII encoding and in Android iso-8859-1. This should not mater as the iso-8859-1 is natural extension of the ASCII encoding, but still, check the arrays. Also try using this function for SHA1 digest on Android:

 /**
   * 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 - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签