That is because the encrypted output is full of control characters, which means that you need to make sure that you read the file as a binary file, not as a text-file when you read the encrypted content.
getline, for example, is not a "binary" function, it will for example stop reading when it comes to a "newline". If we take a simple case of text:
and the key being "A":
the bytes in the output will be:
0x00, 0x03, 0x02, 0x05, 0x04, 0x07, 0x06, 0x09, 0x08, 0x0B, 0x0A, 0x0D
So the first problem is that you are using "X" as termination on your getline, which you may not find in the line when reading from the encrypted file.
Second problem is that the above string contains a newline (0x0A) which will be treated as a "special" character...
There are potentially other problems in your code as well, but those are major parts.
Try using "read" and "write" to the file instead, and if you are using a Windows machine to run this code, add "ios:binary" to the stream constructor calls.