JS:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>RSA Login Test</title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></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> <script type="text/javascript"> function cmdEncrypt() { setMaxDigits(129); var key = new RSAKeyPair("<%=strPublicKeyExponent%>", "", "<%=strPublicKeyModulus%>"); var pwdMD5Twice = $.md5($.md5($("#txtPassword").attr("value"))); var pwdRtn = encryptedString(key, pwdMD5Twice); $("#encrypted_pwd").attr("value", pwdRtn); $("#formLogin").submit(); return; } </script> </head> <body> <form action="Default.aspx" id="formLogin" method="post"> <div> <div> User Name: </div> <div> <input id="txtUserName" name="txtUserName" value="<%=postbackUserName%>" type="text" maxlength="16" /> </div> <div> Password: </div> <div> <input id="txtPassword" type="password" maxlength="16" /> </div> <div> <input id="btnLogin" type="button" value="Login" onclick="return cmdEncrypt()" /> </div> </div> <div> <input type="hidden" name="encrypted_pwd" id="encrypted_pwd" /> </div> </form> <div> <%=LoginResult%> </div> </body> </html>
C#:
static void Main(string[] args) { //用于字符串和byte[]之间的互转 UTF8Encoding utf8encoder = new UTF8Encoding(); //产生一对公钥私钥 RSACryptoServiceProvider rsaKeyGenerator = new RSACryptoServiceProvider(1024); string publickey = rsaKeyGenerator.ToXmlString(false); string privatekey = rsaKeyGenerator.ToXmlString(true); //使用公钥加密密码 RSACryptoServiceProvider rsaToEncrypt = new RSACryptoServiceProvider(); rsaToEncrypt.FromXmlString(publickey); string strPassword = "@123#abc$"; Console.WriteLine("The original password is: {0}", strPassword); byte[] byEncrypted = rsaToEncrypt.Encrypt(utf8encoder.GetBytes(strPassword), false); Console.Write("Encoded bytes: "); foreach (Byte b in byEncrypted) { Console.Write("{0}", b.ToString("X")); } Console.Write("\n"); Console.WriteLine("The encrypted code length is: {0}", byEncrypted.Length); //解密 RSACryptoServiceProvider rsaToDecrypt = new RSACryptoServiceProvider(); rsaToDecrypt.FromXmlString(privatekey); byte[] byDecrypted = rsaToDecrypt.Decrypt(byEncrypted, false); string strDecryptedPwd = utf8encoder.GetString(byDecrypted); Console.WriteLine("Decrypted Password is: {0}", strDecryptedPwd); }
JS中的加密解密操作,以及对应的C#中的加密解密
时间: 2024-10-13 21:17:33