加解密就是将图片转换:对字节进行异或操作,实现加解密
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;
@SpringBootTest
class DemoApplicationTests {
/**
* 加密
* @param decryptedFile 需要加密文件
* @param newFile 加密后文件
* @throws IOException
*/
public static void decryptFile(File decryptedFile, File newFile) throws IOException {
FileInputStream fileInputStream=new FileInputStream(decryptedFile);
FileOutputStream fileOutputStream=new FileOutputStream(newFile);
BufferedInputStream inputStream=new BufferedInputStream(fileInputStream);
BufferedOutputStream outputStream=new BufferedOutputStream(fileOutputStream);
int b;
while ((b=inputStream.read())!=-1){
//与5进行异或
outputStream.write(b^5);
}
outputStream.flush();//刷新流
//关闭流
inputStream.close();
outputStream.close();
}
/**
* 解密
* @param oldFile 需要解密文件
* @param newFile 解密后文件
* @throws IOException
*/
public static void encryptedFile(File oldFile,File newFile) throws IOException {
FileInputStream fileInputStream=new FileInputStream(oldFile);
FileOutputStream fileOutputStream=new FileOutputStream(newFile);
BufferedInputStream inputStream=new BufferedInputStream(fileInputStream);
BufferedOutputStream outputStream=new BufferedOutputStream(fileOutputStream);
int b;
while ((b=inputStream.read())!=-1){
//与5进行异或
outputStream.write(b^5);
}
outputStream.flush();//刷新流
//关闭流
inputStream.close();
outputStream.close();
}
@Test
void test1() throws IOException {
// decryptFile(new File("D:\\sxd-admin\\uploadPath\\avatar\\1.png"),new File("D:\\sxd-admin\\uploadPath\\avatar\\2.png"));
encryptedFile(new File("D:\\sxd-admin\\uploadPath\\avatar\\2.png"),new File("D:\\sxd-admin\\uploadPath\\avatar\\3.png"));
}
}