1. 程式人生 > >3Des加解密,壓縮檔案

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