Java 图像加密&;解密

Java 图像加密&;解密,java,image,encryption,aes,Java,Image,Encryption,Aes,我想对图像文件进行加密和解密。但是,当我运行这些代码时,它会给我 这个错误 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图像时,它无法

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

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图像时,它无法打开,因为文件似乎已损坏、损坏或太大

我已经尝试了很多方法,但是我仍然找不到错误。有人能帮我解决错误吗?多谢各位

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(如错误所示):

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


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

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

要将写入,您需要使用OutputStream(或FileOutputStream、Cipher或任何您需要的内容)。

不要将我的FileInputStream输出?意思当我删除
(ImageOutputStream)
时,写操作出错。它说,
类型ImageIO中的方法write(renderImage,String,ImageOutputStream)不适用于参数(BuffereImage,String,CipherInputStream)
不要命名您的FileInputStream输出*只是命名约定,请参见第二部分,尽管问题略有不同,请再次查看第二部分;您不希望它们都是输入,而是使所有输出、FileOutputStream、而不是InputI都已更改。但是这个错误出现在线程“main”java.lang.IllegalArgumentException:image==null中!在javax.imageio.ImageTypeSpecifier.CreateFromRenderImage(未知源代码)在javax.imageio.imageio.getWriter(未知源代码)在javax.imageio.imageio.write(未知源代码)在encypt.com.trial.main(trial.java:82)上,最清晰的图片是empty@Twister:我认为您应该检查文件“sheep.png”,因为根据文档,
ImageIO.read()
如果无法确定如何读取图像,将返回
null
。您确实需要了解输入和输出的差异。就像在你的新问题中一样,你在这里把它们严重地混淆了。@Twister这种方法可以安全地用于2或3个以上的图像加密吗?
        FileOutputStream output = new FileOutputStream("sheepTest.png");
        CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
        ImageIO.write(input, "PNG", cos);
        cos.close();