如何使用aes在java中加密/解密文件?
如何修复此问题?它不断抛出异常。正如你所看到的,我正试图使用一个图像作为密码,请你帮助我的加密/解密方法修复程序,使这项工作。我需要帮助我当前的代码如下:如何使用aes在java中加密/解密文件?,java,file,encryption,aes,Java,File,Encryption,Aes,如何修复此问题?它不断抛出异常。正如你所看到的,我正试图使用一个图像作为密码,请你帮助我的加密/解密方法修复程序,使这项工作。我需要帮助我当前的代码如下: import java.awt.image.*; import java.io.*; import java.security.*; import java.security.spec.InvalidKeySpecException; import javax.crypto.Cipher; import javax.crypto.spec.S
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);
}
}
}
导入java.awt.image.*;
导入java.io.*;
导入java.security.*;
导入java.security.spec.InvalidKeySpecException;
导入javax.crypto.Cipher;
导入javax.crypto.spec.SecretKeySpec;
导入sun.misc.*;
导入javax.swing.*;
/**
*
*@作者兰斯·格戴
*/
公共类加密{
私有静态最终字符串算法=“AES”;
公共静态字节[]键值;
//最大500 KB
公共静态字节[]值读取=新字节[512000];
公共静态void encrypt(文件f)引发异常{
FileInputStream in=null;
FileOutputStream out=null;
in=新文件输入流(f);
Key=generateKey();
Cipher c=Cipher.getInstance(算法);
c.init(Cipher.ENCRYPT_MODE,key);//我的代码在这里似乎失败了
String name=f.getName();
字符串newFileName=name.substring(0,name.lastIndexOf(“.”)
+“.enc”+name.substring(name.lastIndexOf(“.”),name.length();
File newFile=新文件(f.getParentFile(),newFileName);
out=新文件输出流(newFile);
//将文件读入valueToEnc并返回读取的字节数
valuesRead=新字节[Integer.MAX_VALUE];
int numberRead=in.read(valuesRead);
keyValue=新字节[numberRead];
for(int i=0;i0)&0xff);
keyValue[k++]=(字节)((x>>>8)和0xff);
keyValue[k++]=(字节)((x>>>16)和0xff);
keyValue[k++]=(字节)((x>>>24)和0xff);
}
}
}
虽然您没有真正提出问题,但您的密钥并不是真正的标准密钥。通常生成密钥的方法是:
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");
和//我的代码似乎在这里失败了什么是失败?给我们一个堆栈跟踪或什么的。我不知道出了什么问题,它只是突然抛出了一个异常抱歉,我是一个编程新手。你的GenerateKey不是标准的,或者至少我没有那样使用它。请参阅下面的答案以获取示例