图像加解密工具类封装

该代码示例展示了如何在Java中通过异或操作实现文件的加密和解密。方法是读取文件的每个字节,然后与固定数值5进行异或运算,将结果写入新文件,从而达到加解密的目的。测试用例进行了加密和解密的流程。
摘要由CSDN通过智能技术生成

加解密就是将图片转换:对字节进行异或操作,实现加解密

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"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer524!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值