使用FileStream加密时FileNotFoundException

时间:2015-07-21 00:33:50

标签: c# encryption

我在Visual Studio Windows窗体应用程序中使用C#来编写可以加密和解密文件的程序。我正在完成这个演练:https://msdn.microsoft.com/en-US/library/Aa964697(v=VS.85).aspx并完成所有工作,并对我的环境和偏好进行了一些细微的更改。

当我尝试加密文件时,我得到了一个' FileNotFoundException未处理'程序尝试使用文件流加密文件时出错。到目前为止,一切似乎都在起作用。

以下是EncryptFile方法的代码:

private void EncryptFile(string inFile)
{
    // Create an instance of Rijndael for symmetric encryption of the data.
    RijndaelManaged rjndl = new RijndaelManaged();
    rjndl.KeySize = 256;
    rjndl.BlockSize = 256;
    rjndl.Mode = CipherMode.CBC;
    ICryptoTransform transform = rjndl.CreateEncryptor();

    // Use RSACryptoServiceProvider to enrypt the Rijndael key.
    byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);

    // Create byte arrays to contain the length values of the key and IV.
    byte[] LenK = new byte[4];
    byte[] LenIV = new byte[4];

    int lKey = keyEncrypted.Length;
    LenK = BitConverter.GetBytes(lKey);
    int lIV = rjndl.IV.Length;
    LenIV = BitConverter.GetBytes(lIV);

    // Write the following to the FileStream for the encrypted file (outFs):
    // - length of the key
    // - length of the IV
    // - ecrypted key
    // - the IV
    // - the encrypted cipher content

    // Change the file's extension to ".enc"
    string outFile = EncrFolder + inFile.Substring(0, inFile.LastIndexOf(".")) + ".enc";

    using (FileStream outFs = new FileStream(outFile, FileMode.Create))
    {
        outFs.Write(LenK, 0, 4);
        outFs.Write(LenIV, 0, 4);
        outFs.Write(keyEncrypted, 0, lKey);
        outFs.Write(rjndl.IV, 0, lIV);

        // Now write the cipher text using a CryptoStream for encrypting.
        using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
        {
            // By encrypting a chunk at a time, you can save memory and accommodate large files.
            int count = 0;
            int offset = 0;

            // blockSizeBytes can be any arbitrary size.
            int blockSizeBytes = rjndl.BlockSize / 8;
            byte[] data = new byte[blockSizeBytes];
            int bytesRead = 0;

            using (FileStream inFs = new FileStream(inFile, FileMode.Open))
            {
                do
                {
                    count = inFs.Read(data, 0, blockSizeBytes);
                    offset += count;
                    outStreamEncrypted.Write(data, 0, count);
                    bytesRead += blockSizeBytes;
                }
                while (count > 0);
                inFs.Close();
            }
            outStreamEncrypted.FlushFinalBlock();
            outStreamEncrypted.Close();
        }
        outFs.Close();
    }
}

错误发生在"使用(FileStream inFs = new FileStream(inFile,FileMode.Open))"。 这是错误的图像:

enter image description here

导致错误的原因是什么?

1 个答案:

答案 0 :(得分:0)

你可能会忽略这一行:

string outFile = EncrFolder + inFile.Substring(0, inFile.LastIndexOf(".")) + ".enc";

https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx