RSA非对称性前端加密后端解密
前端加密代码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>RSA非对称加密登录</title> <script src="~/JS/jquery-1.8.1.min.js"></script><script src="~/JS/RSA.js"></script> <script src="~/JS/BigInt.js"></script> <script src="~/JS/Barrett.js"></script> <script type="text/javascript"> $(function () { index.init(); }); var index = function () { setMaxDigits(129); //第一个参数为加密指数、第二个参数为解密参数、第三个参数为加密系数 var key_to_encode = new RSAKeyPair("010001", "", "A12A08F50D3B15E2BA7658C49A6CD068E3BB8AB439B8E5D125904F5C954AD7553CE712046182D299E3831516A2477578D86BA9D0C5C70D78FFCACCFD040C430A7B53729D517139B893DB7B228759D44BE5A3A5AE870287A2B5F165EE698EEAB9FEF345F82D27D789F0E5F79490AB324A4EDEE11A76A38FCB6E05F64335D2A60D"); var _initEvent = function () { $("#submit").click(function () { submitData(); }); } function submitData() { var userName = $("#userName").val(); var pwd = $("#pwd").val(); pwd = encryptedString(key_to_encode, pwd) $.ajax({ url: "/Home/Login", data: { "userName": userName, "pwd": pwd }, datatype: "json", type: "get", success: function (json) { $("#result").html("登录结果:" + json.result + "<br/>前端传入密码:" + json.InPwd + "<br/>后端解密后:" + json.OutPwd); } }); } return { init: function () { _initEvent(); } } }(); </script> </head> <body> 用户名:<input type="text" id="userName" value="张三" /><br /><br /> 密 码:<input type="text" id="pwd" value="12345677" /><br /><br /> <input type="button" id="submit" value="提交" /> <p id="result"></p> </body> </html>
后端登录处理和解密代码
public ActionResult Login(string userName, string pwd) { string outPwd = RSADecrypts.RSADecrypt(pwd); if (outPwd == "12345677" && userName == "张三") { return Json(new { result = "登录成功", InPwd = pwd, OutPwd = outPwd }, JsonRequestBehavior.AllowGet); } else { return Json(new { result = "用户名或密码不对", InPwd = pwd, OutPwd = outPwd }, JsonRequestBehavior.AllowGet); } } public static string RSADecrypt(string pToDecrypt) { string privateKey = "<RSAKeyValue><Modulus>oSoI9Q07FeK6dljEmmzQaOO7irQ5uOXRJZBPXJVK11U85xIEYYLSmeODFRaiR3V42Gup0MXHDXj/ysz9BAxDCntTcp1RcTm4k9t7IodZ1Evlo6WuhwKHorXxZe5pjuq5/vNF+C0n14nw5feUkKsySk7e4Rp2o4/LbgX2QzXSpg0=</Modulus><Exponent>AQAB</Exponent><P>0nRJAznZ9iAUkCkW//b9sQ5KquqRqqTe8/jp2ZAkCXuGz7WNCMQsSX/doJY0Dfum5MDeq0Y4ShB/LRSWhrKSrw==</P><Q>xArz6MLHAHOY7z76pA5o10mERKp7z4yhm/wFa9ghgy3Q3bQAfotRsLbT16ELmXqFi1Wo0Z2CprtZpVMsaGdyAw==</Q><DP>n0/t8WY6zCmOx26DfbKwavbBqU62hHIYYKG+DLcjwJXlAJmTsN4oFF+aGzV5JFirByHFrewTMmX4j9y08jR9+w==</DP><DQ>XDg+xrahgE/gmGLK6tOwc92sb9S2y0p/V241aqbQFPI/d5tORDjrY7KNcrjaSQd+vK297WUn6aDmKcQ3dqF62w==</DQ><InverseQ>N1WISfI5sfIovGeRCpTjz0kGfCuqURyjU+MxCD+xmCCgDcNKmSiDaUzKprnoCZbFd5IvSYGFSpPSdtpQ3qcVRw==</InverseQ><D>HgwCuIE7e1SWk/rvyKSx5GRU8kKwok7ChADYMvgoiGX9YU1EINPtpMA8j6F1JuJ5YcwM0H4XIuVktzApmegZVV6oS8xgdiWWixYufc8MThyP5FEBBd4LxPkVDSBsO3v8mdsWAZIi+Gup+SCnyrNqDp2a984/dfwyxWPj1PmacYc=</D></RSAKeyValue>"; CspParameters cspParameters = new CspParameters(); cspParameters.KeyContainerName = "containerName"; cspParameters.KeyNumber = 1; cspParameters.Flags = CspProviderFlags.UseMachineKeyStore; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParameters)) { rsa.FromXmlString(privateKey); byte[] result = rsa.Decrypt(HexStringToBytes(pToDecrypt), false); System.Text.ASCIIEncoding enc = new ASCIIEncoding(); return enc.GetString(result); } } private 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; }
生成公钥私钥代码
private 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 void CreateKey(out string publicKey, out string privateKey) { RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024); RSAParameters parameter = rsaProvider.ExportParameters(false); publicKey = BytesToHexString(parameter.Exponent) + "," + BytesToHexString(parameter.Modulus); // publicKeyXML = rsa.ToXmlString(true); privateKey = rsaProvider.ToXmlString(true); } }
注:RSA只是非对称加密算法的一种,在非对称加密中使用的主要算法有:RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法)等,对称加密算法中常用的算法有:DES、3DES、TDEA、Blowfish、RC2、RC4、RC5、IDEA、SKIPJACK、AES等
附Demo地址:http://download.csdn.net/detail/fcydxbd/9724553
参考地址:http://blog.csdn.net/dslinmy/article/details/37362661