第一步: index.aspx页面代码
引用jQuery ,引用非对称jsencrypt
<script src="JS/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="JS/jsencrypt.js" type="text/javascript"></script>
添加控件
<form id="form1" class="login" runat="server" οnsubmit="return checkForm();">
公钥<input type="hidden" id="pub_key" value="Public_Key" />
加密后的密码:<input type="hidden" name="pwd" id='Password1' />
用户名: <asp:TextBox ID="txtUserName" runat="server" MaxLength="100" placeholder="支持用户名/手机号/邮箱"></asp:TextBox>
密码: <input id="txtPassowrd" type="password" autocomplete="off" placeholder="密码" οnkeydοwn="checkEnter(event);"/>
<asp:LinkButton ID="btnLogin" runat="server" CssClass="btn login-btn" OnClick="btnLogin_Click">登录</asp:LinkButton>
</form>
<script type="text/javascript" language="javascript">
function checkEnter(event) {
if (event.keyCode == 13) {
__doPostBack('<%= btnLogin.ClientID %>', '');
}
}
function checkForm() {
var value = $("#txtPassowrd").val();
var encrypt = new JSEncrypt();
encrypt.setPublicKey($("#pub_key").val());
var data = encrypt.encrypt(value);
$("#Password1").val(data);
return true;
}
</script>
第二步
/// <summary>
/// 登录方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLogin_Click(object sender, EventArgs e)
{
string password = Request.Form["pwd"];
password = RSAHelper.Login(password);
}
创建非对称帮助类RSAHelper
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Test.Common
{
public class RSAHelper
{
public static string Login(string pwd)
{
//密钥格式要生成pkcs#1格式的 而不是pkcs#8格式的
string privateKey = CommonFunction.TryGetAppSetting("Private_Key");
try
{
RSACryptoServiceProvider rsaCryptoServiceProvider = CreateRsaProviderFromPrivateKey(privateKey);
RSACryptoServiceProvider.UseMachineKeyStore = true;
//把+号,再替换回来
byte[] res = rsaCryptoServiceProvider.Decrypt(Convert.FromBase64String(pwd.Replace("%2B", "+")), false);
return Encoding.UTF8.GetString(res);
}
catch (Exception exception)
{
Log.ErrorLog.LogError(exception);
return "";
}
}
public static RSACryptoServiceProvider CreateRsaProviderFromPrivateKey(string privateKey)
{
var privateKeyBits = System.Convert.FromBase64String(privateKey);
var RSA = new RSACryptoServiceProvider();
CspParameters _cpsParameter;
_cpsParameter = new CspParameters();
_cpsParameter.Flags = CspProviderFlags.UseMachineKeyStore;
RSA = new RSACryptoServiceProvider(1024, _cpsParameter);
var RSAparams = new RSAParameters();
using (BinaryReader binr = new BinaryReader(new MemoryStream(privateKeyBits)))
{
byte bt = 0;
ushort twobytes = 0;
twobytes = binr.ReadUInt16();
if (twobytes == 0x8130)
binr.ReadByte();
else if (twobytes == 0x8230)
binr.ReadInt16();
else
throw new Exception("Unexpected value read binr.ReadUInt16()");
twobytes = binr.ReadUInt16();
if (twobytes != 0x0102)
throw new Exception("Unexpected version");
bt = binr.ReadByte();
if (bt != 0x00)
throw new Exception("Unexpected value read binr.ReadByte()");
RSAparams.Modulus = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.Exponent = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.D = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.P = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.Q = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.DP = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.DQ = binr.ReadBytes(GetIntegerSize(binr));
RSAparams.InverseQ = binr.ReadBytes(GetIntegerSize(binr));
}
RSA.ImportParameters(RSAparams);
return RSA;
}
public static int GetIntegerSize(BinaryReader binr)
{
byte bt = 0;
byte lowbyte = 0x00;
byte highbyte = 0x00;
int count = 0;
bt = binr.ReadByte();
if (bt != 0x02)
return 0;
bt = binr.ReadByte();
if (bt == 0x81)
count = binr.ReadByte();
else
if (bt == 0x82)
{
highbyte = binr.ReadByte();
lowbyte = binr.ReadByte();
byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
count = BitConverter.ToInt32(modint, 0);
}
else
{
count = bt;
}
while (binr.ReadByte() == 0x00)
{
count -= 1;
}
binr.BaseStream.Seek(-1, SeekOrigin.Current);
return count;
}
}
}