首页 > 代码库 > JS中的加密解密操作,以及对应的C#中的加密解密
JS中的加密解密操作,以及对应的C#中的加密解密
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);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。