Ciphertext is not text!
The input to and output from an encryption function is, strictly speaking, a bit string. A `bit string' is an ordered sequence of `bits', each of value either `0' or `1'. There isn't a convenient `bit string' type in the usual programming languages so we use an ordered sequence of `bytes' instead, 8 bits to one byte, and we almost always choose to work with values that are an exact multiple of 8 like 64 bits or 256 bits to make life easier.
The input to an encryption function is usually a representation of a text string like "Hello world!". Different systems store text in different ways. You need to convert the text to an unambiguous sequence of bytes before you encrypt it. For ECB and CBC modes you need to add padding bytes as well to ensure the input block is an exact multiple of the cipher block size.
Do not store ciphertext bytes in a string.
Once encrypted, the output is another sequence of bytes known as ciphertext. This sequence of bytes is generally not printable - it shows up as garbage. You can safely save this sequence of bytes directly to a binary file. It's often more convenient to encode the ciphertext bytes into a hexadecimal or base64 string, which is much easier to handle. But you do not convert the ciphertext back to text. It won't work.
When decrypting, after you've deciphered the ciphertext back to plaintext, you still have a sequence of bytes. You need to convert these bytes back to a string of text before you can read it, provided your decryption was successful.
Use the functions below to convert a string of text to an unambiguous array of bytes and vice versa.