溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么實(shí)現(xiàn)security.js RSA加密與java客戶端解密

發(fā)布時(shí)間:2021-12-14 15:05:38 來源:億速云 閱讀:265 作者:柒染 欄目:互聯(lián)網(wǎng)科技

本篇文章給大家分享的是有關(guān)怎么實(shí)現(xiàn)security.js RSA加密與java客戶端解密,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

  在通常的http協(xié)議的網(wǎng)站中直接提交數(shù)據(jù)可以通過信息抓取從而暴露提交者所提交的信息,(伍子胥:l47可181O微51l3可微)   特別是注冊時(shí)的密碼和登錄時(shí)的密碼容易被泄露。那么怎么防止這種現(xiàn)象呢?很多人會(huì)想到加密技術(shù),對沒錯(cuò),本文所講的就是使用rsa非對稱加密技術(shù)進(jìn)行數(shù)據(jù)提交,由客戶獲取后臺(tái)所產(chǎn)生的公鑰對提交字段進(jìn)行加密,用戶提交后再由后臺(tái)所產(chǎn)生的私鑰進(jìn)行解密。這里以用戶登錄時(shí)對用戶密碼進(jìn)行加密為列,下面直接上代碼:

  前端js代碼:

<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>

    <script type="text/javascript" src="js/security.js"></script>

    <script type="text/javascript">

    $(function(){

        $('#subt').click(function(){

                var name = jQuery('#loginName').val();

                var password =jQuery('#loginPwd').val();

                if(name==null||name==""){

                    alert("用戶名不得為空!");

                    return;

                }

                if(password==null||password==""){

                    alert("密碼不得為空!");

                    return;

                }

                jQuery.ajax({

                type:"post",

                url:"loginset",

                success:function(rd){

                    if(rd!=null){

                        //加密模

                        var Modulus = rd.split(';')[0];

                        //公鑰指數(shù)

                        var public_exponent = rd.split(';')[1];

                        //通過模和公鑰參數(shù)獲取公鑰

                        var key = new RSAUtils.getKeyPair(private_exponent, "", Modulus);

                        //顛倒密碼的順序,要不然后解密后會(huì)發(fā)現(xiàn)密碼順序是反的

                        var reversedPwd = password.split("").reverse().join("");

                        //對密碼進(jìn)行加密傳輸 

                        var encrypedPwd = RSAUtils.encryptedString(key,reversedPwd);

                        jQuery('#subPwd').val(encrypedPwd);

                        jQuery('#loginPwd').val("");

                        jQuery('#login').submit();

                    }

                }

            })

        })

    })

    </script>

  前端html代碼:

<div style="text-align: center;">

  <form id="login" action="login" method="post">

   <input type="hidden" id="subPwd" name="subPwd" />

        <table align="center">

            <tr>

                <td>登錄</td>

            </tr>

            <tr>

                <td>用戶名:<input type="text" id="loginName" name="loginName" /></td>

            </tr>

            <tr>

                <td>密&nbsp;&nbsp;&nbsp;碼:<input type="password" id="loginPwd" name="loginPwd" /></td>

            </tr>

            <tr>

                <td><input id="subt" type="button" value="登錄" /></td>

            </tr>

        </table>

    </form>

 </div>

后臺(tái)java產(chǎn)生RSA加密參數(shù)代碼:

RSAUtils rsa = new RSAUtils();

//生成公鑰和密鑰

Map<String,Object> keyMap = rsa.createKey();

RSAPublicKey publicKey = (RSAPublicKey) keyMap.get("publicKey");

RSAPrivateKey privateKey = (RSAPrivateKey) keyMap.get("privateKey");

//js通過模和公鑰指數(shù)獲取公鑰對字符串進(jìn)行加密,注意必須轉(zhuǎn)為16進(jìn)制

//模

String Modulus = publicKey.getModulus().toString(16);

//公鑰指數(shù)

String Exponent = publicKey.getPublicExponent().toString(16);

//私鑰指數(shù)    

String private_exponent = privateKey.getPrivateExponent().toString();

HttpSession session = request.getSession();

      //java中的模和私鑰指數(shù)不需要轉(zhuǎn)16進(jìn)制,但是js中的需要轉(zhuǎn)換為16進(jìn)制

session.setAttribute("Modulus",publicKey.getModulus().toString());

session.setAttribute("private_exponent",private_exponent);

String strSet = Modulus+";"+Exponent;

response.setContentType("text/html;charset=UTF-8");

response.setCharacterEncoding("UTF-8");

PrintWriter out = response.getWriter();

out.write(strSet);

out.flush();

以上就是怎么實(shí)現(xiàn)security.js RSA加密與java客戶端解密,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI