使用 Cipher CipherInputStream CipherOutputStream 实现对文件的加解密

本文介绍了一款基于Java实现的文件加解密工具,支持DESede、Blowfish及AES加密算法。通过Cipher、CipherInputStream及CipherOutputStream实现文件的安全传输。

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

最近使用ftp对文件进行传输,而又担心文件安全问题,所以就此写了一个对文件加解密的工具,文件加密类型支持这三种DESede,Blowfish,AES

下面就不多说了,直接上code

package com.jin.demo.des;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

/**
 * 使用 Cipher CipherInputStream CipherOutputStream 实现对文件的加解密
 * @auther jinsx
 * @date 2019-04-05 15:22
 */
public class CipherFile {

    // 加密类型,支持这三种DESede,Blowfish,AES
    private static final String ENCRYPT_TYPE = "AES";
    // 加密秘钥,长度为24字节
    private static final String ENCRYPT_KEY = "mQbJILokBccRHUkS+XBk7A==";

    /**
     * 加密文件
     * @param srcFileName  要加密的文件
     * @param destFileName 加密后存放的文件名
     */
    public boolean encryptFile(String srcFileName, String destFileName) {
        InputStream is = null;
        OutputStream out = null;
        CipherInputStream cis = null;
        try {
            is = new FileInputStream(srcFileName);
            out = new FileOutputStream(destFileName);
            SecretKey deskey = new SecretKeySpec(ENCRYPT_KEY.getBytes(), ENCRYPT_TYPE);
            Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            // 创建加密流
            cis = new CipherInputStream(is, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = cis.read(buffer)) > 0) {
                out.write(buffer, 0, r);
            }
            System.out.println("文件" + srcFileName + "加密完成,加密后的文件是:" + destFileName);
            return true;
        } catch (Exception e) {
            System.out.println("加密文件" + srcFileName + "出现异常");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (cis != null) {cis.close();}
            } catch (IOException e) {}
            try {
                if (is != null) {is.close();}
            } catch (IOException e) {}
            try {
                if (out != null) {out.close();}
            } catch (IOException e) {}
        }
    }

    /**
     * 解密文件
     * @param srcFileName  要解密的文件
     * @param destFileName 解密后存放的文件名
     */
    public boolean decryptFile(String srcFileName, String destFileName) {
        InputStream is = null;
        OutputStream out = null;
        CipherOutputStream cos = null;
        try {
            is = new FileInputStream(srcFileName);
            out = new FileOutputStream(destFileName);
            SecretKey deskey = new SecretKeySpec(ENCRYPT_KEY.getBytes(), ENCRYPT_TYPE);
            Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            // 创建解密流
            cos = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = is.read(buffer)) > 0) {
                cos.write(buffer, 0, r);
            }
            System.out.println("文件" + srcFileName + "解密完成,解密后的文件是:" + destFileName);
            return true;
        } catch (Exception e) {
            System.out.println("解密文件" + srcFileName + "出现异常");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (cos != null) {cos.close();}
            } catch (IOException e) {}
            try {
                if (is != null) {is.close();}
            } catch (IOException e) {}
            try {
                if (out != null) {out.close();}
            } catch (IOException e) {}
        }
    }

    /***
     * 测试加密解密
     * @param args
     */
    public static void main(String[] args) {
        CipherFile deEncrypt = new CipherFile();
        // 加密
        deEncrypt.encryptFile("F:\\data\\file-key.zip", "F:\\data\\encryptFile\\file-key.zip");
        // 解密
        deEncrypt.decryptFile("F:\\data\\encryptFile\\file-key.zip", "F:\\data\\decryptFile\\file-key.zip");
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值