书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
本文实例讲述了JAVA对称加密算法PBE定义与用法。分享给大家供大家参考,具体如下:
一 AES、DES和3DES在使用上比较一致
二 PBE算法优点
三 PBE介绍
1、PBE:Password Based Encryption 基于口令加密。通过加Salt来增强安全性。
2、对已有算法的包装
3、JDK和BC的实现
4、通过加Salt来增强安全性
四 常见算法
五代码实现
package com.imooc.security.pbe; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import org.apache.commons.codec.binary.Base64; public class ImoocPBE { private static String src = "cakin24 security pbe"; public static void main(String[] args) { jdkPBE(); } public static void jdkPBE() { try { //初始化盐 SecureRandom random = new SecureRandom(); byte[] salt = random.generateSeed(8); //口令与密钥 String password = "cakin24"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES"); Key key = factory.generateSecret(pbeKeySpec); //加密 PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 100); Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES"); cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk pbe encrypt : " + Base64.encodeBase64String(result)); //解密 cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec); result = cipher.doFinal(result); System.out.println("jdk pbe decrypt : " + new String(result)); } catch (Exception e) { e.printStackTrace(); } } }
六 实现效果
jdk pbe encrypt : UKXIqWmtrNBpJ1ayTgGWmpswSLPLel5X
jdk pbe decrypt : cakin24 security pbe
七 应用场景
转载请注明:谷谷点程序 » JAVA对称加密算法PBE定义与用法实例分析