First, you should use AES, as it is a de facto standard.
AES encrypts bytes with a block size of 16 bytes. Obviously, this is great for any large piece of data. But the last bit, obviously, can be less than 16 bytes.
For the last block you will need to place it, and a typical addition is done through PCKS7, which is pretty straight forward.
Suppose you have a line: "icecream" as the last block.
"icecream" is 8 bytes, so you need 8 more bytes to make a block
So you just add the character 8 (not 8) 8 times
"icecream\x08\x08\x08\x08\x08\x08\x08\x08"
There will be your final line. You will now encrypt the data.
Remember that when decrypting, you will need to catch this last block and remove the gasket before using it.
source share