图像文件的加密与解密

2024-03-10

结合我的另一个question https://stackoverflow.com/questions/12131627/image-encryption-decryption,并且在更改了这一小部分代码之后

FileOutputStream output = new FileOutputStream("sheepTest.png");
    CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
    ImageIO.write(input, "PNG", cos);
    cos.close();

从解密部分,我遇到了另一个错误,这是

Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
at javax.imageio.ImageIO.getWriter(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at encypt.com.trial.main(trial.java:82)

当我单击sheepTest.png时,该文件是空的。错误在哪里?谁能帮我解决这个错误?谢谢。

public class trial {
public static void main(String[] arg) throws Exception {

   // Scanner to read the user's password. The Java cryptography
   // architecture points out that strong passwords in strings is a
   // bad idea, but we'll let it go for this assignment.
   Scanner scanner = new Scanner(System.in);
   // Arbitrary salt data, used to make guessing attacks against the
   // password more difficult to pull off.
   byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
           (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };

   {
     File inputFile = new File("sheep.png");
      BufferedImage input = ImageIO.read(inputFile);
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
     // Get a password from the user.
     System.out.print("Password: ");
     System.out.flush();
     PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.nextLine().toCharArray());          
     // Set up other parameters to be used by the password-based
     // encryption.
     PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
     SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
     // Make a PBE Cyhper object and initialize it to encrypt using
     // the given password.
     Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
     pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
     FileOutputStream output = new FileOutputStream("sheepTest.png");
     CipherOutputStream cos = new CipherOutputStream(
            output, pbeCipher);
       //File outputFile = new File("image.png");
         ImageIO.write(input,"PNG",cos);
      cos.close();          

   }
   // Now, create a Cipher object to decrypt for us. We are repeating
   // some of the same code here to illustrate how java applications on
   // two different hosts could set up compatible encryption/decryption
   // mechanisms.
  {
       File inputFile = new File("sheepTest.png");
         BufferedImage input = ImageIO.read(inputFile);
       // Get another (hopefully the same) password from the user.
      System.out.print("Decryption Password: ");
       System.out.flush();
       PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.next().toCharArray());
       // Set up other parameters to be used by the password-based
       // encryption.
       PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
       SecretKeyFactory keyFac = SecretKeyFactory
               .getInstance("PBEWithMD5AndDES");
       SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
       // Make a PBE Cyper object and initialize it to decrypt using
       // the given password.
       Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
       pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
       // Decrypt the ciphertext and then print it out.
       /*byte[] cleartext = pbeCipher.doFinal(ciphertext);
       System.out.println(new String(cleartext));*/
       FileOutputStream output = new FileOutputStream("sheepTest.png");
       CipherOutputStream cos = new CipherOutputStream(
              output, pbeCipher);
        ImageIO.write(input,"PNG",  cos);
        cos.close();

   }
   }
}

继 NateCK 富有洞察力的帖子(顺便说一句,做得很好)之后,我修改了您的解密部分

// Note that we are not reading the image in here...
System.out.print("Decryption Password: ");
System.out.flush();
PBEKeySpec pbeKeySpec = new PBEKeySpec(scanner.next().toCharArray());
// Set up other parameters to be used by the password-based
// encryption.
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
SecretKeyFactory keyFac = SecretKeyFactory
        .getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Make a PBE Cyper object and initialize it to decrypt using
// the given password.
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

// We're now going to read the image in, using the cipher
// input stream, which wraps a file input stream
File inputFile = new File("sheepTest.png");
FileInputStream fis = new FileInputStream(inputFile);
CipherInputStream cis = new CipherInputStream(fis, pbeCipher);
// We then use all that to read the image
BufferedImage input = ImageIO.read(cis);
cis.close();

// We then write the dcrypted image out...
// Decrypt the ciphertext and then print it out.
FileOutputStream output = new FileOutputStream("sheepTest.png");
ImageIO.write(input, "PNG", output);

我的例子基于 NateCKs 的发现。如果你觉得它有用,点个赞就好了,但 NateCK 值得称赞;)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

图像文件的加密与解密 的相关文章

随机推荐

  • For 语句,每第 1000 次演练,做某事

    我正在遍历 For 循环 100 000 次 这个数字可以多样化 每第一千次我都想做一些特别的事情 那些我在其他演练中没有做的事情 像这样的东西 for int i 0 i lt 100000 i doTasks Normal if i 1
  • git-http-backend 与 apache2.4 Centos 7

    我尝试在我的 apache 服务器上设置 Git 服务器 但它不起作用 我得到了以下 git conf SetEnv GIT PROJECT ROOT var www html git project1 SetEnv GIT HTTP EX
  • Java 8 Stream API 中的多个聚合函数

    我有一个类定义如下 public class TimePeriodCalc private double occupancy private double efficiency private String atDate 我想使用 Java
  • 如何防止在 IE9 中加载页面时出现“无法获取属性‘dir’的值:对象为 null 或未定义”错误

    我有一个 Dojo 1 7 4 应用程序 在 IE9 中加载页面时出现 无法获取属性 dir 的值 对象为 null 或未定义 错误 我使用的是 AMD 版本 当它必须单独加载所有文件时 不会发生错误 我可以控制的所有代码都包含在 dojo
  • Kotlin:抑制未使用的属性?

    我的源代码如下 有警告 从未使用属性 我添加了 Suppress UNUSED PARAMETER Suppress UNUSED PROPERTY GETTER Suppress UNUSED PROPERTY SETTER 然而 它们都
  • 关闭 vba 生成的 Excel 绘图上的标记阴影

    我正在将一些用于在 Excel 中生成散点图的代码从 Win 7 Excel 2010 移植到 OS X Excel 2011 在 Mac 上 数据点显示有阴影 我不想要阴影 也不知道如何摆脱它 Using 这个工作表 http dl dr
  • C++ 指针数组的内存分配

    我有一个关于内存分配的问题 假设我创建了一个像这样的指针数组 int numbers new int 1024 1024 我原以为这需要 8MB 内存 Mac 64 位上为 8 字节指针 但事实并非如此 仅当为每个指针赋值时才分配内存 因此
  • 注册一个全局钩子,检测鼠标是否拖动文件/文本

    我知道有可能为鼠标注册全局钩子 http www codeproject com KB cs globalhook aspx移动 按钮单击 滚动等 但我想知道是否有任何方法可以检测用户是否实际上使用全局挂钩拖动文件或文本 或其他内容 似乎找
  • 从三地址代码到 JVM 字节码的代码生成

    我正在研究 Renjin 的字节码编译器 R 代表 JVM 并尝试将中间三地址码 TAC 表示形式转换为字节码 我查阅过的所有有关编译器的教科书都讨论了代码生成期间的寄存器分配 但我还没有找到任何用于在基于堆栈的虚拟机 如 JVM 上生成代
  • 向 ggplot 添加图例

    这个问题是这篇文章的后续问题 上一篇文章 https stackoverflow com questions 21531230 using geom path from ggplot library 我有12个变量 M1 M2 M12 为此
  • 将位图转换为多边形 - (反向光栅化)[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 给定一个位图图像 上面有一些纯色斑点 您将使用什么算法来构造与斑点形状相同的多边形 这可以通过多个步骤完成 稍后可以通过最佳拟合算法来切割高分辨
  • 如何在 WPF 应用程序中构建动态数据输入表单?

    我正在计划一个 WPF 应用程序 它将 能够创造动态数据输入表格 这意味着表单从数据库中的数据而不是从 XAML 获取要显示的字段及其顺序等 如果可能的话使用 MVVM 模式 我计划这样做 在客户数据输入视图中 我将设置数据上下文
  • 离子应用程序 | Firebase Crashlytics 无法与崩溃报告配合使用?

    我在我们的 Ionic 应用程序中使用 ionic native firebase 插件 并且该插件中包含崩溃报告 由于 Firebase 崩溃报告在 9 月 9 日之后将不再可用 因此我们正在尝试切换到 Firebase Crashlyt
  • 无法使用 C# 将 [] 索引应用于“System.Array”类型的表达式

    我正在尝试使用包含字符串数组的列表 但是当我尝试使用方括号访问数组元素时 我收到错误 我的数组列表声明如下 public List
  • 在 Valgrind 下运行 Eclipse

    这里有人成功运行 Eclipse 吗Valgrind http valgrind org 我正在与涉及 JNI 代码的特别棘手的崩溃作斗争 并希望 Valgrind 或许可以 再次 证明其卓越性 但是当我在 Valgrind 下运行 Ecl
  • nltk 函数计算某些单词的出现次数

    nltk书中有一个问题 使用 state union 语料库阅读器阅读国情咨文演讲的文本 计算每个文档中男性 女性和人物的出现次数 随着时间的推移 这些词的使用发生了什么变化 我想我可以使用像 state union 1945 Truman
  • Selenium WebDriver 中的 DesiredCapability 有什么用?

    Selenium WebDriver 中的 DesiredCapability 有什么用 我们什么时候想使用它以及如何使用 举例回答将不胜感激 您应该阅读有关的文档所需能力 https github com SeleniumHQ selen
  • 终端进程命令无法启动退出代码:0 和退出代码:2

    Visual Studio 代码终端无法工作 捷径ctrl 因为终端不工作 Error The terminal process terminated with exit code 0 终端进程命令 C WINDOWS System32 W
  • t.Cleanup 有什么用?

    问题 我想知道的用例t CleanupGo1 14中引入 与使用 defer 相比 t Cleanup 有何便利 https golang org pkg testing T Cleanup https golang org pkg tes
  • 图像文件的加密与解密

    结合我的另一个question https stackoverflow com questions 12131627 image encryption decryption 并且在更改了这一小部分代码之后 FileOutputStream