PBE安全编码组件 基于口令的加密---PBE:前面的对称加密几乎如出一辙,流程基本一致,PBE综合了对称加密和消息摘要算法的优势,形成对称加密算法的一个特例。没有密钥的概念 ,使用口令代替密钥 @author SeanDragon
| 24 | * @author SeanDragon |
| 25 | */ |
| 26 | public final class ToolPBE { |
| 27 | private ToolPBE() { |
| 28 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Java 6 支持以下任意一种算法 |
| 33 | * <p> |
| 34 | * <pre> |
| 35 | * PBEWithMD5AndDES |
| 36 | * PBEWithMD5AndTripleDES |
| 37 | * PBEWithSHA1AndDESede |
| 38 | * PBEWithSHA1AndRC2_40 |
| 39 | * </pre> |
| 40 | */ |
| 41 | public static final String ALGORITHM = "PBEWithMD5AndTripleDES"; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * 盐的长度 |
| 46 | */ |
| 47 | private static final int SALT_BYTE_SIZE = 32 / 2; |
| 48 | |
| 49 | /** |
| 50 | * 生成密文的长度 |
| 51 | */ |
| 52 | private static final int HASH_BIT_SIZE = 128 * 4; |
| 53 | |
| 54 | /** |
| 55 | * 迭代次数 |
| 56 | */ |
| 57 | private static final int PBKDF2_ITERATIONS = 1000; |
| 58 | |
| 59 | /** |
| 60 | * 盐初始化<br> |
| 61 | * 盐长度必须为8字节 |
| 62 | * |
| 63 | * @return byte[] 盐 |
| 64 | * |
| 65 | * @throws Exception |
| 66 | */ |
| 67 | public static byte[] initSalt() { |
| 68 | SecureRandom random = new SecureRandom(); |
| 69 | return random.generateSeed(SALT_BYTE_SIZE); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 转换密钥 |
| 74 | * |
| 75 | * @param password |
| 76 | * 密码 |
| 77 | * |
| 78 | * @return Key 密钥 |
| 79 | * |
| 80 | * @throws Exception |
| 81 | */ |
| 82 | private static Key toKey(String password) throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 83 | // 密钥材料转换 |
nothing calls this directly
no outgoing calls
no test coverage detected