Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#加密和解密_C#_Security_C# 4.0_Encryption - Fatal编程技术网

C#加密和解密

C#加密和解密,c#,security,c#-4.0,encryption,C#,Security,C# 4.0,Encryption,在c#.net中,当我试图解密文件时,它会向我显示此错误,但对加密有效 错误:进程无法访问文件SecureDownloadManager.log,因为另一个进程正在使用该文件 代码: 该错误是由于未完全处理对象造成的。使用后,需要使用“using”子句来释放对象 您可以使用以下使用FileStream的代码: System.IO.File.WriteAllBytes(outputFile); 它取代了: FileStream fsOut = new FileStream(outputFile,

在c#.net中,当我试图解密文件时,它会向我显示此错误,但对加密有效

错误:进程无法访问文件SecureDownloadManager.log,因为另一个进程正在使用该文件

代码:


该错误是由于未完全处理对象造成的。使用后,需要使用“using”子句来释放对象

您可以使用以下使用
FileStream
的代码:

System.IO.File.WriteAllBytes(outputFile);
它取代了:

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

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

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

我希望它能解决您的问题。

请尝试以下代码,并告诉我它是否能解决问题

//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;
    using (FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create))
    {
        RijndaelManaged RMCrypto = new RijndaelManaged();

        using (CryptoStream cs = new CryptoStream(fsCrypt,
          RMCrypto.CreateEncryptor(key, key),
          CryptoStreamMode.Write))
        {
            using (FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);
            }
        }

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

我已经更改了您的加密代码,请将其复制到您的加密按钮单击,然后重试
//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;
    using (FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create))
    {
        RijndaelManaged RMCrypto = new RijndaelManaged();

        using (CryptoStream cs = new CryptoStream(fsCrypt,
          RMCrypto.CreateEncryptor(key, key),
          CryptoStreamMode.Write))
        {
            using (FileStream fsIn = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                    cs.WriteByte((byte)data);
            }
        }

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