ByteArray加密后文件损坏 [英] File Corrupted after ByteArray Encryption

查看:79
本文介绍了ByteArray加密后文件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我加密/解密文本文档,我写的这种加密方法。

然后我尝试加密word文档纯文本(内容),遗憾的是某些字符不受支持。

所以我决定加密整个文件字节。

我认为这种方法可以加密任何文件,因为现在我加密了整个字节。

但是在我加密之后解密,文件损坏,文本文件除外。

这是我的代码。



This encryption method I write working if I encrypt / decrypt text document.
Then I tried to encrypt word document plain text (content), and sadly some character was unsupported.
So I decided to encrypt the whole file bytes.
I thought this method can encrypt any file because now I encrypt the whole bytes.
But after I encrypt and decrypt, the file corrupted except text document.
This is my code.

byte[] b = null;

            b = System.IO.File.ReadAllBytes(this.txtFile.Text);

            string key = this.txtKey.Text;
            int keyIndex = 0;

            for (int i = 0; i < b.Length; i++) 
            {
                if (keyIndex >= key.Length)
                {
                    keyIndex = 0; //key will repeat all over again
                }

                string fileBIN = Convert.ToString(b[i], 2);
                string keyBIN = Convert.ToString(Convert.ToInt32(Convert.ToChar(key[keyIndex])), 2);
                string newBIN = string.Empty;

                keyIndex += 1;

                if (fileBIN.Length > keyBIN.Length)
                {
                    for (int x = 0; x < Math.Abs(fileBIN.Length - keyBIN.Length); x++) { keyBIN = "0" + keyBIN; x -= 1; } //add 0 to binary that was shorter
                }
                else
                {
                    for (int x = 0; x < Math.Abs(fileBIN.Length - keyBIN.Length); x++) { fileBIN = "0" + fileBIN; x -= 1; } //add 0 to binary that was shorter
                }

                if (fileBIN == keyBIN)
                {
                    newBIN = fileBIN;
                }
                else
                {
                    for (int x = 0; x < fileBIN.Length; x++)
                    {
                        if (fileBIN.Substring(x, 1).ToString() == keyBIN.Substring(x, 1).ToString()) { newBIN += "0"; }
                        else { newBIN += "1"; }
                    }
                }
                b[i] = Convert.ToByte(newBIN, 2);
            }

            System.IO.File.WriteAllBytes(this.txtFile.Text, b);

推荐答案

它不起作用的原因是因为你将所有内容都转换为字符串。加密系统(除了非常基本的东西)总是在字节流上工作,而不是字符串。你写的这个本土的东西永远不会正常工作。它只是没有意义。
The reason it doesn't work is because you're converting everything to strings. Encryption systems (anything but very rudimentary) always work on byte streams, not strings. This "home grown" thing you wrote is never going to work right. It just doesn't make sense.


某些角色不受支持 - 已经荒谬了。永远不要尝试加密字符。首先,将所有数据序列化为字节数组, byte [] ;它可以使用 System.Text.Encoding 类中的一个来完成:

http://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110 %29.aspx [ ^ ]。



当然,只使用UTF编码,最好是UTF-8。



然后进行加密,并在需要时进行反向操作:解密并从 byte [] 获取字符串。



参见: http://msdn.microsoft.com/en-us/library/System.Security.Cryptography%28v=vs.110%29.aspx [ ^ ]。



很抱歉,但挖掘代码并不是很有趣。为什么浪费任何已经毫无意义的时间?



-SA
"Some character in unsupported" — is already the absurd. Never ever try to encrypt characters. First, serialize all data into array of bytes, byte[]; it can be done using one of System.Text.Encoding classes:
http://msdn.microsoft.com/en-us/library/system.text.encoding%28v=vs.110%29.aspx[^].

And, of course, only use UTF encodings, better be UTF-8.

Then do encryption, and, when you need it, the reverse operations: decryption and getting strings from byte[].

See also: http://msdn.microsoft.com/en-us/library/System.Security.Cryptography%28v=vs.110%29.aspx[^].

Sorry, but digging in your code is just not interesting. Why wasting any time which already makes no sense?

—SA


这篇关于ByteArray加密后文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆