思路:
在writer时使用异或将字节码乱序,解密时候再用异或就能返回本身的值。
原图:
在这里插入图片描述

加密:
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("1.jpg");
FileOutputStream fos = new FileOutputStream("secret.jpg");
byte[] buffer = new byte[20];
int len;
while ((len=fis.read(buffer))!=-1){
for (int i = 0; i < len; i++) {
buffer[i] =(byte)( buffer[i]^5);
}
fos.write(buffer,0,len);
}
fos.close();
fis.close();
}
结果:

解密:
@Test
public void test2() throws IOException{
FileInputStream fis = new FileInputStream("secret.jpg");
FileOutputStream fos = new FileOutputStream("notsecret.jpg");
byte[] buffer = new byte[20];
int len;
while ((len=fis.read(buffer))!=-1){
for (int i = 0; i < len; i++) {
buffer[i] =(byte)( buffer[i]^5);
}
fos.write(buffer,0,len);
}
fos.close();
fis.close();
}
结果:

文件加密与解密:简单的异或操作实践
这篇博客介绍了使用异或操作实现简单文件加密和解密的过程。通过在writer时对字节码进行异或操作,将原始图片(1.jpg)加密为(secret.jpg),然后在解密时再次异或以恢复原始内容(notsecret.jpg)。这是一种基础的对称加密方法,适用于小规模数据的安全存储。
5739





