jsencrypt表单加密

版权声明:学习中。。。 https://blog.csdn.net/fangdengfu123/article/details/69398047
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>jsencrypt的使用</title>
        <script src="js/jquery-2.0.3.min.js"></script>
        <script src="js/jsencrypt.min.js"></script>
    </head>
    <body>
        <div style="width: 50%;float: left;">
        <label for="message">下面简单介绍jsencrypt的基本使用</label><br />
        <textarea id="message" name="message" rows="20" cols="60">
            创建秘钥对JKS格式keystore:
            keytool -genkey -v -alias test -keyalg RSA -keystore test.jks

            将JKS格式keystore转换成PKCS12证书文件:
            keytool -importkeystore -srckeystore test.jks -destkeystore test.p12 -srcstoretype JKS -deststoretype PKCS12<br />

            使用OpenSSL工具从PKCS12证书文件导出密钥对:
            openssl pkcs12 -in test.p12 -nocerts -nodes -out test.key

            从密钥对中提取公钥:
            openssl rsa -in test.key -pubout -out test_public.pem
        </textarea><br />
        </div>
        <div>
        <label for="publicKey">Public Key</label><br />
        <textarea id="publicKey" name="publicKey" rows="20" cols="60">
            -----BEGIN RSA PUBLIC KEY-----
            MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
            WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
            aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
            AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
            xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
            m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
            -----END RSA PUBLIC KEY-----
        </textarea>
        <br />
        <label for="input">jsencrypt:</label><br />
        name:<input type="text" name="username" id="username" value="username" /><br />
        password:<input type="password" name="password" id="password" value="password" /><br />
        <input type="button" id="submit" value="submit" />
        </div>
        <script type="text/javascript">
            $(function(){
                $('submit').click(function(){
                    var data = [];
                    data['username'] = $('#username').val();
                    data['password'] = $('#password').val();

                    var publicKey = $('#publicKey').val();
                    encryptSend('./Jsencrypt.do',data,publicKey);
                    //Jsencrypt.do对应服务端处理地址
                });
            });

            //使用jsencrypt库加密前段参数
            function enctyptSend(url,data,publicKey){
                var jsencrypt = new JSEncrypt();
                jsencrypt.setPublicKey(publicKey);

                //enData用来装载加密后的数据
                var enData = new Object();

                //将参数用jsencrypt加密后付给enData
                for (var key in data) {
                    enData[key] = jsencrypt.encrypt(data[key]);
                }

                $.ajax({
                    type : "post",
                    url : "url",
                    data : enData,
                    dataType : 'json',
                    success : function(data){
                        console.info(data);
                    },
                    error : function(xhr){
                        console.error('Something went wrong...');
                    }
                });
            }
        </script>
    </body>
</html>
展开阅读全文

没有更多推荐了,返回首页