H
H
Hector Synchrome2021-03-05 23:19:16
C++ / C#
Hector Synchrome, 2021-03-05 23:19:16

Why is .txt not fully encrypted (CryptoStream, AES)?

I study encryption algorithms.
There is a code

string path = "palace.txt";
using (Aes aesAlg = Aes.Create())
            {
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (FileStream fsCrypt = new FileStream(path, FileMode.Open))
                {
                    using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                    {
                        int data;
                        while ((data = fsCrypt.ReadByte()) != -1)
                            cs.WriteByte((byte)data);
                    }
                }
            }

The "palace.txt" file contains the following text:

Barton did feebly change man she afford square add. Want eyes by neat so just must. Past draw tall up face show rent oh mr. Required is debating extended wondered as do. New get described applauded incommode shameless out extremity but. Resembled at perpetual no believing is otherwise sportsman. Is do he dispatched cultivated travelling astonished. Melancholy am considered possession on collecting everything.


And here is the text obtained after encryption:

Barton did feeblkѕ>›ty}&йМkя”M6 afford square ах ТФуКЗћ“х¶8–X neat so just muf†‚[email protected]їE†YFшll up face show %"qm\N7Ч^вЉ ‡їхired is debatingpQ…yahl‰cџ€ Еѕed as do. New geы†x%cKBs?eB#qIЂЪauded incommode r7–UЋWq
)XЂ‰0tremity but. ResЫzЬ-'хb•©џД%шual no believingнџёШ3~Ж0ІЬђEortsman. Is do hZKрњхW-
з am con.dXrX7YҐmR7on on
collecting


If you look closely, you can see that only half of the text was encrypted. I think this part is the problem.
int data;
while ((data = fsCrypt.ReadByte()) != -1)
     cs.WriteByte((byte)data);


Tell me, what is the error, what should I do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question