Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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# 对文件的路径访问被拒绝。AES加密文件的ReadAllBytes_C#_Xml_Encryption_Aes - Fatal编程技术网

C# 对文件的路径访问被拒绝。AES加密文件的ReadAllBytes

C# 对文件的路径访问被拒绝。AES加密文件的ReadAllBytes,c#,xml,encryption,aes,C#,Xml,Encryption,Aes,我正在尝试读取file.ReadAllBytes文件的二进制内容,并获得拒绝访问该文件的异常。我必须先打开文件吗?它在这个try-catch中采用异常路径并显示 无法加载配置数据。对路径“c:\worl\Project Alpha\Code\AlphaBackendService\AlphaBackendService\bin\Debug\alphaService.xml”的访问被拒绝 我错过了什么 try { string path = AppDomain.CurrentDomain.Ba

我正在尝试读取file.ReadAllBytes文件的二进制内容,并获得拒绝访问该文件的异常。我必须先打开文件吗?它在这个try-catch中采用异常路径并显示

无法加载配置数据。对路径“c:\worl\Project Alpha\Code\AlphaBackendService\AlphaBackendService\bin\Debug\alphaService.xml”的访问被拒绝

我错过了什么

try
{
  string path = AppDomain.CurrentDomain.BaseDirectory;
  eventLog1.WriteEntry(path);
  string fileName = System.IO.Path.Combine(path, "alphaService.xml");

  string sKey = "LvtZELDrB394hbSOi3SurLWAvC8adNpZiJmQDJHdfJU=";
  Byte[] keyBytes = Convert.FromBase64String(sKey);

  Byte[] contentsBytes = File.ReadAllBytes(fileName);

  string xmlStr = Encoding.UTF8.GetString(contentsBytes); 

  DecryptStringFromBase64String(xmlStr, keyBytes);

  eventLog1.WriteEntry(xmlStr);

  using (XmlReader reader = XmlReader.Create(new StringReader(xmlStr)))
  {
    reader.ReadToFollowing("DatabaseServerName");
    DatabaseServerName = reader.ReadElementContentAsString();
    reader.ReadToFollowing("DatabaseUserName");
    DatabaseUserName = reader.ReadElementContentAsString();
    reader.ReadToFollowing("DatabasePassword");
    DatabasePassword = reader.ReadElementContentAsString();
    reader.ReadToFollowing("RegistrationCode");
    RegistrationCode = reader.ReadElementContentAsString();
  }
  eventLog1.WriteEntry("Configuration data loaded successfully");
}
catch (Exception ex)
{
  eventLog1.WriteEntry("Unable to load configuration data.  " + ex.Message);
}
Decrypt函数需要一个包含内容的字符串,但它执行Convert.FromBase84String,因此我不知道是否应该使用File.ReadAllBytes

static string DecryptStringFromBase64String(string cipherText, byte[] Key)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");

        string plaintext = null;
        // this is all of the bytes
        var allBytes = Convert.FromBase64String(cipherText);
        // get our IV that we pre-pended to the data
        byte[] iv = new byte[KeySize / 16];
        Array.Copy(allBytes, iv, iv.Length);
        // get the data we need to decrypt
        byte[] cipherBytes = new byte[allBytes.Length - iv.Length];
        Array.Copy(allBytes, iv.Length, cipherBytes, 0, cipherBytes.Length);

        using (var aes = Aes.Create())
        {
            // Create a decrytor to perform the stream transform.
            var decryptor = aes.CreateDecryptor(Key, iv);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherBytes))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return plaintext;
    }

这意味着您需要一定级别的权限才能访问该文件。当该文件是纯XML时,我可以访问该文件。我检查了,这不是权限问题。您可以从命令行复制该文件吗?如果是,该程序是否以与您相同的权限运行?您可能有权限问题。另一种可能是其他线程或进程以独占模式打开文件。文件格式与文件访问无关。