AES加密文件上的File.ReadAllBytes拒绝访问该路径

时间:2014-04-08 17:01:20

标签: c# xml encryption aes

我正在尝试使用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;
    }

0 个答案:

没有答案