• 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("
    ");
        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);
    }
  • 相关阅读:
    负载均衡,分布式,集群的理解,多台服务器代码如何同步
    浅谈控制反转与依赖注入
    Jetbrains系列产品重置试用方法
    讲述一下一个还没毕业的科班程序猿的求职历程
    一个在求职路上挣扎的萌新
    用C#操作文件/文件夹(删除,复制,移动)
    随笔-关于公网IP无法访问服务器的解决办法
    C/s从文件(TXT)中读取数据插入数据库
    Sql Server + ADO.NET
    进程。线程与线程池
  • 原文地址:https://www.cnblogs.com/wenghaowen/p/3912132.html
Copyright © 2020-2023  润新知