Java对文件加密解密

package ac.drsi.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class encryptionUtils {
	private static final int numOfEncAndDec = 0x99; // 加密解密秘钥
	private static int dataOfFile = 0; // 文件字节内容


/**
 * 
 * @param srcFile 初始文件
 * @param encFile 加密文件
 * @throws Exception
 */
	private static void EncFile(File srcFile, File encFile) throws Exception {
		if (!srcFile.exists()) {
			System.out.println("source file not exixt");
			return;
		}

		if (!encFile.exists()) {
			System.out.println("encrypt file created");
			encFile.createNewFile();
		}
		InputStream fis = new FileInputStream(srcFile);
		OutputStream fos = new FileOutputStream(encFile);

		while ((dataOfFile = fis.read()) > -1) {
			fos.write(dataOfFile ^ numOfEncAndDec);
		}

		fis.close();
		fos.flush();
		fos.close();
	}
	
	
/**
 * 
 * @param encFile 加密文件
 * @param decFile 解密文件
 * @throws Exception
 */
	private static void DecFile(File encFile, File decFile) throws Exception {
		if (!encFile.exists()) {
			System.out.println("encrypt file not exixt");
			return;
		}

		if (!decFile.exists()) {
			System.out.println("decrypt file created");
			decFile.createNewFile();
		}

		InputStream fis = new FileInputStream(encFile);
		OutputStream fos = new FileOutputStream(decFile);

		while ((dataOfFile = fis.read()) > -1) {
			fos.write(dataOfFile ^ numOfEncAndDec);
		}

		fis.close();
		fos.flush();
		fos.close();
	}
	public static void main(String[] args) {

		File srcFile = new File("F:\\test\\info.doc"); // 初始文件
		File encFile = new File("F:\\test\\encryption.test"); // 加密文件
		File decFile = new File("F:\\test\\decrypt.doc"); // 解密文件

		try {
			// EncFile(srcFile, encFile); //加密操作
			DecFile(encFile, decFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

链接: https://pan.baidu.com/s/1s_PclsocmXIhiAreSD0Tig 提取码: tttc

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值