JS中的加密解密操作,以及對應的C#中的加密解密

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   io   

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.