簡單加密文件

import java.io.*;

public class FileEncryptUtil {

	/**
	 * 密鑰
	 */
    private static final int encryptKey =0x12;

    /**
     * 加密/解密 文件
     * @param srcFile 原文件
     * @param encFile 加密/解密後的文件
     * @throws Exception
     */
    public static void encryptAndDecodeFile(File srcFile, File encFile) throws Exception {
        InputStream inputStream = new FileInputStream(srcFile);
        OutputStream outputStream = new FileOutputStream(encFile);
        encryptAndDecodeStream(inputStream, outputStream);
    }

    /**
     * 加密/解密 文件
     * @param inputStream 源流
     * @param outputStream 目標流
     * @throws Exception
     */
    public static void encryptAndDecodeStream(InputStream inputStream, OutputStream outputStream) throws Exception {
        int read = 0;
        while ((read = inputStream.read()) > -1) {
            outputStream.write(read ^ encryptKey);
        }
        inputStream.close();
        outputStream.flush();
        outputStream.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章