PBE算法是基于口令的算法加密算法。
实现:JDK和BC
// JDK PBE算法
private static void jdkPBE() {
try {
// 初始化盐
SecureRandom random = new SecureRandom();
byte[] seed = random.generateSeed(8);
// 口令和密钥
String password = "ps";
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBEWITHMD5andDES");
Key key = factory.generateSecret(pbeKeySpec);
// 加密
PBEParameterSpec parameter = new PBEParameterSpec(seed, 100);
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
cipher.init(Cipher.ENCRYPT_MODE, key, parameter);
byte[] result = cipher.doFinal(src.getBytes());
System.out.println("jdk pbe encrypt:"
+ Base64.encodeBase64String(result));
// 解密
cipher.init(Cipher.DECRYPT_MODE, key, parameter);
result = cipher.doFinal(result);
System.out.println("jdk pbe decrypt:" + new String(result));
} catch (Exception e) {
}
}