How do I properly use the “PBEWithHmacSHA512AndAES_256” algorithm?

后端 未结 2 1187
闹比i
闹比i 2020-12-17 22:19

I\'m doing some Java encryption, and cannot figure out a way to properly use the the PBEWithHmacSHA512AndAES_256 algorithm.

Encryption seems to work fine, but I am u

相关标签:
2条回答
  • 2020-12-17 22:31

    I've enhanced the StandardPBEByteEncryptor from teh jasyptto support the "PBEWithHmacSHA512AndAES_256" algorithm. see code here

    See the unit test here PBEEncryptorTest

    0 讨论(0)
  • 2020-12-17 22:33
    // PROBLEM: If I pass "ivParamSpec", I get "java.security.InvalidAlgorithmParameterException: Wrong parameter type: PBE expected"
    // Whereas if I pass pbeParamSpec, I get "java.security.InvalidAlgorithmParameterException: Missing parameter type: IV expected"
    // What to do?
    cipherDecrypt.init(
        Cipher.DECRYPT_MODE,
        key,
        ivParamSpec
        //pbeParamSpec
        );
    

    Use the AlgorithmParameters from the encrypting Cipher:

    cipherDecrypt.init(
        Cipher.DECRYPT_MODE,
        key,
        cipherEncrypt.getParameters()
        );
    

    If you want a cleaner way that doesn't involve cipherEncrypt at the decrypting site, save the algorithm parameters as a byte and transmit them along with the key data:

    byte[]  algorithmParametersEncoded = cipherEncrypt.getParameters().getEncoded();
    

    and reconstruct them at the decrypting site thus:

    AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(ALGORITHM);
    algorithmParameters.init(algorithmParametersEncoded);
    

    and use algorithmParameters as the parameters argument for Cipher.init() above.

    0 讨论(0)
提交回复
热议问题