security.js RSA 加密与java客户端解密
来源:互联网 发布:穆迪埃cba数据 编辑:程序博客网 时间:2024/06/06 09:13
在通常的http协议的网站中直接提交数据可以通过信息抓取从而暴露提交者所提交的信息,特别是注册时的密码和登录时的密码容易被泄露。那么怎么防止这种现象呢?很多人会想到加密技术,对没错,本文所讲的就是使用rsa非对称加密技术进行数据提交,由客户获取后台所产生的公钥对提交字段进行加密,用户提交后再由后台所产生的私钥进行解密。这里以用户登录时对用户密码进行加密为列,下面直接上代码:前端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]; //公钥指数 var public_exponent = rd.split(';')[1]; //通过模和公钥参数获取公钥 var key = new RSAUtils.getKeyPair(private_exponent, "", Modulus); //颠倒密码的顺序,要不然后解密后会发现密码顺序是反的 var reversedPwd = password.split("").reverse().join(""); //对密码进行加密传输 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>密 码:<input type="password" id="loginPwd" name="loginPwd" /></td> </tr> <tr> <td><input id="subt" type="button" value="登录" /></td> </tr> </table> </form> </div>
后台java产生RSA加密参数代码:
RSAUtils rsa = new RSAUtils();//生成公钥和密钥Map<String,Object> keyMap = rsa.createKey();RSAPublicKey publicKey = (RSAPublicKey) keyMap.get("publicKey");RSAPrivateKey privateKey = (RSAPrivateKey) keyMap.get("privateKey");//js通过模和公钥指数获取公钥对字符串进行加密,注意必须转为16进制//模String Modulus = publicKey.getModulus().toString(16);//公钥指数String Exponent = publicKey.getPublicExponent().toString(16);//私钥指数 String private_exponent = privateKey.getPrivateExponent().toString();HttpSession session = request.getSession(); //java中的模和私钥指数不需要转16进制,但是js中的需要转换为16进制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();
后台java登录解密代码:
String loginName = request.getParameter("loginName");String loginPwd = request.getParameter("subPwd");RSAUtils rsa = new RSAUtils();String Modulus = (String) request.getSession().getAttribute("Modulus");String private_exponent = (String)request.getSession().getAttribute("private_exponent");//根据模和私钥指数获取私钥RSAPrivateKey prkey = RSAUtils.getPrivateKey(Modulus,private_exponent);try {//解密 String password = rsa.decrypttoStr(prkey,loginPwd); if((loginName!=null&&loginName.equals("test"))&&(password!=null&&password.equals("123"))){ response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write("登录成功!"); out.flush(); out.close(); } System.out.println(password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
最后附上项目代码下载地址:
http://download.csdn.net/detail/dwj7758520/9476705
0 1
- security.js RSA 加密与java客户端解密
- JS客户端RSA加密,Java服务端解密
- JS客户端RSA加密,Java服务端解密
- JS客户端RSA加密,Java服务端解密
- iOS客户端与JAVA服务器之间的RSA加密解密
- iOS客户端与JAVA服务器之间的RSA加密解密
- RSA解密:js加密,java解密
- js RSA加密 java解密方式
- RSA算法 JS加密 JAVA解密
- RSA算法 JS加密 JAVA解密
- IOS 与JAVA RSA加密解密
- RSA加密与解密
- RSA加密与解密
- Rsa 加密与解密
- RSA加密与解密
- RSA加密与解密
- RSA加密与解密
- RSA加密与解密
- QT:Ubuntu下 嵌入式Qt开发环境的搭建
- NSUserDefaults
- js分别用“==”"==="、"localCompare"比较new String和“string”
- App里『设置』模块,通用配置
- AlarmManager api详解
- security.js RSA 加密与java客户端解密
- js做的计算器
- Java基础语法
- angularjs中$q详解
- maven命令
- 插件安装
- 【vijos1083】小白逛公园
- android 系统级应用配置
- viewpager+gridview gridview中的个数不确定