I'm working on a problem with encrypted data transfer, where we get a "corrupted pad block" failure on decryption approx 1 in 200 times. The scheme is quite similar to encrypted emails (and designed for rather large amounts of data: up to one GB per unit):
- Generate random 256 bytes to use as session key.
- Encrypt the session key using the receiver's public key
- Encrypt data with CBC AES PKCS#7 padding, using the session key as key parameter for AES.
The receiver will then
- Decrypt the session key using his private key.
- Decrypt the data using the session key.
Now, on average approx 1 in 200 times, the random bytes will cause the data to be un-decryptable: we get "pad block corrupted" at the end of the decryption. The same byte sequence will either always work or always cause failure. It seems like the contents of the data to encrypt does not make any difference (but I still have some testing left in this area).
Any suggestions on how to avoid failure ?