谁能告诉我怎么做?
我在c#中使用sha1进行了散列,但是如何使用javascript实现相同的散列功能?
我想用不同的技术来学习。 谢谢!
编辑: 我试过以下链接: http://coursesweb.net/javascript/sha1-encrypt-data_cs
我没理由,为什么这与c#&的SHA1CryptoServiceProvider的结果不同?提到的链接中的例子
这是代码,我尝试过: 1.上面链接中提到的例子(javascript) 2。
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button_ServerSide" onclick="Button1_Click" />
代码背后:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string strText = String.Empty;
strText = TextBox1.Text;
//SHA512CryptoServiceProvider encrypt = new SHA512CryptoServiceProvider();
SHA1CryptoServiceProvider encrypt = new SHA1CryptoServiceProvider();
byte[] encryptText = encrypt.ComputeHash(Encoding.Default.GetBytes(strText));
TextBox1.Text = "";
foreach (byte tempData in strText)
{
TextBox1.Text = TextBox1.Text + "x";
}
string str = System.Text.Encoding.Default.GetString(encryptText);
Response.Write("Entered Text: " + strText + " Encrypted Text Length: " + encryptText.Length + " enpwd: " + strText);
Response.Write(" encryptText: " + encryptText.Equals(strcrypt.Value));
Response.Write(" encryptText STR: " + str);
//ProtectedData.Protect();
}
}
答案 0 :(得分:2)
Javascript没有任何内置的散列函数,因此您必须使用外部库。
还有another question on StackOverflow这方面有更多信息。
答案 1 :(得分:1)