如何使用aes在java中加密/解密文件?

时间:2021-10-21 18:24:24

How do I fix this it keeps throwing exceptions. As you can see i am trying to use an image as a password would you please help program of fix my encrypt/decrypt method so this works. I need help my current code is as follows:

我如何解决这个问题,它不断抛出异常。正如你所看到我试图使用图像作为密码,你请帮助程序修复我的加密/解密方法所以这是有效的。我需要帮助我的当前代码如下:

import java.awt.image.*;
import java.io.*;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
import javax.swing.*;

/**
*
* @author Lance Gerday
*/
public class Encrypt {

   private static final String ALGORITHM = "AES";
   public static byte[] keyValue;
   // 500 KB max
   public static byte[] valuesRead = new byte[512000];

   public static void encrypt(File f) throws Exception {
       FileInputStream in = null;
       FileOutputStream out = null;
       in = new FileInputStream(f);
       Key key = generateKey();
       Cipher c = Cipher.getInstance(ALGORITHM);
       c.init(Cipher.ENCRYPT_MODE, key);//my code seems to fail here


       String name = f.getName();
       String newFileName = name.substring(0, name.lastIndexOf("."))
               + ".enc" + name.substring(name.lastIndexOf("."), name.length());
       File newFile = new File(f.getParentFile(), newFileName);
       out = new FileOutputStream(newFile);
       //reads the file into valueToEnc and returns the number of bytes read
       valuesRead = new byte[Integer.MAX_VALUE];
       int numberRead = in.read(valuesRead);
       keyValue = new byte[numberRead];
       for (int i = 0; i < numberRead; i++) {
           keyValue[i] = valuesRead[i];
       }
       byte[] encValue = c.doFinal(keyValue);
       String encryptedValue = new BASE64Encoder().encode(encValue);
       out.write(encryptedValue.getBytes());
   }

   public static void decrypt(File f) throws Exception {
       Key key = generateKey();
       Cipher c = Cipher.getInstance(ALGORITHM);
       c.init(Cipher.DECRYPT_MODE, key);

       FileInputStream in = null;
       FileOutputStream out = null;

       if (f.canRead()) {
           in = new FileInputStream(f);
       }

       String name = f.getName();
       String newFileName = name.substring(0, name.lastIndexOf(".enc"));
       File newFile = new File(f.getParentFile(), newFileName);
       out = new FileOutputStream(newFile);
       //reads the file into valueToEnc and returns the number of bytes read
       valuesRead = new byte[Integer.MAX_VALUE];
       int numberRead = in.read(valuesRead);
       keyValue = new byte[numberRead];
       for (int i = 0; i < numberRead; i++) {
           keyValue[i] = valuesRead[i];
       }
       String encryptedValue = new String(keyValue);
       byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
       byte[] decValue = c.doFinal(decordedValue);
       out.write(decValue);
   }

   private static Key generateKey() throws Exception {
       Key key = new SecretKeySpec(keyValue, ALGORITHM);
       return key;
   }

   public static void setKeyValue(File f) {
       BufferedImage img = null;
       try {
           img = javax.imageio.ImageIO.read(f);
       } catch (Exception e) {
           JOptionPane.showMessageDialog(null, "Fail error at line 92");
       }
       Raster r = img.getData();
       int[] data = r.getPixels(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight(), (int[]) null);
       for (int a : data) {
       }
       int dataLength = data.length;
       keyValue = new byte[dataLength << 2];

       for (int i = 0; i < dataLength; i++) {
           int x = data[i];
           int k = i << 2;
           keyValue[k++] = (byte) ((x >>> 0) & 0xff);
           keyValue[k++] = (byte) ((x >>> 8) & 0xff);
           keyValue[k++] = (byte) ((x >>> 16) & 0xff);
           keyValue[k++] = (byte) ((x >>> 24) & 0xff);
       }
   }
}

1 个解决方案

#1


2  

Though you don't really ask for a question, your secret key isn't really standard. Usually the way to generate is:

虽然你并没有真正提出问题,但你的秘密密钥并不是真正的标准。通常生成方式是:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);  // or 192 or 256
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Reference AES and JCE

参考AES和JCE

#1


2  

Though you don't really ask for a question, your secret key isn't really standard. Usually the way to generate is:

虽然你并没有真正提出问题,但你的秘密密钥并不是真正的标准。通常生成方式是:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);  // or 192 or 256
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Reference AES and JCE

参考AES和JCE