【发布时间】:2016-09-10 21:39:44
【问题描述】:
所以我已经被困了很长时间,并且无法解密我使用 PBEWithHmacSHA256AndAES_128 加密的文件。
加密工作正常,但尝试解密时,出现以下错误:
线程“main”中的异常
java.security.InvalidAlgorithmParameterException:错误的参数类型:需要 PBE
错误发生在这一行:
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, ivSpec);
这是我使用的加密代码:
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(SALT, COUNT);
pbeKeySpec = new PBEKeySpec(get_SHA_1_SecurePassword(password, SALT).toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// PRINT HASH OF PW
System.out.println(get_SHA_1_SecurePassword(password, SALT).toCharArray());
// Create PBE Cipher and initialise PBE Cipher with key and parameters
Cipher pbeCipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Get bytes of input file
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
// Encrypt and get bytes of encrypted file
byte[] iv = pbeCipher.getIV();
byte[] outputBytes = pbeCipher.doFinal(inputBytes);
String fileName = inputFile.getName();
File outputFile = new File(fileName + ".enc");
FileOutputStream outputStream = new FileOutputStream(outputFile);
// store first 16 bytes in the file as the IV
outputStream.write(iv);
// store the rest of the encrypted file
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
return outputFile;
这是我正在尝试进行的解密:
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(SALT, COUNT);
pbeKeySpec = new PBEKeySpec(hashedReadPasswords[index].toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
// Get bytes of encrypted file
FileInputStream inputStream = new FileInputStream(encryptedFile);
byte[] inputBytes = new byte[(int)encryptedFile.length()];
inputStream.read(inputBytes);
// Get IV
byte[] iv = new byte[16];
for(int i=0; i<16; i++)
{
iv[i] = inputBytes[i];
}
// Get encrypted data
byte[] cipherBytes = new byte[inputBytes.length-16];
int y=0;
for(int i=16; i<inputBytes.length; i++)
{
cipherBytes[y] = inputBytes[i];
y++;
}
// Initialise PBE Cipher with key and parameters
IvParameterSpec ivSpec = new IvParameterSpec(iv);
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, ivSpec);
// decrypt and get bytes of plain text file
byte[] outputBytes = pbeCipher.doFinal(cipherBytes);
File outputFile = new File("decrypted_BS13");
FileOutputStream outputStream = new FileOutputStream(outputFile);
// store the rest of the decrypted file
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
return outputFile;
任何帮助都将不胜感激,无论我在网上找到什么 - 它都没有帮助!
【问题讨论】:
标签: java encryption cryptography passwords