0

Javaで画像を正常に暗号化/復号化できないため、これについてアドバイスが必要です。ユーザーが画像を復号化するには、パスワードで保護されたテキスト ファイルや PDF ファイルと同様に、画像をクリックして画像パスワードを入力する必要があります。

以下はJavaで画像を暗号化する関数です

public static void ImgEncrypt()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("C:/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("C:/output.jpg");
             CipherOutputStream cos = new CipherOutputStream(
                    output, pbeCipher);
               //File outputFile = new File("image.png");
                 ImageIO.write(input,"JPG",cos);
              cos.close();    

    }
    }
4

1 に答える 1

0

ファイルを開いたときに復号化できる唯一の方法は、それが自己復号化ファイル (オペレーティング システムが実行できる実行可能ファイル) である場合、または関連するアプリケーションに復号化のためのプロトコルが含まれている場合です。自分で暗号化したファイルをクリックするだけで、それが機能することを期待することはできません。

于 2012-11-11T10:41:35.523 に答える