Search code examples
encryptionaesbouncycastlepublic-key-encryptiondes

Can I use PKCS5Padding padding algorithm while decryption for already encrypted data using PKCS7Padding?


I'm replacing PKCS7Padding padding with PKCS5Padding for my AES encryptions.

Can I use PKCS5Padding padding algorithm while decryption for already encrypted data using PKCS7Padding?

I tried using a sample program on local and somehow it works and didn't give any error. Just want to ensure if we need any sort of Migration for all the existing records.

For all the existing records, Do I need to decrypt first using PKCS7Padding and then again encrypt using PKCS5Padding and store it back in the DB?


Solution

  • You may check the following post https://crypto.stackexchange.com/questions/9043/what-is-the-difference-between-pkcs5-padding-and-pkcs7-padding

    PKCS5 and PKCS7 are effectively the same by algorithm, just PKCS5 is defined on 8 byte block (64 bit). Therefore cipher AES/.../PKCS5Padding is formally incorrect designation, effectively PKCS7 is used.

    Do I need to decrypt first using PKCS7Padding and then again encrypt using PKCS5Padding and store it back in the DB?

    No, just to change the padding you don't need to do anything