赞
踩
离开上家公司后才发现上家公司的网站 用户登录 对密码是没有任何加密的形式,自己整理了一下对密码加密的方法 如下:
不加密:
我们使用网络探测器能轻易的获取用户的用户名密码,如果这些信息被不发份子获得,后果可想而知;
这里我们可以看到 密码是以明文的方式 被网络探测器 嗅到
现对密码进行加密,如果对用户也不希望明文显示也可以进行加密:代码如下:
java 后台代码:
生成公钥方法:
- /**
- * 生成公钥
- * @return
- * @throws Exception
- */
- @ClearInterceptor(ClearLayer.ALL)
- public void Rdspwd() throws Exception{
- HttpServletResponse response = getResponse();;
- PrintWriter writer = response.getWriter();
- String publicKey = RSAUtils.generateBase64PublicKey();
- writer.write(publicKey);
- renderNull();
- }
页面代码:我们在页面一加载的时候就Ajax请求生成公钥的方法
需要先在Javascript 中声明 公钥变量
- //获取public key
- var publicKey = null;
- <button type="button" id="login-btn" class="width-35 btn btn-sm btn-primary" οnclick="doLogin()">
- <i class="ace-icon fa fa-key"></i>
- <span class="bigger-110" >登录</span>
- </button>
function getPublicKey(dologin){ $.ajax({ url: "/login/Rdspwd", type: "post", dataType: "text", success: function(data) { if(data) publicKey = data; if(dologin==1){ if(publicKey==null){ $("#msg").html("获取publicKey失败,请联系管理员!"); $("#login-btn").removeAttr("disabled"); }else{ doLogin(1); } } } }); }
- var ustring = $.trim($("#ustring").val());
- var pstring = $.trim($("#pstring").val());
//进行加密 var encrypt = new JSEncrypt(); if(publicKey != null){ encrypt.setPublicKey(publicKey); var password = encrypt.encrypt(pstring); var username = encrypt.encrypt(ustring); //提交之前,检查是否已经加密。假设用户的密码不超过20位,加密后的密码不小于20位 if(password.length < 20) { //加密失败提示 alert("登录失败,请稍后重试..."); }else{ $.ajax({ url: "${contextPath}/dologin", type: "post", data: {"usname": username,"pwd": password,"vcstring": vcstring}, dataType: "json", } }
- //解密帐号
- username = RSAUtils.decryptBase64(username);
- //解密密码
- password = RSAUtils.decryptBase64(password);
RSAUtils 类:
- public class RSAUtils {
-
-
- //KeyPair is a simple holder for a key pair.
- private static final KeyPair keyPair = initKey();
- /**
- * 初始化方法,产生key pair,提供provider和random
- * @return KeyPair instance
- */
- private static KeyPair initKey() {
-
- try {
- //添加provider
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
- Security.addProvider(provider);
- //产生用于安全加密的随机数
- SecureRandom random = new SecureRandom();
-
- KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", provider);
- generator.initialize(1024, random);
- return generator.generateKeyPair();
- } catch(Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 产生public key
- * @return public key字符串
- */
- public static String generateBase64PublicKey() {
- PublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
-
- //encodeBase64(): Encodes binary data using the base64
- //algorithm but does not chunk the output.
- //getEncoded():返回key的原始编码形式
- return new String(Base64.encodeBase64(publicKey.getEncoded()));
- }
- /**
- * 解密数据
- * @param string 需要解密的字符串
- * @return 破解之后的字符串
- */
- public static String decryptBase64(String string) {
- //decodeBase64():将Base64数据解码为"八位字节”数据
- return new String(decrypt(Base64.decodeBase64(string.getBytes())));
- }
-
- private static byte[] decrypt(byte[] byteArray) {
- try {
- Provider provider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
- Security.addProvider(provider);
- //Cipher: 提供加密和解密功能的实例
- //transformation: "algorithm/mode/padding"
- Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", provider);
- PrivateKey privateKey = keyPair.getPrivate();
- //初始化
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- //doFinal(): 加密或者解密数据
- byte[] plainText = cipher.doFinal(byteArray);
- return plainText;
- } catch(Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- }
前台页面加载时调用生成公钥方法 生成公钥, 然用户点击登录时获取用户输入的用户名、密码,根据之前生成的公钥 进行私钥加密,传入后台的数据是已经加过密的数据, 然后我们在后台在进行解密 ,得到用户输入的原始密码, 后面的就可以根据自己的业务需求进行处理。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。