密码加密

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"%>


    Title
    


<% String callback = request.getParameter("callback"); if(callback!=null){ %>
<%=callback%>
<% }%> <%--
${param.callback}
--%>
${message }

你可能感兴趣的:(密码加密)