Java通過流對檔案加密

來源:互聯網
上載者:User

標籤: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通過流對檔案加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.