加密Word文档会更改最后一个字节

时间:2018-12-02 12:52:57

标签: c# encryption byte document caesar-cipher

因此,我正在尝试使用Caesar密码移位对Word文档进行加密/解密。我的Caesar类适用于images(.png)和(.txt)文档。但是,当我加密Word文档(.docx)并再次解密该Word文档时,最后一个字节将被修改。下图显示了我的意思: enter image description here

除最后一个字节外的所有字节均已正确更改。修改后的文档包含一个SOH字节,而不是NUL字节,它显然是“标头的开始”。

这是我的caeser密码类,其中包含加密和解密功能:

        static char ciphChar(char ch, int key)
    {
        if (!char.IsLetter(ch))
        {
            return ch;
        }
        char d = char.IsUpper(ch) ? 'A' : 'a';
        return (char)((((ch + key) - d) % 26) + d);
    }

    public static string Cipher(string message, int key, bool cipherMode) 
    {
        if (cipherMode == false)
        {
            key = 26 - key;
        }
        string output = string.Empty;
        foreach (char ch in message)
            output += ciphChar(ch, key);
        return output;
    }

加密和解密功能也在下面(Caeser密码通过密钥中的数字移动消息。因此,例如,如果消息为“ a”且密钥为3,则消息变为“ d”,因为“ a'在字母表中移动了3个字母为'd'):

        public static string Encipher(string input, int key)
    {
        return Cipher(input, key, true);
    }

    public static string Decipher(string input, int key)
    {
        return Cipher(input, key, false);
    }

要在文档上使用加密功能,我使用Convert.ToBase64String将文档字节转换为字符串。在将字节读取为字符串后,我使用了enciphe / deciphe函数,并使用以下代码Convert.FromBase64String将字符串转换回字节。

我的代码成功加密和解密了.txt文件和.png文件。但是,对于.docx文件,最后一个字节无法正确解码。.感谢您为解决我的问题提供的指导。

编辑1:我添加了一些代码,以便您可以重建我正在处理的问题。

  1. 创建一个名为caesarShift的类,并从我在原始帖子中上面编写的caesar cipher和enciper / deccipher函数中复制代码。

  2. 使用3个按钮和1个文本框创建一个主窗体。复制以下代码:

        public static byte[] EncryptFile(string filePath)
    {
        byte[] fileBytes = File.ReadAllBytes(filePath);
        byte[] bytesEncrypted = Enciphe(fileBytes);
        File.WriteAllBytes(filePath, bytesEncrypted);
        return bytesEncrypted;
    }
    
    public static byte[] DecryptFile(string filePath)
    {
        byte[] fileBytes = File.ReadAllBytes(filePath);
        byte[] bytesDecrypted = Deciphe(fileBytes);
        File.WriteAllBytes(filePath, bytesDecrypted);
        return bytesDecrypted;
    }
    
    static byte[] Enciphe(byte[] file)
    {
        var fileToString = Convert.ToBase64String(file);
        string caeser;
        caeser = caesarShift.Encipher(fileToString, 3);
        file = Convert.FromBase64String(caeser); 
        return file;
    }
    
    static byte[] Deciphe(byte[] file)
    {
        var fileToString = Convert.ToBase64String(file);
        string caeser;
        caeser = caesarShift.Decipher(fileToString, 3);
        file = Convert.FromBase64String(caeser);
        return file;
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "encryptable files|*.rtf;*.docx;*.jpg;*.txt;*.png;";
        openFileDialog1.Title = "Select File";
        if (Directory.Exists(textBox1.Text))
        {
            openFileDialog1.InitialDirectory = textBox1.Text;
        }
        else
        {
            openFileDialog1.InitialDirectory = @"C:\";
        }
    
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = openFileDialog1.FileName;
        }
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
       EncryptFile(textBox1.Text);
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        DecryptFile(textBox1.Text);
    }
    
  3. 创建一个伪单词文档并测试加密/解密功能。您还可以测试.jpg和.txt文件(它们正在工作)。

0 个答案:

没有答案
相关问题