图像加密&解密 [英] Image Encryption & Decryption

查看:206
本文介绍了图像加密&解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对图像文件进行加密和解密。但是,当我运行此代码时,它会给我
这个错误

I want to do encrypt and decrypt on image files. However, when i run this codes, it give me this error

Exception in thread "main" java.lang.ClassCastException: javax.crypto.CipherInputStream cannot be cast to javax.imageio.stream.ImageOutputStream
at encypt.com.trial.main(trial.java:82)

当我试图打开sheepTest.png图像时,它无法打开,因为文件似乎已损坏,损坏或太大。

and when i tried to open the sheepTest.png image, it cannot be open as the file appears to be damaged, corrupted or it is too large.

我已经尝试了很多方法,但是我仍然找不到错误。任何人都可以帮我解决错误吗?谢谢。

I've tried many ways already, however I still cannot find the mistake.Can anyone help me to solve the error? Thank you.

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));*/
       FileInputStream output = new FileInputStream("sheepTest.png");
       CipherInputStream cos = new CipherInputStream(
              output, pbeCipher);
        ImageIO.write(input,"PNG",(ImageOutputStream) cos);
        cos.close();

   }
   }
}


推荐答案

首先,不要命名您的FileInputStream输出。第二,问题是你试图将CipherInputStream转换为imageoutputstream(如错误所示):

First of all, don't name your FileInputStream output. Second, the issue is that you try to cast your CipherInputStream to an imageoutputstream (like the error says) here:

ImageIO.write(input,"PNG",(ImageOutputStream) cos);

这不起作用,因为CipherInputStream不是ImageOutputStream。

This won't work as CipherInputStream isn't an ImageOutputStream.

好的,在你的命名上发现了另一个问题;我认为你把它命名为正确,但使用了错误的类;将最后几行更改为:

Alright, found a further issue on your naming; I think you named it right, but used the wrong class; change your last few lines to this:

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

要编写 out ,您需要使用OutputStream(或FileOutputStream或密码或你需要的任何东西)。

For writing out, you want to use an OutputStream (or FileOutputStream or Cipher or whatever you need).

这篇关于图像加密&解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆