IO-(Paly_Secret_Decode)@Test

使用IO-做一个加密解密的Test

还可以进行Buffer的"缓冲流"处理,提高复制速度,我这边没写,有需要自己加

/*
对图片进行加密操作
 */
public class Secret_Test {
    @Test
    //加密操作
    public void secretTest() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1- 实例化文件
            File srcFile = new File("方园.jpeg");
            File destFile = new File("方园Secret.jpeg");
            //2- 创建一个读入和写出的流
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);

            //3- 复制操作+加密操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                //加密--就是在读入的时候,加一个改变的循环
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4- 关闭流
            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (fileOutputStream != null)
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    @Test
    //对加密和解密--进行封装
    //1- 这里src表示原文件,dest表示 复制原文件,产生的加密文件
    //2- 如果要解密,就把"被加密的文件放在src位置,dest出来的就是解密的文件"
    public void SecretAndDecode(String src, String dest) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //1- 实例化文件
            File srcFile = new File(src);
            File destFile = new File(dest);
            //2- 创建一个读入和写出的流
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);

            //3- 复制操作+加密操作
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                //加密--就是在读入的时候,加一个改变的循环
                for (int i = 0; i < len; i++) {
                    buffer[i] = (byte) (buffer[i] ^ 5);
                }
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4- 关闭流
            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (fileOutputStream != null)
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    @Test
    //对上面,封装的加密和解密,进行测试
    public void test3() {
        SecretAndDecode("方园Secret.jpeg", "方园decode.jpeg");
    }
}

posted on   陈嘻嘻-  阅读(121)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示