I tested the code in your post and it seems to work correctly (I changed it to return the Base64 encoded data directly, rather than concatenating it to the QuestionMark
and ParameterName
), and Decrypt(Encrypt(New String("x"C, 800)))
worked fine.
This suggests that something is going on between your encrypting the data and subsequently decrypting it. The fact that you're concatenating it to QuestionMark
and ParameterName
suggests that you are perhaps putting it in a URL? If so there is likely a maximum length on the parameter data which is resulting in truncation of the ciphertext causing the error you're seeing when attempting to decrypt.
After Base64 encryption (which results in a 4/3 expansion of the output) 751 bytes of plaintext puts you just under 1024 bytes of encrypted data. However 752 bytes results in an additional block (16 bytes) of output, which after Base64 encoding results in 1024 bytes. It seems no coincidence that you're having problems at exactly 1KB of output.
It's probably also worth mentioning that your method for generating both the key and IV are potentially insecure - it appears that you are using a fixed salt when deriving the key bytes from the password which, depending on how this is being used could reduce security. The second problem is that you are also deriving the IV from the password. The IV should be random data, unrelated to the key being used, and a different IV should be used for every encryption. This ensures that repeated encryption of the same message with the same key still results in distinct ciphertext. The random IV can be transmitted (unencrypted) along with the ciphertext and extracted for use in the decryption process.