-2

我在网上找到了这段代码我想知道在这个程序中传递了什么这个输出和输入字符串是什么?我将输入作为文件名传递,并将输出作为路径传递,但它给出了错误。

private void EncryptFile(string inputFile, string outputFile)
{
    try
    {
        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);

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

        fsIn.Close();
        cs.Close();
        fsCrypt.Close();
    }
    catch
    {
        MessageBox.Show("Encryption failed!", "Error");
    }
}
4

1 回答 1

2

outputFile参数不是路径,而是需要写入的完全限定文件名。调用此代码的示例方法是:

EncryptFile(@"c:\temp\unencryptedfile.txt", @"c:\temp\encryptedfile.txt");

除此之外,将catch代码替换为以下内容:

catch(Exception ex) {
  MessageBox.Show(ex.Message); // will show the top exception
  if (ex.InnerException != null) {
    MessageBox.Show(ex.InnerException.Message); // will show additional details if present
  }
}

旁注:您知道,您拥有的代码会泄漏内存。您可能想要调查该using子句并查找您正在使用哪些类 implement IDisposable

于 2012-10-11T17:41:54.093 回答