rsa加密

来源:互联网 发布:python 教程 hetland 编辑:程序博客网 时间:2024/05/16 03:37
基本思路:
1.后台给一个公钥对 传给前台
2.前台拿到后台给的公钥对进行密码加密
3.加密以后把值给隐藏域,后台可以通过form表单接受值
4.后台拿到值后进行解密
5.拿到解密的值 去数据库进行比对。
6.完成
 
前台代码:
<script src="~/Scripts/jquery-1.8.2.js"></script><script src="Scripts/jQuery.md5.js" type="text/javascript"></script>
<script src="Scripts/BigInt.js" type="text/javascript"></script>
<script src="Scripts/RSA.js" type="text/javascript"></script>
<script src="Scripts/Barrett.js" type="text/javascript"></script>
<html>
<head >
<title>RSA Login Test</title>
</head>
<body>
<form action="../Home/Result" id="formLogin" method="post">
<div>
<div>
<input id="txtPassword" name="txtPassword" type="text" maxlength="16" />
</div>
<div>
<input id="btnLogin" type="button" value="Login" />
</div>
</div>
<div>
<input type="hidden" name="encrypted_pwd" id="encrypted_pwd" />
</div>
</form>
<div>
</div>
</body>
</html>
<script type="text/javascript">
$(function () {
$('#btnLogin').click(function () {
ckSub();
})
})
function ckSub() {
// 对md5用rsa加密
setMaxDigits(130);
//@ViewData["Exponent"]@ViewData["Modulus"]代表后台传过来的键值对
var key = new RSAKeyPair("@ViewData["Exponent"]", "", "@ViewData["Modulus"]");
console.log('key',key);
//取到密码的值进行md5加密
var pwdMD5 = $.md5($("#txtPassword").attr("value"));
//返回加密后的值
var result = encryptedString(key, pwdMD5);
//隐藏域的值,随表单提交 后台取值
$("#encrypted_pwd").val(result);
//提交表单
$("#formLogin").submit();
}
</script>
//后台代码
/// 刚加载的时候私钥保存session,公钥穿后台
/// </summary>
/// <param name="f"></param>
/// <returns></returns>
public ActionResult Index(FormCollection f)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//把公钥适当转换,准备发往客户端
RSAParameters parameter = rsa.ExportParameters(true);
ViewData["Exponent"] = RsaHelper.BytesToHexString(parameter.Exponent);
ViewData["Modulus"] = RsaHelper.BytesToHexString(parameter.Modulus);
//将私钥存Session中
Session["private_key"] = rsa.ToXmlString(true);
return View();
}
//表单提交走的action
public ActionResult Result(FormCollection f)
{
string strPwdToDecrypt = f["encrypted_pwd"];
//进行解码
string result = RsaHelper.DecryptRSA(strPwdToDecrypt, Session["private_key"].ToString());
return RedirectToAction("Index");
}
//然后看RsaHelper,所有的rsa封装全在里面
static public class RsaHelper
{
/// <summary>
/// 私钥
/// </summary>
/// <returns></returns>
static public string PrivateKey()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
return RSA.ToXmlString(true);
}
/// <summary>
/// RSA解密字符串
/// </summary>
/// <param name="value">密文</param>
/// <param name="privateKey">私钥</param>
/// <returns>明文</returns>
public static string DecryptRSA(string value, string privateKey)
{
string decryptData = "";
try
{
var provider = new RSACryptoServiceProvider();
provider.FromXmlString(privateKey);
byte[] result = provider.Decrypt(RsaHelper.HexStringToBytes(value), false);
ASCIIEncoding enc = new ASCIIEncoding();
decryptData = enc.GetString(result);
}
catch (Exception e)
{
throw new Exception("RSA解密出错!", e);
}
return decryptData;
}
public static string BytesToHexString(byte[] input)
{
StringBuilder hexString = new StringBuilder(64);
for (int i = 0; i < input.Length; i++)
{
hexString.Append(String.Format("{0:X2}", input[i]));
}
return hexString.ToString();
}
public static byte[] HexStringToBytes(string hex)
{
if (hex.Length == 0)
{
return new byte[] { 0 };
}
if (hex.Length % 2 == 1)
{
hex = "0" + hex;
}
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
{
result[i] = byte.Parse(hex.Substring(2 * i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return result;
}
}

1 0
原创粉丝点击