Commons Codec
来自于Apache.commons.codec
public class Text {
public static void main(String[] args) {
//加密算法可以验证两个文件是否为同一个文件,把两个文件的额输入流放入MD5HEX中
try {
FileInputStream fileInputStream1 = new FileInputStream("D:/1.txt");
FileInputStream fileInputStream2 = new FileInputStream("D:/2.txt");
String str1 = DigestUtils.md5Hex(fileInputStream1);
String str2 = DigestUtils.md5Hex(fileInputStream2);
System.out.println(str1.equals(str2));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//给密码加盐,更加安全
String salt = "$%^%@%$#%!*I(@*(#^#@%#^$@%";
String password = salt + "123";
//MD5是不可逆算法,密文无法推算明文
//b1e6ac1934b89a1ad4900603795bd411
String src = DigestUtils.md5Hex(password);
System.out.println(src);
//sha算法密文长度更长 sha1Hex sha256Hex sha512Hex
//d14402eb5075fbdc83af18ae5a69a8572d4418f7
//d8c9cc46ba3576820eb1c550ed359be571aeb5e0d9290cf5e00fd7bcf33bd3cc
//7071c9d9ac821101fe21646c898ebb3eecc1ec481d5fa84d18260120c59317f8d129bb053c90920385523aee538c50e8729700f679b22a8e6d9f6dad995edda2
String src1 = DigestUtils.sha1Hex(password);
System.out.println(src1);
}
}
Javascript加密库
http://code.google.com/p/crypto-js/
在客户端加密,可以做到密码在传输过程中为密文
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-5">
<%
String callback = request.getParameter("callback");
if(callback!=null){
%>
<div class="alert alert-danger"><%=callback%></div>
<% }%>
<%-- <c:if test="${not empty param.callback}">
<div class="alert alert-danger">${param.callback}</div>
</c:if>--%>
<c:if test="${not empty message}">
<div class="alert alert-danger">${message }</div>
</c:if>
<form action="/login" method=post id="loginForm">
<div class="form-group">
<label>账号</label>
<input type="text"name ="username" id="username" value="${username }" class="form-control">
</div>
<div class="form-group">
<label>密码</label>
<input type="password"name ="password" id = "paw" class="form-control">
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="remember" id="remember" class="btn btn-info">记住账号
</label>
</div>
<div class="form-group">
<button type="button" class="btn btn-info" id="button">登录</button>
</div>
</form>
</div>
</div>
</div>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script src="/static/js/jquery.cookie.js"></script>
<script src="/static/js/cryptojs/rollups/md5.js"></script>
<script>
//$("#username").val($.cookie("username"));
$("#button").click(function () {
/*客户端记录密码功能*/
/* if($("#remember")[0].checked){
$.cookie("username",$("#username").val(),{expires:7,path:"/"})
}*/
/*获取password输入值,转换为MD5,替换原有明文,提交是以密文进行提交*/
var password = $("#paw").val();
password = CryptoJS.MD5(password);
$("#paw").val(password);
$("#loginForm").submit();
});
</script>
</body>
</html>
网友评论