使用AES对文件加解密 java实现

对文件进行AES加解密的过程,使用Java语言调用Cipher库实现。
本文实现的加解密方式为AES CBC方式,使用不填充(NoPadding)的方式进行。
文中主要实现了对于给出的密文的解密过程以及对文件(我自己的学号姓名)的加密,需要者可根据自己需要在Main函数中修改即可。
如果想要测试程序正误,附一份密文以及加密密钥和初始向量:

点击这里下载

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import javax.crypto.Cipher;  
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;  
  
public class Test {  
      
    private static final int ZERO = 0;  
    private static final int ONE = 1; 
    //实验操作路径
    private static String derectory = "E:\\StudyDocu\\code\\sth"; 
     
    
      
    public static void main(String[] args) {  
        //key: 加密密钥
        String key = "aaaabbbbccccdddd";
        //ivParameter:AES cbc加密模式的iv向量
        String ivParameter = "AAAABBBBCCCCDDDD";
        try {     
            //对给出的密文的解密过程。
            //密文保存在“密文.txt”中
            //将解密结果保存在“明文.txt”中
            File file = new File(derectory+"/"+"密文.txt");  
            String fileName = "明文.txt";
            decriptfile(file,key,ivParameter,fileName);  
            
            //对自己的学号姓名的加密过程。
            //学号姓名保存在“学号姓名.txt中”
            //将加密结果保存在“encrypt_Id&Name.txt”中
            String fileName1 = "encrypt_Id&Name.txt";
            File file2 = new File(derectory+"/"+"学号姓名.txt");
            encryptfile(file2, key, ivParameter,fileName1);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }   
  
    /** 
     * 文件处理方法 
     * code为加密或者解密的判断条件 
     * file 密文文件
     * key 加密密钥 
     * ivParameterm iv向量
     * filename 加解密结果存入的文件名
     */  
    public static void doFile(int code, File file, String key, String ivParameterm, String filename) throws Exception{  
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(  
                file));  
        byte[] bytIn = new byte[(int) file.length()];  
        bis.read(bytIn);  
        bis.close();  
        // AES加密  
        byte[] raw = key.getBytes("ASCII");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");  
        //Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
        IvParameterSpec iv = new IvParameterSpec(ivParameterm.getBytes());
        if(0 == code){  
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec,iv);  
        }else if(1 == code){  
            cipher.init(Cipher.DECRYPT_MODE, skeySpec,iv);  
        }         
        // 写文件  
        byte[] bytOut = cipher.doFinal(bytIn);  
        File outfile = new File(derectory+"/"+filename);  
        BufferedOutputStream bos = new BufferedOutputStream(  
                new FileOutputStream(outfile));  
        bos.write(bytOut);  
        bos.close();  
    }  
      
    //文件加密  
    public static void encryptfile(File file, String key, String ivParameter, String filename) throws Exception {  
        doFile(ZERO,file,key,ivParameter,filename);  
    }  
      
    //文件解密  
    public static void decriptfile(File file, String key, String ivParameter, String filename) throws Exception{  
        doFile(ONE,file,key,ivParameter,filename);  
    }  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文主要介绍移动端的加解密算法的分类、其优缺点特性及应用,帮助读者由浅入深地了解和选择加解密算法。文中会包含算法的...
    苹果粉阅读 11,592评论 5 29
  • 概述 之前一直对加密相关的算法知之甚少,只知道类似DES、RSA等加密算法能对数据传输进行加密,且各种加密算法各有...
    Henryzhu阅读 3,060评论 0 14
  • 序 本文主要小结一下java里头的AES以及RSA加解密。 AES 使用AES加密时需要几个参数: 密钥长度(Ke...
    go4it阅读 844评论 0 0
  • 0x01 目录 常见编码: ASCII编码 Base64/32/16编码 shellcode编码 Quoted-p...
    H0f_9阅读 13,067评论 2 17
  • 先来个目录(今天先说一部分,其余的后续整理,陆续发布,敬请关注): 一、出纳岗工作流程 二、销售费用岗工作流程 三...
    闻道解惑阅读 725评论 0 1