以壓縮包的形式對資料夾進行加密和解密
package jzj.timingtraining.security; import java.io.File; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.UUID; public class ZipCipherUtil { /** * 對目錄srcFile下的所有檔案目錄進行先壓縮後加密,然後儲存為destfile * * @param srcFile * 要操作的檔案或資料夾 * @param destfile * 壓縮加密後存放的檔案 * @param keyfile * 金鑰 */ public void encryptZip(String srcFile, String destfile, String keyStr) { File temp = new File("/sdcard/" +UUID.randomUUID().toString() + ".zip"); temp.deleteOnExit(); // 先壓縮檔案 new ZipUtil().zip(srcFile, temp.getAbsolutePath()); // 對檔案加密 try { new CipherUtil().encrypt(temp.getAbsolutePath(), destfile, keyStr); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } temp.delete(); deleteDir(new File(srcFile)); } /** * 對檔案srcfile進行先解密後解壓縮,然後解壓縮到目錄destfile下 * * @param srcfile * 要解密和解壓縮的檔名 * @param destfile * 解壓縮後的目錄 * @param publicKey * 金鑰 */ public void decryptUnzip(String srcfile, String destfile, String keyStr) { File temp = new File(UUID.randomUUID().toString() + ".zip"); temp.deleteOnExit(); // 先對檔案解密 try { new CipherUtil().decrypt(srcfile, temp.getAbsolutePath(), keyStr); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 解壓縮 new ZipUtil().unZip(temp.getAbsolutePath(),destfile); temp.delete(); deleteDir(new File(srcfile)); } public static void main(String[] args) throws Exception { long l1 = System.currentTimeMillis(); //加密 new ZipCipherUtil().encryptZip("d:\\photo", "d:\\photo.zip", "12345"); //解密 new ZipCipherUtil().decryptUnzip("d:\\photo.zip", "d:\\photo", "12345"); long l2 = System.currentTimeMillis(); // System.out.println((l2 - l1) + "毫秒."); // System.out.println(((l2 - l1) / 1000) + "秒."); } /** * 遞迴刪除目錄下的所有檔案及子目錄下所有檔案 * @param dir 將要刪除的檔案目錄 * @return boolean Returns "true" if all deletions were successful. * If a deletion fails, the method stops attempting to * delete and returns "false". */ private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); //遞迴刪除目錄中的子目錄下 for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目錄此時為空,可以刪除 return dir.delete(); } }
package jzj.timingtraining.security; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** * 對檔案或資料夾進行壓縮和解壓 * */ public class ZipUtil { /** * 新增到壓縮檔案中 * @param out * @param f * @param base * @throws Exception */ private void directoryZip(ZipOutputStream out, File f, String base) throws Exception { // 如果傳入的是目錄 if (f.isDirectory()) { File[] fl = f.listFiles(); // 建立壓縮的子目錄 out.putNextEntry(new ZipEntry(base + "/")); if (base.length() == 0) { base = ""; } else { base = base + "/"; } for (int i = 0; i < fl.length; i++) { directoryZip(out, fl[i], base + fl[i].getName()); } } else { // 把壓縮檔案加入rar中 out.putNextEntry(new ZipEntry(base)); FileInputStream in = new FileInputStream(f); byte[] bb = new byte[10240]; int aa = 0; while ((aa = in.read(bb)) != -1) { out.write(bb, 0, aa); } in.close(); } } /** * 壓縮檔案 * * @param zos * @param file * @throws Exception */ private void fileZip(ZipOutputStream zos, File file) throws Exception { if (file.isFile()) { zos.putNextEntry(new ZipEntry(file.getName())); FileInputStream fis = new FileInputStream(file); byte[] bb = new byte[10240]; int aa = 0; while ((aa = fis.read(bb)) != -1) { zos.write(bb, 0, aa); } fis.close(); System.out.println(file.getName()); } else { directoryZip(zos, file, ""); } } /** * 解壓縮檔案 * * @param zis * @param file * @throws Exception */ private void fileUnZip(ZipInputStream zis, File file) throws Exception { ZipEntry zip = zis.getNextEntry(); if (zip == null) return; String name = zip.getName(); File f = new File(file.getAbsolutePath() + "/" + name); if (zip.isDirectory()) { f.mkdirs(); fileUnZip(zis, file); } else { f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); byte b[] = new byte[10240]; int aa = 0; while ((aa = zis.read(b)) != -1) { fos.write(b, 0, aa); } fos.close(); fileUnZip(zis, file); } } /** * 根據filePath建立相應的目錄 * @param filePath * @return * @throws IOException */ private File mkdirFiles(String filePath){ File file = new File(filePath); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return file; } /** * 對zipBeforeFile目錄下的檔案壓縮,儲存為指定的檔案zipAfterFile * * @param zipBeforeFile 壓縮之前的檔案 * @param zipAfterFile 壓縮之後的檔案 */ public void zip(String zipBeforeFile, String zipAfterFile) { try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(zipAfterFile))); fileZip(zos, new File(zipBeforeFile)); zos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 解壓縮檔案unZipBeforeFile儲存在unZipAfterFile目錄下 * * @param unZipBeforeFile 解壓之前的檔案 * @param unZipAfterFile 解壓之後的檔案 */ public void unZip(String unZipBeforeFile, String unZipAfterFile) { try { ZipInputStream zis = new ZipInputStream(new FileInputStream(unZipBeforeFile)); File f = new File(unZipAfterFile); f.mkdirs(); fileUnZip(zis, f); zis.close(); } catch (Exception e) { e.printStackTrace(); } } }
package jzj.timingtraining.security; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.GeneralSecurityException; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; /** * 使用AES對檔案進行加密和解密 * */ public class CipherUtil { /** * 使用AES對檔案進行加密和解密 * */ private static String type = "AES"; /** * 把檔案srcFile加密後儲存為destFile * @param srcFile 加密前的檔案 * @param destFile 加密後的檔案 * @param privateKey 金鑰 * @throws GeneralSecurityException * @throws IOException */ public void encrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException { Key key = getKey(privateKey); Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(mkdirFiles(destFile)); crypt(fis, fos, cipher); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } } /** * 把檔案srcFile解密後儲存為destFile * @param srcFile 解密前的檔案 * @param destFile 解密後的檔案 * @param privateKey 金鑰 * @throws GeneralSecurityException * @throws IOException */ public void decrypt(String srcFile, String destFile, String privateKey) throws GeneralSecurityException, IOException { Key key = getKey(privateKey); Cipher cipher = Cipher.getInstance(type + "/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(mkdirFiles(destFile)); crypt(fis, fos, cipher); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } } /** * 根據filePath建立相應的目錄 * @param filePath 要建立的檔案路經 * @return file 檔案 * @throws IOException */ private File mkdirFiles(String filePath) throws IOException { File file = new File(filePath); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); return file; } /** * 生成指定字串的金鑰 * @param secret 要生成金鑰的字串 * @return secretKey 生成後的金鑰 * @throws GeneralSecurityException */ private static Key getKey(String secret) throws GeneralSecurityException { KeyGenerator kgen = KeyGenerator.getInstance(type); kgen.init(128, new SecureRandom(secret.getBytes())); SecretKey secretKey = kgen.generateKey(); return secretKey; } /** * 加密解密流 * @param in 加密解密前的流 * @param out 加密解密後的流 * @param cipher 加密解密 * @throws IOException * @throws GeneralSecurityException */ private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException { int blockSize = cipher.getBlockSize() * 1000; int outputSize = cipher.getOutputSize(blockSize); byte[] inBytes = new byte[blockSize]; byte[] outBytes = new byte[outputSize]; int inLength = 0; boolean more = true; while (more) { inLength = in.read(inBytes); if (inLength == blockSize) { int outLength = cipher.update(inBytes, 0, blockSize, outBytes); out.write(outBytes, 0, outLength); } else { more = false; } } if (inLength > 0) outBytes = cipher.doFinal(inBytes, 0, inLength); else outBytes = cipher.doFinal(); out.write(outBytes); } }
相關推薦
以壓縮包的形式對資料夾進行加密和解密
package jzj.timingtraining.security; import java.io.File; import java.io.IOException; import java.security.GeneralSecurityException; imp
xp環境下對資料夾進行加密和解密
1.在開始選單裡開啟命令提示符(XP在執行裡輸入cmd) 2.用md命令建立資料夾,現以在E盤根目錄建立niu資料夾為例 鍵入md E://niu..// 注意,有2個點,還有斜槓,這是關鍵! 3.在我的電腦裡雙擊E://目錄下的niu.資料夾,結果提示錯誤。呵呵,正常是打不開的~ 4.如果自己要開啟資料夾,
對儲存過程進行加密和解密(SQL 2008/SQL 2012)
Use masterGoif object_ID('[sp_DecryptObject]') is not null Drop Procedure [sp_DecryptObject]Gocreate procedure sp_DecryptObject ( @O
使用map以時間對資料夾進行排序
理論:摘自他人:map是用來存放<key, value>鍵值對的資料結構,可以很方便快速的根據key查到相應的value。假如儲存學生和其成績(假定不存在重名,當然可以對重名加以區分),我們用map來進行儲存就是個不錯的選擇。 我們這樣定義,map<stri
Ubuntu一個直接以視窗的形式開啟資料夾的命令
nautilus 之前一直找不到~/.keras/keras.json這個改變keras後臺的檔案,用這個一下就找到了。 example: nautilus ~/.keras 參考:http://www.cnblogs.com/onlycxue/arch
利用反射機制建立工具類對資料進行加密和解密
對資料庫的資料進行加密,包括使用者資料、專案資料、聊天資料等,需要在插入表的的時候進行加密,查詢的時候進行解密。利用java的反射機制,建立以下工具類,對資料進行加解密。 public class CryptoUtil { /** * 加密、解密方法 * @param ob
java 使用AES對資料進行加密和解密
最近做的聊天功能,李老闆希望對聊天的資料進行加密,然後存入資料庫,首先想到的便是AES加密的方式,以前也用過幾次,這次正好記錄下來: MD5加密是不可逆的,可以對使用者的密碼加密; AES加密是可逆的,可以對資料庫的資料進行加密,因為使用者查詢資料時,要返回明文,適合。 publ
Delphi中使用cxGrid對資料集進行Sort和Locate操作
核心提示:在編寫某個系統時,由於使用了資料集型別無關技術(即資料集可能是ADOQuery,也有可能是TClientDataSet等等)。當需要對資料進行排序和查詢時,只好利用cxGrid自身的功能來實現:fun... 在編寫某個系統時,由於使用了資料集型別無關技術(即資料集可能是ADOQuery,也有可
java按行讀取檔案並對檔案進行加密和解密
package com.alibaba.datax.plugin.reader.selfxmlfilereader.util; import java.io.UnsupportedEncodingException; import java.security
Linux下利用openssl對檔案進行加密和解密
--建立檔案test.txt, 特意寫入中英文 # cd /tmp # echo "test測試" > test.txt--開始加密, 使用aes-128-cbc演算法, 也可以使用其他演算法, 通過檢視openssl的幫助可獲知 # openssl aes-128-
登入記住使用者名稱和密碼時對使用者名稱和密碼進行加密和解密
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/jquer
如何運用OpenSSL 對檔案進行加密和解密
我們在平時的 Linux 運維管理的時候,經常會進行各種資料備份任務。將資料匯出然後打包。通常在安全性要求比較高的環境下,我們可以藉助 OpenSSL 工具對打包後的資料進行加密,這樣能進一步的保障資料的安全性。OpenSSL 使用密碼方式加密或解密檔案1. 使用 opens
什麽是私有密鑰密碼技術——密鑰加密算法采用同一把密鑰進行加密和解密
解密 網絡安全 位操作 線性復雜 對稱 大量 控制 全局 相位 什麽是私有密鑰密碼技術 私有密鑰(Symmetric Key),又叫對稱密鑰。密鑰加密算法采用同一把密鑰進行加密和解密。它的優點是加密和解密速度非常快,但密鑰的分發和管理比較困難。信息的發送者和接收者必須明確同
javascript對頁面簡單的加密和解密
最近看了幾個頁面,發現好多都是經過處理的。不過,裡面附帶著一段javascrpit處理加密的頁面。下面就簡單說說加密和解密的過程。一、加密1、將字串中的每個字元轉換為數字形式方法是 字串處理的方法 charCodeAt(index)charCodeAt() 方法可返回指定位置
對資料夾中的檔案進行分別壓縮並加密
呼叫系統的winRAR工具對檔案進行壓縮和加密 1 import zipfile as zf 2 import platform as pf 3 import subprocess 4 import os 5 import os.path 6 7 rootdir = r'' #設定檔
如何使用python對資料夾中的檔案進行批量改名(增、刪、改字串欄位)
【時間】2018.10.12 【題目】如何使用python對資料夾中的檔案進行批量改名(增、刪、改字串欄位) 【問題描述】今天需要對資料夾中的檔案進行批量改名,主要是因為名字中多出了自己不想要的字元段“data”想要將其刪除。這裡便以刪除名字中的字元段為例,至於增、改道理類
檔案處理 將txt檔案當中資料取出進行以郵箱分類處理 將郵箱拿出來寫入對應的資料夾 分資料夾進行儲存 新手求吐槽優化
import os pathemail = r'E:\python\7.11\郵箱.txt' with open(pathemail,'r',encoding='utf-8') as f: flist = f.readlines() for i in range(len(fli
java 移動節點,排序號進行重新排序,以閉包形式實現ztree增刪改查操作,其中排序運用到此方法
package com.avic.custom.controller.ct; import java.util.HashMap; import java.util.Map; /** * 移動節點重新排序操作 * @author Administrator * */
matlab 遍歷兩層資料夾,對兩層資料夾進行改變圖片大小處理【轉載】
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-
【Java】對檔案或資料夾進行重新命名
在Java中,對檔案或資料夾進行重新命名是很簡單的,因為Java的File類已經封裝好rename