集册 Java实例教程 解密解压缩

解密解压缩

欢马劈雪     最近更新时间:2020-01-02 10:19:05

476
解密解压缩


//package com.nowjava;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;//来自 NowJava.com - 时代Java

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 java.util.Enumeration;


import java.util.UUID;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;


import javax.crypto.Cipher;
/** 来自 n o w j a   v  a . c o m - 时  代  Java**/

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;


public class Main {

    public static void decryptUnzip(String srcFile, String target,

            String keyFile) throws Exception {

        File temp = new File(UUID.randomUUID().toString() + ".zip");

        temp.deleteOnExit();

        decrypt(srcFile, temp.getAbsolutePath(), getKey(keyFile));

        unzip(temp.getAbsolutePath(), target);

        temp.delete();

    }


    public static void decrypt(String srcFile, String destFile,

            Key privateKey) throws Exception {

        SecureRandom sr = new SecureRandom();

        Cipher ciphers = Cipher.getInstance("AES/CBC/PKCS5Padding");

        IvParameterSpec spec = new IvParameterSpec(privateKey.getEncoded());

        ciphers.init(Cipher.DECRYPT_MODE, privateKey, spec, sr);

        FileInputStream fis = new FileInputStream(srcFile);

        FileOutputStream fos = new FileOutputStream(destFile);

        byte[] b = new byte[2064];

        int byteread = 0;

        while ((byteread = fis.read(b)) != -1) {

            fos.write(ciphers.doFinal(b, 0, byteread));

        }

        fos.close();

        fis.close();

    }


    public static Key getKey(String keyPath) throws Exception {

        FileInputStream fis = new FileInputStream(keyPath);

        byte[] b = new byte[16];

        fis.read(b);

        SecretKeySpec dks = new SecretKeySpec(b, "AES");

        fis.close();

        return dks;

    }


    public static void unzip(String zipFileName, String target)

            throws Exception {

        ZipFile zipfile = new ZipFile(zipFileName);

        Enumeration subFiles = zipfile.entries();

        ZipEntry zipEntry = null;

        byte[] buf = new byte[1024];

        while (subFiles.hasMoreElements()) {

            zipEntry = (ZipEntry) subFiles.nextElement();

            if (zipEntry.isDirectory()) {

                continue;

            }

            OutputStream os = new BufferedOutputStream(

                    new FileOutputStream(getRealFileName(target,

                            zipEntry.getName())));

            InputStream is = new BufferedInputStream(

                    zipfile.getInputStream(zipEntry));

            int readLen = 0;

            while ((readLen = is.read(buf, 0, 1024)) != -1) {

                os.write(buf, 0, readLen);

            }

            is.close();

       
展开阅读全文