3Des加解密,压缩文件

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
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;


public class ThreeDes {


    /**
     *
     * @param key     加密的密码
     * @param zipOutPath  压缩文件输出的路径
     * @param zipFilePath  要压缩的文件
     * @throws Exception
     */
    public static void encryptZip(String key, String zipOutPath, String zipFilePath) throws Exception {


        OutputStream zipOutputPathStream = new FileOutputStream(new File(zipOutPath));
        SecretKey secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
        Cipher instance = Cipher.getInstance("DESede");
        instance.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        OutputStream cipherOutputStream = new CipherOutputStream(zipOutputPathStream, instance);
        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 decryptZip(String key, String unZipPath, String outFilePath) throws Exception{
        InputStream zipInputStream = new FileInputStream(new File(unZipPath));
        SecretKey secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        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();
    }


    public static void main(String[] args) throws Exception {
//        String key = "111111111111111111111111";
//        String zipPath = "C:\\Users\\Sky\\Desktop\\liuwc.txt.zip";
//        String zipFile = "C:\\Users\\Sky\\Desktop\\内网传输.txt";
//
//        encryptZip(key, zipPath, zipFile);
//
//        String unZipOutPath = "C:\\Users\\Sky\\Desktop\\result.txt";
//
//        decryptZip(key, zipPath, unZipOutPath);


        Double d = 1.20d;
        BigDecimal bigDecimal = new BigDecimal("1.00");
        BigDecimal decimal = new BigDecimal(String.valueOf(d));
        System.out.println(bigDecimal  +">>>>>>"+decimal);
        System.out.println(bigDecimal.equals(decimal)+">>");

    }


}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值