Encryption 如何在java中加密图像

Encryption 如何在java中加密图像,encryption,Encryption,需要这方面的建议,因为我仍然无法在java中成功加密/解密图像。 对于要解密图像的用户,用户必须单击图像并输入图像密码,类似于受密码保护的文本文件或pdf文件 下面是java中图像加密的函数 public static void ImgEncrypt()throws Exception{ // Scanner to read the user's password. The Java cryptography // architecture poin

需要这方面的建议,因为我仍然无法在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();    

    }
    }

打开文件时可以解密的唯一方法是当文件是自解密文件时(操作系统可以运行的可执行文件),或者当相关应用程序包含用于解密的协议时。您不能只单击您自己加密的文件就期望它工作。

如果您有问题,请明确说明。如果有问题,请清楚地说明问题。如果该代码引发异常,请包含完整的堆栈跟踪。不要只是扔了一堵代码墙,然后抱着最好的希望。当我运行这个函数时,程序成功运行了。但是,当我打开图像文件时,预览无法显示(我假设它是加密的)。有没有一种方法可以让我输入密码来解密文件,比如打开一个受密码保护的.doc或.pdf文件?代码取自这里:想知道是否有java库用于加密图像文件(如Axcrypt)的软件?否则,当他们得到加密的图像时,他们将不得不下载并运行java应用程序来解密加密的图像文件,这相当麻烦。我认为这里没有一个简单的选项,用户1663380。如果这确实是您的问题,请指出这一点(并可能投票赞成/接受)。