java的几种常用的加密

1、BASE64 

Base64定义:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常见于邮件、http加密,截取http信息,你就会发现登录操作的用户名、密码字段通过BASE64加密的。





实例:


Java代码 复制代码 收藏代码
1.package cn.tzz.java.crypto; 
2. 
3.import java.io.IOException; 
4. 
5.import sun.misc.BASE64Decoder; 
6.import sun.misc.BASE64Encoder; 
7. 
8.public class BASE64Util { 
9.    /**加密*/  
10.    public static String encrypt(byte[] key) {   
11.        return (new BASE64Encoder()).encodeBuffer(key);   
12.    } 
13.     
14.    /**解密*/ 
15.    public static String decrypt(String key) {   
16.        try { 
17.            return new String((new BASE64Decoder()).decodeBuffer(key),"utf-8"); 
18.        } catch (IOException e) { 
19.            e.printStackTrace(); 
20.        } 
21.        return null; 
22.    } 
23.     
24.    public static void main(String[] args) { 
25.        String encryptStr = encrypt("123456".getBytes()); 
26.        System.out.println("加密:"+encryptStr); 
27.        System.out.println("解密:"+decrypt(encryptStr)); 
28.    } 
29.} 


2、MD5&SHA


MD5 -- message-digest algorithm 5 (信息-摘要算法)

SHA(Secure Hash Algorithm,安全散列算法)




 





Java代码 复制代码 收藏代码
1.package cn.tzz.java.crypto.simple; 
2. 
3.import java.security.MessageDigest; 
4.import java.security.NoSuchAlgorithmException; 
5. 
6.public class MD5SHACryptoUtil { 
7. 
8.    /**md5加密*/ 
9.    public static String md5Encrypt(String str) { 
10.        String md5String = null; 
11.        try { 
12.            // 获得MD5摘要算法的 MessageDigest 对象 
13.            MessageDigest md = MessageDigest.getInstance("MD5"); 
14.            // 使用指定的字节更新摘要 
15.            md.update(str.getBytes()); 
16.            // 获得密文,把密文转换成十六进制的字符串形式 
17.            //方式一 
18.            md5String = byte2Hex(md.digest()); 
19.            //方式二 
20.            //md5String = byteToHex(md.digest()); 
21.            //方式三 
22.            //md5String = byteToString(md.digest()); 
23.        } catch (Exception ex) { 
24.            ex.printStackTrace(); 
25.        } 
26.        return md5String; 
27.    } 
28.     
29.    /**SHA1加密*/ 
30.    public static String shaEncrypt(String date) { 
31.        byte[] digest = null; 
32.        String rs = null; 
33.        try { 
34.            // 得到一个SHA-1的消息摘要 
35.            MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); 
36.            // 添加要进行计算摘要的信息 
37.            messageDigest.update(date.getBytes()); 
38.            // 得到该摘要 
39.            digest = messageDigest.digest(); 
40.            // 将摘要转为字符串 
41.            rs = byte2Hex(digest); 
42.        } catch (NoSuchAlgorithmException e) { 
43.            e.printStackTrace(); 
44.        } 
45.        return rs; 
46.    } 
47.     
48.    // 1.把密文转换成十六进制的字符串形式(Integer.toHexString函数) 
49.    public static String byte2Hex(byte[] b) { 
50.        StringBuffer sb = new StringBuffer(); 
51.        String tmp = ""; 
52.        for (int i = 0; i < b.length; i++) { 
53.            tmp = Integer.toHexString(b[i] & 0XFF); 
54.            if (tmp.length() == 1){ 
55.                sb.append("0"); 
56.            } 
57.            sb.append(tmp); 
58.        } 
59.        return sb.toString(); 
60.    } 
61.     
62.    // 2.把密文转换成十六进制的字符串形式(自定义) 
63.    public static String byteToHex(byte[] b) { 
64.        // 全局数组 
65.        char[] hexDigits = {'0' , '1' , '2' , '3' , '4' , '5' ,'6' , '7' , '8' , '9' , 'a' , 'b' , 
66.                'c' , 'd' , 'e' , 'f'}; 
67.        // 把密文转换成十六进制的字符串形式 
68.        int j = b.length; 
69.        char str[] = new char[j * 2]; 
70.        int k = 0; 
71.        for (int i = 0; i < j; i++) { 
72.            byte byte0 = b[i]; 
73.            str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 
74.            str[k++] = hexDigits[byte0 & 0xf]; 
75.        } 
76.        return new String(str); 
77.    } 
78.     
79.    // 3.转换字节数组为16进制字串 
80.    private static String byteToString(byte[] b) { 
81.        StringBuffer sBuffer = new StringBuffer(); 
82.        for (int i = 0; i < b.length; i++) { 
83.            sBuffer.append(byteToArrayString(b[i])); 
84.        } 
85.        return sBuffer.toString(); 
86.    } 
87.     
88.    // 返回形式为数字跟字符串 
89.    private static String byteToArrayString(byte b) { 
90.        String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 
91.        int iRet = b; 
92.        if (iRet < 0) { 
93.            iRet += 256; 
94.        } 
95.        int iD1 = iRet / 16; 
96.        int iD2 = iRet % 16; 
97.        return strDigits[iD1] + strDigits[iD2]; 
98.    } 
99.     
100.    public static void main(String[] args) { 
101.        System.out.println(md5Encrypt("123456")); 
102.        System.out.println(shaEncrypt("123456")); 
103.    } 
104.} 

猜你喜欢

转载自weitao1026.iteye.com/blog/2266328