AES文件的加解密

本文介绍了一个基于AES算法的文件加密和解密工具类,详细展示了如何使用Java进行文件的安全存储。通过CBC模式和PKCS5Padding填充方式,实现了文件数据的加密和解密功能,同时提供了加密后文件的保存方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


package com.zhao;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.io.FileUtils;

/**
 * @author Yongqian.Zhao
 * 
 *File encryption and decryption
 */
public class AESFileUtil {
    
	/**
	 * AES type
	 */
	private static final String AES_TYPE = "AES/CBC/PKCS5Padding";
	
	/**
	 * key spec
	 */
	private static final String AES = "AES";
	
	/**
	 * key
	 */
	private static final String KEY = "ACADEMYDKACADEMY";

	/**
	 * Encryption method
	 * 
	 * @param data
	 * @return
	 */
	public static String enCrypt(byte[] data) {
		try {
			Cipher cipher = Cipher.getInstance(AES_TYPE);
			byte[] bytes = new byte[data.length];
			System.arraycopy(data, 0, bytes, 0, data.length);
			SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), AES);
			AlgorithmParameters params = cipher.getParameters();
			byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
			IvParameterSpec ivspec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
			byte[] enBytes = cipher.doFinal(bytes);
			Encoder encoder = Base64.getEncoder();
			return encoder.encodeToString(enBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Decryption method
	 * 
	 * @param data
	 * @return String
	 */
	public static String desCrypt(String data) {
		try {
			Decoder decoder = Base64.getDecoder();
			byte[] bytes = decoder.decode(data);
			Cipher cipher = Cipher.getInstance(AES_TYPE);
			SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), AES);
			AlgorithmParameters params = cipher.getParameters();
			byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
			IvParameterSpec ivspec = new IvParameterSpec(iv);
			cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
			return new String(cipher.doFinal(bytes));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Save files with encryption and decryption
	 * 
	 * @param file
	 * @param type
	 * @param flag
	 * @return File
	 */
	public static File enDescrySaveAsFile(File file, String type ,String flag) {
		File localFile = null;
		try {
			String fileName = new StringBuffer(UUID.randomUUID().toString()).append(type).toString();
			localFile = new File(fileName);
			byte[] fileByte = FileUtils.readFileToByteArray(file);
			String fileText = "";
			if ("encryption".equalsIgnoreCase(flag)) {
				fileText = enCrypt(fileByte);
			}
			else if("decryption".equalsIgnoreCase(flag)) {
				fileText = desCrypt(new String(fileByte));
			}
			FileUtils.writeStringToFile(localFile, fileText, StandardCharsets.UTF_8);
			return localFile;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (file.exists()) {
				file.delete();
			}
		}
		return localFile;
	}

	public static void main(String[] args) throws Exception {
//		File file = new File("./sample.pdf");
//		File saveAsFile = enDescrySaveAsFile(file, ".pdf", "encryption");
		File file = new File("./688f15f8-6b04-4d50-8a8b-7a59b8b818f3.pdf");
		File saveAsFile = enDescrySaveAsFile(file, ".pdf", "decryption");
		System.out.println(saveAsFile);
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值