我试图让用户输入一个密码,然后使用一个公式来创建一个时间敏感的字符串。IE 如果您在一分钟后调用该函数,则字符串将完全不同。然后我用这个字符串通过 SHA1 算法运行它。我的问题是我需要 .Net 和 Mono 库来产生相同的结果,但它们不是。我正在使用 Xmarin studio 将单声道库部署到 android。我正在使用 .Net 3.5 框架来部署我的 Web 服务。
我已经确认作为参数传递给 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);
}
}
}