欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现AES加密解密 -- 对文件的加密解密

程序员文章站 2024-03-19 13:05:04
...

对文件的加密解密

工具类:

package cn.com.dh.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AesHelper {

	// 指定加密key
	private static String RANDOM_KEY="erdws32wq43e4r5t6fdszxdcvgbhjijkiu8iu9oplikjuyteq1qazxcbnmkbcdxs";
    
	// 获取默认加密cipher
	public static Cipher getEncryptCipher() throws Exception{
		return getCipher(Cipher.ENCRYPT_MODE);
	}
	// 获取默认解密cipher
	public static Cipher getDecryptCipher() throws Exception{
		return getCipher(Cipher.DECRYPT_MODE);
	}
	public static Cipher getCipher(int model) throws Exception{
			//1.获取加密生成器
			KeyGenerator ******=KeyGenerator.getInstance("AES");
	        //2.根据ecnodeRules规则初始化**生成器
	        //生成一个128位的随机源,根据传入的字节数组
	        ******.init(128, new SecureRandom(RANDOM_KEY.getBytes()));   
	        //3.产生原始对称**
	        SecretKey original_key=******.generateKey();
	        //4.获得原始对称**的字节数组
	        byte [] raw=original_key.getEncoded();
	        //5.根据字节数组生成AES**
	        SecretKey key=new SecretKeySpec(raw, "AES");
	        //6.根据指定算法AES自成密码器
	        Cipher cipher=Cipher.getInstance("AES");
	        //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
	        cipher.init(model, key);
	        return cipher;
	}
}

加密、解密的实现

package cn.com.dh.util;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Scanner;

@Slf4j
public class EncryFileUtil {

	/**
    *
    * @param key     加密的密码
    * @param zipOutPath  压缩文件输出的路径
    * @param zipFilePath  要压缩的文件
    * @throws Exception
    */
   public static void encryptFile( String zipOutPath, String zipFilePath) throws Exception {
	   File file = new File(zipFilePath);
       OutputStream zipOutputPathStream = new FileOutputStream(new File(zipOutPath + File.separator + (file.getName()).toString()));

       Cipher cipher = AesHelper.getEncryptCipher();
       OutputStream cipherOutputStream = new CipherOutputStream(zipOutputPathStream, cipher);
       ZipOutputStream zipOutputStream = new ZipOutputStream(cipherOutputStream);
       zipOutputStream.putNextEntry(new ZipEntry(zipFilePath));

       InputStream fileInputStream = new FileInputStream(new File(zipFilePath));

       BufferedInputStream bis = new BufferedInputStream(fileInputStream);
       BufferedOutputStream bos = new BufferedOutputStream(zipOutputStream);

       byte[] bytes = new byte[1024];
       int len = 0;
       while ((len = bis.read(bytes)) != -1) {
           bos.write(bytes, 0, len
           );
       }
       bos.flush();
       bos.close();
       bis.close();
       fileInputStream.close();
       zipOutputStream.close();
       cipherOutputStream.close();
       zipOutputPathStream.close();

   }
	
	
   /**
   *
   * @param key  解密的密码
   * @param unZipPath   要解密的压缩文件路径
   * @param outFilePath 解密解压缩之后的文件路径
   * @throws Exception
   */
  public static void decryptFile( String unZipPath, String outFilePath) throws Exception{
      InputStream zipInputStream = new FileInputStream(new File(unZipPath));
      Cipher cipher = AesHelper.getDecryptCipher();
      CipherInputStream cipherInputStream = new CipherInputStream(zipInputStream, cipher);
      ZipInputStream decryptZipInputStream = new ZipInputStream(cipherInputStream);
      if (decryptZipInputStream.getNextEntry() == null) {
          return;
      }
      FileOutputStream fileOutputStream = new FileOutputStream(new File(outFilePath));
      BufferedInputStream bis = new BufferedInputStream(decryptZipInputStream);
      BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
      byte[] bytes = new byte[1024];
      int len = 0;
      while ((len = bis.read(bytes)) != -1) {
          bos.write(bytes, 0, len);
      }
      bos.flush();
      bos.close();
      bis.close();
      fileOutputStream.close();
      decryptZipInputStream.close();
      cipherInputStream.close();
      zipInputStream.close();
  }
}