System.ArgumentException:路径中的非法字符

时间:2014-05-27 16:42:54

标签: c# asp.net-mvc

我正在尝试解密加密文件,但是我在上述方法(StreamReader ...)的第4行中遇到System.ArgumentException: Illegal characters in path.错误。任何人都可以告诉我为什么会发生这种确切的错误?我没有任何路径传递错误发生的方法但是前一个方法的路径是要解密的文件的绝对路径

方法:

   public static string AsymmDecrypt(string input, string privatekeyfilename)
        {
            RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider();
            StreamReader sr = new StreamReader(privatekeyfilename);
            myAlg.FromXmlString(sr.ReadLine());
            sr.Close();

            byte[] cipher = myAlg.Decrypt(Convert.FromBase64String(input), true);
            return UTF8Encoding.UTF8.GetString(cipher);
        }

传递参数:

input = "����\0JFIF\0\0H\0H\0\0��\0C\0\b"

privatekeyfilename = "<RSAKeyValue><Modulus>2o6sUW4eEF43RK6SWyxuII0wfmhyiTXkBVX8l5J20Y5IvY10/7W3vT0RkwB/lcZUNUgOC2PG6ISJQ53PR76yybSodU9uNmYZye+eJnImP+a6SFZKMxeFESFkye5kroHc4qQ76Zyb37YcMulD214EWnyiu3gcBoA5yyOXyM8PojU=</Modulus><Exponent>AQAB</Exponent><P>7Ke13x362OFEr7VjYmpJOpC+...

堆栈追踪:

    [ArgumentException: Illegal characters in path.]
   System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) +10631190
   System.IO.Path.GetFileName(String path) +21
   System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) +82
   System.IO.StreamReader..ctor(String path) +32
   eLearningLTD.Securities.EncryptDecrypt.AsymmDecrypt(String input, String privatekeyfilename) in D:\Assignments\SSD\aAssignment\eLearningLTD\eLearningLTD\Securities\EncryptDecrypt.cs:211
   eLearningLTD.Securities.EncryptDecrypt.HybridDecryption(String filePath, String publicKey, String privateKey) in D:\Assignments\SSD\aAssignment\eLearningLTD\eLearningLTD\Securities\EncryptDecrypt.cs:178
   eLearningLTD.Controllers.ResourcesController.download(String id) in D:\Assignments\SSD\aAssignment\eLearningLTD\eLearningLTD\Controllers\ResourcesController.cs:213
   lambda_method(Closure , ControllerBase , Object[] ) +180
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +324
   System.Web.Mvc.Controller.ExecuteCore() +106
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +19
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 个答案:

答案 0 :(得分:0)

问题是: StreamReader sr = new StreamReader(privatekeyfilename); 它需要path,但您只需传递私钥。

要解决您必须更换的问题:

     StreamReader sr = new StreamReader(privatekeyfilename);
     myAlg.FromXmlString(sr.ReadLine());
     sr.Close();

使用

     myAlg.FromXmlString(privatekeyfilename);