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

后端 未结 4 1599
忘了有多久
忘了有多久 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 08:17

    (3)DES encrypts/decrypts blocks of 8 bytes. As not all texts are precisely 8 bytes, the last block must contain bytes that are not original from the plain text.

    Trick is to find out which one is the last character of the plain text. Sometimes the length of the plain text is known beforehand - then the padding characters can be anything really.

    If the length of the plain text is not known then a deterministic padding algorithm must be used, e.g. PKCS5Padding. PKCS5Padding always performs padding, even if the plaintext is N * blocksize in bytes. The reason for this is simple: otherwise it doesn't know if the last byte is plain text or padding.

    I will try to come with a working code later...have to test it. In the meantime try using the padding algorithms.

提交回复
热议问题