javax.crypto.BadPaddingException:Given final block not properly padded

后端 未结 4 1598
忘了有多久
忘了有多久 2021-02-04 07:50

I have to decrypt a frame on my server. Encrypted frame comes from client device through GPRS on socket.Encryption is done with TripleDes and

4条回答
  •  北海茫月
    2021-02-04 07:59

    The main issue with your code is that you are decrypting using a default of PKCS5Padding. "TripleDES" will result in "TripleDES/ECB/PKCS5Padding" internally. This is as it is implemented in the Sun JCE provider; most other providers copy this default.

    It seems you are expecting zero padding, which means you should use "DESede/ECB/NoPadding" instead. After that you can use an external function to calculate the plain text size (removing zero padding may remove zero valued plain text at the end if you are not careful).

    Other issues:

    • trying to pad data before decryption (you should unpad data after decryption)
    • encoding and character encoding issues, such as trying to pad with the character value of "0", which is probably wrong

    I've indicated "ECB" because I don't know the actual mode used. You could amend your question with the right mode and padding algorithm if you can find out. You might want to try CBC mode as well if ECB does not work.

    Note that ECB mode is not safe to use except for very specific circumstances. Using CBC with a randomized IV is a minimal requirement.

提交回复
热议问题