標籤:method exist 目標 .com 開啟 delete 檔案路徑 建議 bst
加密目標檔案
代碼如下:
不建議進行二次加密,若二次加密必須要二次解密
package com.xgt.util;import java.io.*;public class VideoEncodeUtil { public static void main(String[] args) throws Exception { encrypt("D:\\py交易\\11.mp4","fuckyourself"); decrypt("D:\\py交易\\11.mp4","D:\\py交易\\22.mp4",4); System.out.println(readFileLastByte("D:\\py交易\\11.mp4",12)); } /** * 檔案file進行加密 * @param fileUrl 檔案路徑 * @param key 密碼 * @throws Exception */ public static void encrypt(String fileUrl, String key) throws Exception { File file = new File(fileUrl); String path = file.getPath(); if(!file.exists()){ return; } int index = path.lastIndexOf("\\"); String destFile = path.substring(0, index)+"\\"+"abc"; File dest = new File(destFile); //擷取待加密檔案的輸入資料流 InputStream in = new FileInputStream(fileUrl); //建立中轉檔案輸出資料流 OutputStream out = new FileOutputStream(destFile); //待加密檔案的流 byte[] buffer = new byte[1024]; int r; //加密之後的檔案的流 byte[] buffer2=new byte[1024]; while (( r= in.read(buffer)) > 0) { for(int i=0;i<r;i++) { byte b=buffer[i]; //buffer2[i]=b==255?0:++b; //每個位元組加2加密 b+=2; buffer2[i] = b; } out.write(buffer2, 0, r); out.flush(); } in.close(); out.close(); file.delete(); dest.renameTo(new File(fileUrl)); appendMethodA(fileUrl, key); System.out.println("加密成功"); } /** * * @param fileName * @param content 密鑰 */ public static void appendMethodA(String fileName, String content) { try { // 開啟一個隨機訪問檔案流,按讀寫方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 檔案長度,位元組數 long fileLength = randomFile.length(); //將寫檔案指標移到檔案尾。 randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 解密 * @param fileUrl 源檔案 * @param tempUrl 臨時檔案 * @param keyLength 密碼長度 * @return * @throws Exception */ public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{ File file = new File(fileUrl); if (!file.exists()) { return null; } File dest = new File(tempUrl); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } //擷取待解密的檔案輸入資料流 InputStream is = new FileInputStream(fileUrl); //建立目標檔案輸出資料流,用來產生解密後的檔案 OutputStream out = new FileOutputStream(tempUrl); byte[] buffer = new byte[1024]; byte[] buffer2=new byte[1024]; byte bMax=(byte)255; long size = file.length() - keyLength; int mod = (int) (size%1024); int div = (int) (size>>10); int count = mod==0?div:(div+1); int k = 1, r; while ((k <= count && ( r = is.read(buffer)) > 0)) { if(mod != 0 && k==count) { r = mod; } for(int i = 0;i < r;i++) { byte b=buffer[i]; //buffer2[i]=b==0?bMax:--b; //每個位元組減2解碼 b-=2; buffer2[i] = b; } out.write(buffer2, 0, r); k++; } out.close(); is.close(); return tempUrl; } /** * 判斷檔案是否加密 * @param fileName * @return * * 加密成功返回key * 加密失敗返回非key的字串 */ public static String readFileLastByte(String fileName, int keyLength) { File file = new File(fileName); if(!file.exists())return "沒有檔案"; StringBuffer str = new StringBuffer(); try { // 開啟一個隨機訪問檔案流,按讀寫方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 檔案長度,位元組數 long fileLength = randomFile.length(); //將寫檔案指標移到檔案尾。 for(int i = keyLength ; i>=1 ; i--){ randomFile.seek(fileLength-i); str.append((char)randomFile.read()); } randomFile.close(); return str.toString(); } catch (IOException e) { e.printStackTrace(); } return "異常"; }}
Java通過流對檔案加密