Why doesn’t copy-pasting from a terminal preserve the original bytes?

18 hours ago 2
ARTICLE AD BOX

I’m working on a simple encryption/decryption program (C++), where I encrypt a string and then decrypt it back.

If I write the encrypted data directly to a file using binary mode and then read it back, everything works correctly.

However, if I print the encrypted data to the terminal, copy-paste that output into a .txt file, and then try to decrypt it, I get incorrect results (wrong characters for space, comma and exclamation mark).

My understanding is that the encrypted data is just raw bytes (0–255), not valid text. So I suspect the issue is related to how the terminal handles encoding (e.g. UTF-8) when displaying and copying the data.

So what exactly happens during terminal output and copy-paste that causes this corruption?

Edit: to clarify the issue, here is a concrete example.

Original text: Dobber dan, danes sem zasspana!

If I encrypt it, write it to a file (binary), and then decrypt it, I get: Dobber dan, danes sem zasspana!

However, if I:

print the encrypted data to the terminal copy-paste that output into a .txt file decrypt from that file

I get corrupted output like: Dobberµéùdan+τÅÿdanerτÅÿsem zasspana←ΩÜ╕

So the problem only appears when the encrypted data is copied through the terminal. My question here is why does copying output from a terminal change the original bytes?

Read Entire Article