AES padding and writing the ciphertext to a disk file

半腔热情 提交于 2019-12-02 02:05:29

Based on the comment:

In C++ I save a file combined_file2 << encrypt(buffer.str()); which I read in my C# program string plaintext = Decrypt(File.ReadAllBytes(path));

I don't think embedded NULLs is causing problems from the encrypt function since its returning a string, and that includes an explicit length.

However, an embedded NULL and the way the file is being written to disk in C++ (or read from disk in C#) will be a problem since this will stop at the first embedded NULL: combined_file2 << encrypt(buffer.str()).

Perhaps something like the following would be helpful:

StringSource ss(ciphertext, true /*pumpAll*/);
FileSink fs("my-encrypted-file.bin", true /*binary*/);
ss.TransferTo(fs);

If you are using a C++ stream, then use the write method on the stream object:

ofstream combined_file2;
...
combined_file2.write(ciphertext.data(), ciphertext.size());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!