JavaIO流实现图片(或者文件)简单 加密及 解密操作

IO流实现对图片的加密及解密

1、首先我是要用IO流的读入和写出的操作来进行处理图片的,如果对IO流的像相关知识不够熟悉的朋友可以去看看这篇文章,里面详细地介绍了IO流的操作。
地址:https://blog.csdn.net/qq_43012792/article/details/106839721

2、实现图片加密代码:

说明:我的原图片是放在了当前Module(day0618)下的,新建加密图片也是放在了此目录下
新的加密图片的大小还是和原图片的大小一样,原因是利用 ^ 运算了,没有对字节增加或减少

package io;

import java.io.*;

public class PhotoIo {
   
    public static void main(String[] args) {
   
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
   
            // 1、提供图片文件和写出文件
            File srcfile = new File("day0618\\IO.jpg");
            File dastfile = new File("day0618\\新的图片1.jpg");

            // 2、创建流
            // 2.1、创建节点流
            FileInputStream fis = new FileInputStream(srcfile);
            FileOutputStream fos = new FileOutputStream(dastfile);

            // 2.2、创建缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            // 3、数据读入和写出操作
            byte[] b
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中对文本文件进行加密解密操作可以使用加密算法和解密算法来实现。常用的加密算法有DES、AES、RSA等,解密算法则是对应的解密算法。下面是一个简单的示例,使用DES算法对文本文件进行加密解密加密操作: ```java import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; public class FileEncryptor { public static void encrypt(String key, File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; SecureRandom secureRandom = new SecureRandom(key.getBytes()); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, secureRandom); int length; while ((length = inputStream.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, length); if (output != null) { outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { outputStream.write(output); } inputStream.close(); outputStream.flush(); outputStream.close(); } } ``` 解密操作: ```java import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; public class FileDecryptor { public static void decrypt(String key, File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; SecureRandom secureRandom = new SecureRandom(key.getBytes()); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey, secureRandom); int length; while ((length = inputStream.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, length); if (output != null) { outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { outputStream.write(output); } inputStream.close(); outputStream.flush(); outputStream.close(); } } ``` 示例中使用的是DES算法,可以根据需要替换为其他算法。对于密钥,建议使用安全的方式存储,例如通过密码学哈希函数对其进行散列,以保证安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值