【问题标题】:file encryption and decryption c# Rijndael cipherc# Rijndael cipher 文件加密和解密
【发布时间】:2012-11-18 22:25:31
【问题描述】:

我可以使用以下代码加密位于我桌面上的文本文件。

 private void btnEncrypt_Click(object sender, EventArgs e)
    {
        //EncryptFile(); 
        try
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "All Files (*.*)|";
            dialog.InitialDirectory = @"Desktop";
            dialog.Title = "Please select a file to encrypt.";

            dialog.ShowDialog();

            inputFile = dialog.FileName;

            outputFile = inputFile;

            string password = @"myKey123"; // Your Key Here
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);


            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

这很好用,但是当我尝试解密同一个文本文件时,没有任何反应,没有抛出错误消息让我知道发生了什么,我使用以下代码进行解密

private void btnDecrypt_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "All Files (*.*)|";
            dialog.InitialDirectory = @"Desktop";
            dialog.Title = "Please select a file to decrypt.";

            dialog.ShowDialog();

            inputFile = dialog.FileName; // "C:\\Users\\daniel\\Desktop\\text.txt";
            outputFile = inputFile;


                string password = @"myKey123"; // Your Key Here

                UnicodeEncoding UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

                RijndaelManaged RMCrypto = new RijndaelManaged();

                CryptoStream cs = new CryptoStream(fsCrypt,
                    RMCrypto.CreateDecryptor(key, key),
                    CryptoStreamMode.Read);

                FileStream fsOut = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                int data;
                while ((data = cs.ReadByte()) != -1)
                    fsOut.WriteByte((byte)data);

                fsOut.Close();
                cs.Close();
                fsCrypt.Close();

            }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        }
    }

有人可以帮忙吗?

【问题讨论】:

  • 在这种情况下,“效果很好”意味着“运行但完全不安全”。
  • outputFile != inputFile时是否解密?
  • 嗨 @CodesInChaos 我知道它不安全,这对我来说只是一个起点,但为输入欢呼

标签: c# visual-studio-2010 encryption rijndael


【解决方案1】:

fsOut 作为只读流打开。试试这个吧。

 FileStream fsOut = new FileStream(
       outputFile, 
       FileMode.Open, 
       FileAccess.Write, 
       FileShare.ReadWrite);

注意:我也会写入一个临时文件并在完成后复制原始文件,但我没有很好的理由。

【讨论】:

  • 嗨,奥斯汀,我稍后会试试这个,因为我没有随身携带笔记本电脑,我会告诉你我的进展情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
相关资源
最近更新 更多