Basic ideas:
1. Background to a public key to the foreground
2. The front desk gets the public key to encrypt the password in the background.
3. Encrypt the value to the hidden field after the background can be accepted by the form form forms
4. After the background to get the value of decryption
5. Get the decrypted value to the database for comparison.
6. Complete
Front Code:
<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>
<title>rsa Login test</title>
<body>
<form action= ". /home/result "id=" Formlogin "method=" POST ">
<div>
<div>
<input id= "Txtpassword" name= "Txtpassword" type= "text" maxlength= "/>"
</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>
<script type= "Text/javascript" >
$ (function () {
$ (' #btnLogin '). Click (function () {
Cksub ();
})
})
function Cksub () {
RSA encryption for MD5
Setmaxdigits (130);
@ViewData ["Exponent"] @ViewData ["modulus"] represents the key-value pairs passed over the background
var key = new Rsakeypair ("@ViewData [" Exponent "]", "", "@ViewData [" modulus "]");
Console.log (' key ', key);
MD5 encryption by taking the value of the password
var pwdMD5 = $.md5 ($ ("#txtPassword"). attr ("value"));
Returns the encrypted value
var result = encryptedstring (key, pwdMD5);
Hides the value of the field, with the form submission background value
$ ("#encrypted_pwd"). Val (result);
Submit Form
$ ("#formLogin"). Submit ();
}
</script>
Background code
The private key is saved when the session is loaded, the public key is worn backstage
</summary>
<param name= "F" ></param>
<returns></returns>
Public ActionResult Index (formcollection f)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
Convert the public key appropriately, ready to be sent to the client
RSAParameters parameter = RSA. Exportparameters (TRUE);
viewdata["Exponent"] = rsahelper.bytestohexstring (parameter. Exponent);
viewdata["modulus"] = rsahelper.bytestohexstring (parameter. modulus);
Save the private key in the session
session["Private_key"] = RSA. Toxmlstring (TRUE);
return View ();
}
The form submits the action to go
Public ActionResult Result (formcollection f)
{
String strpwdtodecrypt = f["Encrypted_pwd"];
to decode
string result = Rsahelper.decryptrsa (Strpwdtodecrypt, session["Private_key"]. ToString ());
Return redirecttoaction ("Index");
}
And look at Rsahelper, all the RSA packages are all inside.
static public class Rsahelper
{
<summary>
Private
</summary>
<returns></returns>
static public string Privatekey ()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();
Return RSA. Toxmlstring (TRUE);
}
<summary>
RSA decryption string
</summary>
<param name= "value" > Redaction </param>
<param name= "Privatekey" > Private key </param>
<returns> Clear </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 decryption Error!", 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;
}
}
No place to upload attachments. In front of the JS public, Baidu can download.
It's an honor to be a mermaid.
C # MVC RSA encryption