文件加密/解密Java
我正在尝试使用 AES 和 Java 加密库加密文件。但这是我在解密文件时发生的错误:“解密时出错:javax.crypto.BadPaddingException:给定的最终块未正确填充。如果在解密过程中使用了错误的密钥,则可能会出现此类问题。”
到目前为止,这是我的代码:
public static void encrypt(File input, String key, File output) {
try
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(key.toCharArray(), key.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
FileInputStream inputStream = new FileInputStream(input);
byte[] inputBytes = new byte[(int) input.length()];
int count;
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(output);
while ((count = inputStream.read(inputBytes, 0, inputBytes.length)) > 0)
{
outputStream.write(inputBytes, 0, count);
}
inputStream.close();
outputStream.close();
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
}
public static void decrypt(File input, String key, File output)
{
try
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(key.toCharArray(), key.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(input);
byte[] inputBytes = new byte[(int) input.length()];
int count;
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(output);
while ((count = inputStream.read(inputBytes, 0, inputBytes.length)) > 0)
{
outputStream.write(inputBytes, 0, count);
}
inputStream.close();
outputStream.close();
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
}
可能是什么问题? 提前致谢!
I'm trying to encrypt a file using AES and Java Crypto Library. But this is the error Which happens while I'm decrypting the file: "Error while decrypting: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption."
This is my code so far:
public static void encrypt(File input, String key, File output) {
try
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(key.toCharArray(), key.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
FileInputStream inputStream = new FileInputStream(input);
byte[] inputBytes = new byte[(int) input.length()];
int count;
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(output);
while ((count = inputStream.read(inputBytes, 0, inputBytes.length)) > 0)
{
outputStream.write(inputBytes, 0, count);
}
inputStream.close();
outputStream.close();
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
}
public static void decrypt(File input, String key, File output)
{
try
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(key.toCharArray(), key.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
FileInputStream inputStream = new FileInputStream(input);
byte[] inputBytes = new byte[(int) input.length()];
int count;
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(output);
while ((count = inputStream.read(inputBytes, 0, inputBytes.length)) > 0)
{
outputStream.write(inputBytes, 0, count);
}
inputStream.close();
outputStream.close();
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
}
What could be the problem?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论