java对文件进行加密解密操作

java对文件进行加密解密操作

源码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import org.junit.Test;




public class Demo2 {
//测试加密
@Test
public void test1() throws IOException{
String sourceFilePath = "e:\\001.png";
String targetFilePath = "e:\\001_0.png";
security(sourceFilePath,targetFilePath);
}

//测试解密
@Test
public void test2() throws IOException{
String sourceFilePath = "e:\\001_0.png";
String targetFilePath = "e:\\001_1.png";
security(sourceFilePath,targetFilePath);
}


/*
* 加密 和 解密操作
* sourceFilePath : 输入文件路径
* targetFilePath : 输出文件路径
*/
public void security(String sourceFilePath,String targetFilePath) throws IOException{
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);

FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try{
inputStream = new FileInputStream(sourceFile);
byte[] buffer = new byte[1024];
int len = 0;
outputStream = new FileOutputStream(targetFile);
while((len = inputStream.read(buffer))>0){
byte[] outputBuffer = new byte[len];
//分别对每个字节进行异或操作
for(int i=0;i<len;i++){
byte b = buffer[i];
b = (byte) (b ^ 25);
outputBuffer[i] = b;
}
outputStream.write(outputBuffer,0,len);
}
}finally{
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值