PBE文件加密-Java应用实例
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Random; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; /** * PBE 全称为Password-Based Encrypt(基于密码的加密)。 */ public class PBE_Encrypt { private static String ALGORITHM1 = "PBE"; private static String ALGORITHM2 = "PBEWithMD5AndDES"; private static int ITERATIONCOUNT = 1000; // 迭代计数。 /** * 根据自定义密码生成密钥 */ public static SecretKey generateKey(String password) throws Exception { PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory kf = SecretKeyFactory.getInstance(ALGORITHM1); return kf.generateSecret(keySpec); } /** * 加密文件 * @param key密钥 * @param sourceFile源文件 * @param secretFile目标加密文件 */ public static void encryptFile(SecretKey key, File sourceFile, File secretFile) throws Exception { /** 产生一个随机盐 */ byte[] salt = new byte[8]; Random random = new Random(); random.nextBytes(salt); /** 为 PKCS #5 标准中所定义的基于密码的加密法构造一个参数集合 salt:复制该 salt 的内容来防止后续修改;1000:迭代计数 */ PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATIONCOUNT); /** 得到Cipher对象来实现对源数据的PBEWithMD5AndDES加密 */ Cipher cipher = Cipher.getInstance(ALGORITHM2); cipher.init(Cipher.ENCRYPT_MODE, key, spec); /** 构造文件缓冲输出流,并将随机盐写入密文头 */ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(secretFile)); out.write(salt); /** 构造源文件读取流 */ BufferedInputStream in = new BufferedInputStream(new FileInputStream(sourceFile)); /** 构造密文输出流,将源文件内容加密写入目标加密文件 */ CipherOutputStream cos = new CipherOutputStream(out, cipher); int b = -1; while ((b = in.read()) != -1) { cos.write(b); } cos.close(); in.close(); } /** * 解密文件 * @param key 密钥 * @param secretFile 加密文件 * @param decryptFile 解密后文件 */ public static void decryptFile(SecretKey key, File secretFile, File decryptFile) throws Exception { /** 从加密文件中读取随机盐 */ byte[] salt = new byte[8]; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(secretFile)); bis.read(salt); PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATIONCOUNT); /** 得到Cipher对象来实现对加密数据的PBEWithMD5AndDES解密 */ Cipher cipher = Cipher.getInstance(ALGORITHM2); cipher.init(Cipher.DECRYPT_MODE, key, spec); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(decryptFile)); CipherInputStream in = new CipherInputStream(bis, cipher); int b = -1; while ((b = in.read()) != -1) { out.write(b); } out.close(); in.close(); } public static void main(String[] args) throws Exception { SecretKey key = generateKey("MySecretKey");// 用自定义的字符串生成一个可信密钥 File sourceFile = new File("D:\\test.jpg");// 源文件 File secretFile = new File("D:\\secret");// 加密目标文件 File decryptFile = new File("D:\\decrypt.jpg");// 解密目标文件 /** 加密文件 */ encryptFile(key, sourceFile, secretFile); /** 解密文件 */ decryptFile(key, secretFile, decryptFile); } }http://xzh.i3geek.com