0

次の例外が発生しています: Exception in thread "main" java.lang.ClassCastException: javax.crypto.CipherInputStream cannot be cast to javax.imageio.stream.ImageOutputStream

私はまだそれを理解することはできません。アドバイスをお願いします

// Example code showing how to use Java's Password-Based encryption. This
// example is a simplified form of the code in the documentation for the
// java cryptography architecture.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

public class sample {
   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("rose.jpg");
      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("output.jpg");
     CipherOutputStream cos = new CipherOutputStream(
            output, pbeCipher);
       //File outputFile = new File("image.png");
         ImageIO.write(input,"JPG",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.
   {
      // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      // Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
      // FileInputStream fileinput = new FileInputStream("output.jpg");
      // CipherInputStream cis = new CipherInputStream(fileinput, pbeCipher);
      // BufferedImage input = ImageIO.read(cis);


     File inputFile = new File("output.jpg");
     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("output.jpg");
       CipherInputStream cos = new CipherInputStream(
              output, pbeCipher);
        ImageIO.write(input,"JPG",(ImageOutputStream) cos);
        cos.close();

   }
  }
 }
4

2 に答える 2

0

最後から 3 行目に次のように記述します。

CipherInputStream cos = new CipherInputStream(
          output, pbeCipher);

私はそれがあるべきだと思います:

CipherOutputStream cos = new CipherOutputStream(
          output, pbeCipher);
于 2012-05-05T01:27:02.397 に答える
0

これが問題の原因となっているコードであると推測します。

    FileInputStream output = new FileInputStream("output.jpg");
    CipherInputStream cos = new CipherInputStream(
          output, pbeCipher);
    ImageIO.write(input,"JPG",(ImageOutputStream) cos);
    cos.close();

問題は簡単です。InputStream (実際には CipherInputStream) を OutputStream のサブタイプにキャストしようとしています。それは単にうまくいきません。

イメージを書きたい場合は、入力ストリーム チェーンではなく、出力ストリーム チェーンを作成する必要があります。画像を読みたい場合は、ImageIO.writeそれを読むために使用すべきではありません...

于 2012-05-05T01:28:18.197 に答える