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)+">>");

    }


}

你可能感兴趣的:(3Des加解密,压缩文件)