使用AES算法对文件加密/解密代码 -- java

这篇博客介绍了如何使用Java实现AES算法进行文件的加密和解密操作。代码示例展示了如何生成密钥、加密文件以及解密文件的过程,其中涉及到CBC和ECB两种模式,并使用了PKCS5Padding填充方式。

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

废话不多说 直接撸代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


class UtilOfAES {
	public static void main(String[] a)throws Exception{
	    //加密
		UtilOfAES.encryptFile("b358335d7e60a77a".getBytes(), "d:/demo.xlsx", "d:/demo.csv", "CBC");
		//解密
		UtilOfAES.decryptFile("b358335d7e60a77a".getBytes(), "d:/demo.csv", "d:/demo1.xlsx", "CBC");
		System.exit(0);
	}

    private static final String ALGORITHM = "AES";
    private static final int KEY_SIZE = 128;
    private static final int CACHE_SIZE = 1024;
    
    /**
     * <p>
     * 生成随机密钥
     * </p>
     * 
     * @return
     * @throws Exception
     */
    public static byte[] getSecretKey() throws Exception {
        return getSecretKey(null);
    }
    
    /**
     * <p>
     * 生成密钥(返回 hex_key)
     * </p>
     * 
     * @param seed 密钥种子
     * @return
     * @throws Exception
     */
    public static byte[] getSecretKey(String seed) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        SecureRandom secureRandom;
        if (seed != null && !"".equals(seed)) {
            secureRandom = new SecureRandom(seed.getBytes());
        } else {
            secureRandom = new SecureRandom();
        }
        keyGenerator.init(KEY_SIZE, secureRandom); 
        SecretKey secretKey = keyGenerator.generateKey(); 
        return secretKey.getEncoded();
    }
    
    /**
     * <p>
     * 文件加密
     * </p>
     * 
     * @param key
     * @param sourceFilePath
     * @param destFilePath
     * @throws Exception
     */
    public static void encryptFile(byte[] key, String sourceFilePath, String destFilePath, String aesMode) throws Exception {
        File sourceFile = new File(sourceFilePath);
        File destFile = new File(destFilePath); 
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            destFile.createNewFile();
            InputStream in = new FileInputStream(sourceFile);
            OutputStream out = new FileOutputStream(destFile);
            Key k = toKey(key);
            byte[] raw = k.getEncoded(); 
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
            Cipher cipher = Cipher.getInstance("AES/"+aesMode.toUpperCase()+"/PKCS5Padding"); 
            if(aesMode.equalsIgnoreCase("CBC")){
            	//aesMode=='CBC'
	            IvParameterSpec iv = new IvParameterSpec(raw);
	            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
            }else{
            	//aesMode=='ECB'
            	cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            }
            CipherInputStream cin = new CipherInputStream(in, cipher);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = cin.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }
            out.close();
            cin.close();
            in.close();
        }
    }    
    
    /**
     * <p>
     * 文件解密
     * </p>
     * 
     * @param key
     * @param sourceFilePath
     * @param destFilePath
     * @throws Exception
     */
    public static void decryptFile(byte[] key, String sourceFilePath, String destFilePath, String aesMode) throws Exception {
        File sourceFile = new File(sourceFilePath);
        File destFile = new File(destFilePath); 
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!destFile.getParentFile().exists()) {
                destFile.getParentFile().mkdirs();
            }
            destFile.createNewFile();
            FileInputStream in = new FileInputStream(sourceFile);
            FileOutputStream out = new FileOutputStream(destFile);
            Key k = toKey(key);
            byte[] raw = k.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
            Cipher cipher = Cipher.getInstance("AES/"+aesMode.toUpperCase()+"/PKCS5Padding"); 
            if(aesMode.equalsIgnoreCase("CBC")){
            	//aesMode=='CBC'
//	            IvParameterSpec iv = new IvParameterSpec(raw);
                IvParameterSpec iv = new IvParameterSpec("11YTe56789AwCD0F".getBytes());
	            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
            }else{
            	//aesMode=='ECB'
            	cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            }
            CipherOutputStream cout = new CipherOutputStream(out, cipher);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                cout.write(cache, 0, nRead);
                cout.flush();
            }
            cout.close();
            out.close();
            in.close();
        }
    }
    
    /**
     * <p>
     * 转换密钥
     * </p>
     * 
     * @param key
     * @return
     * @throws Exception
     */
    private static Key toKey(byte[] key) throws Exception {
    	if(key==null)
    		throw new Exception("invalid key");
    	//不足16位则补0
    	byte[] mykey = new byte[16];
    	for(int i=0; i<mykey.length; i++){
    		mykey[i] = 0;
    	}
    	for(int i=0; i<key.length; i++){
    		if(i>=16)
    			break;
    		mykey[i] = key[i];
    	}
        SecretKey secretKey = new SecretKeySpec(mykey, ALGORITHM);
        return secretKey;
    }
    
}


实现效果

在这里插入图片描述
1、demo.xlsx为加密前的源文件
2、demo.csv为加密后的csv文件
3、demo1.xlsx为解密后的文件

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rm -rf /*1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值