PBE——Passwordbased encryption(基于密码加密)

  1 package com.ice.webos.util.security;
2
3 import java.math.BigInteger;
4 import java.security.Key;
5 import java.util.Random;
6
7 import javax.crypto.Cipher;
8 import javax.crypto.SecretKey;
9 import javax.crypto.SecretKeyFactory;
10 import javax.crypto.spec.PBEKeySpec;
11 import javax.crypto.spec.PBEParameterSpec;
12
13 /**
14 * PBE——Password-based encryption(基于密码加密)。 <br>
15 * 其特点在于口令由用户自己掌管,不借助任何物理媒体;<br>
16 * 采用随机数(这里我们叫做盐)杂凑多重加密等方法保证数据的安全性。<br>
17 * 是一种简便的加密方式。
18 *
19 * @author Ice_Liu
20 *
21 */
22 public class PBECryptUtil {
23 /**
24 * 支持以下任意一种算法
25 *
26 * <pre>
27 *
28 * PBEWithMD5AndDES
29 * PBEWithMD5AndTripleDES
30 * PBEWithSHA1AndDESede
31 * PBEWithSHA1AndRC2_40
32 * </pre>
33 */
34 public static final String ALGORITHM = "PBEWITHMD5andDES";
35
36 /**
37 * 盐初始化
38 *
39 * @return
40 * @throws Exception
41 */
42 public static byte[] initSalt() throws Exception {
43 byte[] salt = new byte[8];
44 Random random = new Random();
45 random.nextBytes(salt);
46 return salt;
47 }
48
49 /**
50 * 转换密钥<br>
51 *
52 * @param password
53 * @return
54 * @throws Exception
55 */
56 private static Key toKey(String password) throws Exception {
57 PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
58 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
59 SecretKey secretKey = keyFactory.generateSecret(keySpec);
60 return secretKey;
61 }
62
63 /**
64 * 加密
65 *
66 * @param data
67 * 数据
68 * @param password
69 * 密码
70 * @param salt
71 * 盐
72 * @return
73 * @throws Exception
74 */
75 public static byte[] encrypt(byte[] data, String password, byte[] salt) throws Exception {
76 Key key = toKey(password);
77 PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);
78 Cipher cipher = Cipher.getInstance(ALGORITHM);
79 cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
80 return cipher.doFinal(data);
81 }
82
83 /**
84 * 解密
85 *
86 * @param data
87 * 数据
88 * @param password
89 * 密码
90 * @param salt
91 * 盐
92 * @return
93 * @throws Exception
94 */
95 public static byte[] decrypt(byte[] data, String password, byte[] salt) throws Exception {
96 Key key = toKey(password);
97 PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);
98 Cipher cipher = Cipher.getInstance(ALGORITHM);
99 cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
100 return cipher.doFinal(data);
101 }
102
103 public static void main(String[] args) {
104 try {
105 CryptUtil.main(args);
106 System.out.println("****************************************");
107 System.out.println("=====PBE加密与解密=====");
108 String s = "阿伯才的覆盖";
109 String pwd = "abcdefg";
110 byte[] salt = initSalt();
111 byte[] c = encrypt(s.getBytes("UTF-8"), pwd, salt);
112 System.out.println(ALGORITHM + " 加密后:" + new BigInteger(c).toString(16));
113 c = decrypt(c, pwd, salt);
114 System.out.println(ALGORITHM + " 解密后:" + new String(c, "UTF-8"));
115
116 } catch (Exception e) {
117 // TODO Auto-generated catch block
118 e.printStackTrace();
119 }
120 }
121
122 }
原文地址:https://www.cnblogs.com/liubin0509/p/2331072.html