无法解密我使用PBEWithHmacSHA256AndAES_128

时间:2016-05-14 19:56:04

标签: java encryption cryptography passwords

所以我被困了很多年,并且无法解密我使用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;

无论我在网上找到什么,都会非常感谢任何帮助 - 它没有帮助!

2 个答案:

答案 0 :(得分:1)

编辑:请参阅this blog,例如PBE加密和解密代码(尽管不使用博客正在使用的相同密码,因为它们不安全!)。 / p>

这里有一些问题。

这绝对是错误的,虽然它没有引起你所描述的问题:

pbeKeySpec = new PBEKeySpec(hashedReadPasswords[index].toCharArray());

您需要提供与加密密码相同的密码。您无法输入哈希密码来解密。因此,您需要传入与加密时类似的pbeKeySpec。

PBE表示“基于密码的加密”。它是一种以安全的方式将密码转换为加密密钥的算法。这意味着您解密的密钥需要与您加密的密钥相同,并以相同的方式派生。

答案 1 :(得分:0)

这个问题在这里得到解决:How do I properly use the "PBEWithHmacSHA512AndAES_256" algorithm?

由于该算法使用 AES 进行加密,因此您需要使用 IV 进行解密。虽然,您还需要PBE参数(您得到的错误),因此,要获得 IvParameterSpec PBEParameterSpec 有一个变量,调用Cipher pbeCipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128"); pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); AlgorithmParameters params = pbeCipher.getParameters(); ,返回 AlgorithmParameters 实例,其中包含密码上使用的 IvParameterSpec PBEParameterSpec
警告:您必须在init

之后调用该方法

密码

pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, params);

破译

{{1}}