2013-04-22 62 views
1

我想让用户输入一个引脚,然后使用公式创建一个时间敏感的字符串。 I.E.如果您在一分钟后调用函数,则字符串将完全不同。然后我用这个字符串运行它通过SHA1算法。我的问题是我需要.Net和Mono库来产生相同的结果,而他们不是。我正在使用Xmarin工作室将单声道库部署到android。我正在使用.Net 3.5框架来部署我的Web服务。使用Xamarin(Mono C#)和.Net生成相同的密钥

我已经确认,作为参数传递给sha1 aglorithm的两个字符串在android和.net上都是相同的。 问题是SHA1算法的输出是不同的。我相信这是因为它们是如何在不同的图书馆中实施的。两个设备上的实际C#代码是相同的。

有谁知道一个简单的算法,我可以使用,不会依赖于库?或者更好的建议,我可能做错了什么。

这是代码为我的C#3.5 web服务

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Security.Cryptography; 


namespace rasToken 
{ 
    /// <summary> 
    /// Summary description for Service1 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class Service1 : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public string rasEncrypt(String userid) 
    { 
     //get pin from data base 
     String pin = "1234"; 
     return generateToken(pin); 

    } 
    public string generateToken(String pin) 
    { 
     String debug=""; 

     int month = DateTime.Now.Month; 
     debug+="month: "+month; 
     int year = DateTime.Now.DayOfYear; 
     debug+="year: "+year; 
     int hour = DateTime.Now.Hour; 
     debug+="hour: "+hour; 
     int minute = DateTime.Now.Minute; 
     debug+="minute: "+minute; 
     int day = DateTime.Now.Day; 
     debug+="day: "+day; 
     int concat = month * minute * year * day * hour * 7857564; 
     concat=Math.Abs(concat); 
     SHA1 sh1 = SHA1.Create(); 
     String hash = concat + "23117345423219" + pin; 
     //MD5 hasher = MD5.Create(); 
     byte[] result = sh1.ComputeHash(getBytes(hash)); 


     String final = getString(result); 



     return final.Substring(0, 8)+hash; 
    } 

    private byte[] getBytes(String hash){ 
     System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); 
     return encoding.GetBytes(hash);   
    } 

    private String getString(byte[] bytes) 
    { 

     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
     String clean = Convert.ToBase64String(bytes).Replace(@"\", string.Empty); 
     return clean; 
     //return encoding.GetString(bytes); 
    } 

} 
} 

这是我对单的Android库

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using System.Security.Cryptography; 

namespace raskey 
{ 
[Activity (Label = "raskey", MainLauncher = true)] 
public class Activity1 : Activity 
{ 
    //int count = 1; 

    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 

     // Get our button from the layout resource, 
     // and attach an event to it 
     Button button = FindViewById<Button> (Resource.Id.myButton); 
     EditText input = FindViewById<EditText> (Resource.Id.pin); 
     //button.Click += delegate { 
     // button.Text = string.Format ("{0} clicks!", count++); 
     //}; 
     input.KeyPress+=(object sender, View.KeyEventArgs e) => { 
      if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) { 
       generateToken(input.Text); 
       Toast.MakeText (this, generateToken(input.Text), ToastLength.Short).Show(); 
       e.Handled = true; 
      } 
     }; 

    } 
    public string generateToken(String pin) 
    { 
     String debug=""; 

     int month = DateTime.Now.Month; 
     debug+="month: "+month; 
     int year = DateTime.Now.DayOfYear; 
     debug+="year: "+year; 
     int hour = DateTime.Now.Hour; 
     debug+="hour: "+hour; 
     int minute = DateTime.Now.Minute; 
     debug+="minute: "+minute; 
     int day = DateTime.Now.Day; 
     debug+="day: "+day; 
     int concat = month * minute * year * day * hour * 7857564; 
     concat=Math.Abs(concat); 
     SHA1 sh1 = SHA1.Create(); 
     String hash = concat + "23117345423219" + pin; 
     //MD5 hasher = MD5.Create(); 
     byte[] result = sh1.ComputeHash(getBytes(hash)); 


     String final = getString(result); 



     return final.Substring(0, 8)+" "+hash; 
    } 

    private byte[] getBytes(String hash){ 
     System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); 
     return encoding.GetBytes(hash);   
    } 

    private String getString(byte[] bytes) 
    { 

     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
     String clean = Convert.ToBase64String(bytes).Replace(@"\", string.Empty); 
     return clean; 
     //return encoding.GetString(bytes); 
    } 
} 

}

+2

SHA1是一种标准算法。每个(正确的)实现都会产生相同的结果。这是代码中的问题,可能与编码有关。 – SLaks 2013-04-22 21:48:46

+0

好的,我会在一秒钟内更新两个项目的代码。 – Craig 2013-04-22 21:52:24

+0

我刚刚更新,并且fyi不介意硬编码的1234。我总是输入1234作为Android设备上的引脚。 – Craig 2013-04-22 22:03:43

回答

4

您的源代码在服务器和客户端看起来不同,

return final.Substring(0, 8)+hash; 

return final.Substring(0, 8)+" "+hash; 

,这将返回不同的散列值。

对于调试,您可能希望跳过散列部分并使用令牌来查看它们是否匹配(如果不是,则说明您的问题与加密无关)。

+0

感谢您的建议。我弄明白了,结果我只是哑巴。我试图在我的布局中使用android来强制用户只输入数字。这导致一个空字符串被传递给我的android设备上的generateToken方法的输入。感谢您的建议。 – Craig 2013-04-25 19:50:30

2

代码,我怀疑你hash字符串是不同的。你应该记录它们并确保它们匹配。