如何在c#中加密/解密pdf或doc文件而不改变其格式

时间:2017-03-07 18:04:52

标签: c#

使用doc或pdf库我无法加密文件及其格式我只能从doc读取文本文件并将其存储在字符串中并传递给加密函数和解密函数

  1. 我想阅读doc中的文字及其格式
  2. 将其传递给加密解密功能
  3. 将解密文本及其格式
  4. 存储在doc文件中

    else if (comboBox1.Text == ".doc")
    {
        var application = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document document = application.Documents.Open(textBox1.Text);
    
        // Loop through all words in the document.
        int count = document.Words.Count;
        string s2 = " ";
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;
            Console.WriteLine("Word {0} = {1}", i, text);
            s2 = s2 + text;
        }
        MessageBox.Show(s2);
        txtSend.Text = s2;
        // Close word.
        application.Quit();
    }
    
    string plainText=txtSend.Text;
    
    CryptLib _crypt = new CryptLib();
    //string plainText = "This is the text to be encrypted";
    String iv = CryptLib.GenerateRandomIV(16); //16 bytes = 128 bits
    string key = CryptLib.getHashSha256("my secret key", 31); //32 bytes = 256 bits
    MessageBox.Show(plainText);//////////////////////
    String cypherText = _crypt.encrypt(plainText, key, iv);
    Console.WriteLine("iv=" + iv);
    Console.WriteLine("key=" + key);
    Console.WriteLine("Cypher text=" + cypherText);
    MessageBox.Show(cypherText);
    textBox1.Text = cypherText;
    Console.WriteLine("Plain text =" + _crypt.decrypt(cypherText, key, iv));
    MessageBox.Show(_crypt.decrypt(cypherText, key, iv));
    textBox2.Text = _crypt.decrypt(cypherText, key, iv);
    

0 个答案:

没有答案