2012-02-23 123 views
-1

我该如何解决这个问题,一直抛出异常。正如你可以看到我试图使用一个图像作为密码,你会帮助解决我的加密/解密方法的程序,所以这个工程。我需要帮助,我现在的代码如下:如何使用aes在java中加密/解密文件?

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); 
       } 
   } 
} 
+3

和问题是什么? – vulkanino 2012-02-23 15:01:33

+0

_ //我的代码似乎在这里失败_什么是失败?给我们一个堆栈跟踪什么的。 – TDJoe 2012-02-23 15:14:03

+0

我不知道出了什么问题,只是抽搐抛出一个异常对不起,我是一个编程新手 – lancegerday 2012-02-23 15:17:38

回答

2

虽然你没有真的问一个问题,你的密钥并不是真正的标准。通常生成的方法是:

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 AESJCE