使用Xamarin (Mono c#)和.net生成相同的键
本文关键字:net Xamarin Mono 使用 | 更新日期: 2023-09-27 18:10:19
我试图让用户输入一个引脚,然后使用公式来创建一个时间敏感的字符串。也就是说,如果你在一分钟后调用这个函数,字符串将完全不同。然后,我将这个字符串通过SHA1算法运行。我的问题是,我需要。net和Mono库产生相同的结果,他们没有。我使用Xmarin studio将mono库部署到android。我使用。net 3.5框架来部署我的web服务。
我已经确认,作为参数传递给sha1算法的两个字符串在android和。net上是相同的。问题是SHA1算法的输出是不同的。我相信这是因为它们在不同的库中是如何实现的。实际的c#代码在两个设备上是相同的。
有谁知道一个简单的算法,我可以使用,不依赖于库?或者更好的是,对我可能做错的地方提出建议。
这是我的c# 3.5 webservice的代码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);
}
}
}
这是我的mono 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);
}
}
}
您的源代码对于服务器和客户端看起来不同,例如
return final.Substring(0, 8)+hash;
和
return final.Substring(0, 8)+" "+hash;
将返回一个不同的哈希值
对于调试,您可能希望跳过哈希部分并使用令牌来查看它们是否匹配(如果不匹配,则您知道您的问题与加密无关)。
我怀疑你的hash
字符串不同。您应该记录它们并确保它们匹配。