【问题标题】:Cannot decrypt a file that I encrypted using PBEWithHmacSHA256AndAES_128无法解密我使用 PBEWithHmacSHA256AndAES_128 加密的文件
【发布时间】: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


    【解决方案1】:

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

    这里有一些问题。

    这绝对是错误的,尽管它不会导致您描述的问题:

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

    您需要提供与加密时相同的密码。您不能输入散列密码来解密。因此,您需要传入与加密时类似的 pbeKeySpec。

    PBE 的意思是“基于密码的加密”。它是一种以安全方式将密码转换为加密密钥的算法。这意味着您解密时使用的密钥必须与加密时使用的密钥相同,并且派生方式相同。

    【讨论】:

    • 你说得对,我知道你不应该使用实际的哈希值进行加密,但我试图使用相同的哈希值进行解密(我使用了相同的盐)。有没有办法在不使用外部库的情况下让它工作?我只应该使用java自带的标准密码库
    • @chozenn300 我认为我引用的博客只使用了 Java,没有使用外部库。
    【解决方案2】:

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

    由于算法使用AES加密,所以需要用到IV来解密。虽然,你还需要 PBE 参数(你得到的错误),所以,要获得 IvParameterSpecPBEParameterSpec 有一个变量,你调用 pbeCipher.getParameters() 返回一个 AlgorithmParameters 实例,其中包含密码上使用的 IvParameterSpecPBEParameterSpec
    警告:您必须在init之后调用方法

    密码

    Cipher pbeCipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    AlgorithmParameters params = pbeCipher.getParameters();
    

    解密

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      相关资源
      最近更新 更多