首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像加解密

图像加解密
EN

Stack Overflow用户
提问于 2012-08-27 00:04:48
回答 1查看 5.1K关注 0票数 0

我想对图像文件进行加密和解密。然而,当我运行这段代码时,它给出了这个错误

代码语言:javascript
复制
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图像时,它无法打开,因为文件似乎已损坏、损坏或太大。

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

代码语言:javascript
复制
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();

   }
   }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-27 00:12:25

首先,不要给你的FileInputStream输出命名。其次,问题是您尝试将CipherInputStream转换为imageoutputstream (如错误所述):

代码语言:javascript
复制
ImageIO.write(input,"PNG",(ImageOutputStream) cos);

这是行不通的,因为CipherInputStream不是ImageOutputStream。

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

代码语言:javascript
复制
        FileOutputStream output = new FileOutputStream("sheepTest.png");
        CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);
        ImageIO.write(input, "PNG", cos);
        cos.close();

为了将写出,你需要使用一个OutputStream (或者FileOutputStream或者密码或者你需要的任何东西)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12131627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档