首页 新闻 搜索 专区 学院

RSACryptoServiceProvider 使用证书私钥解密异常:不正确的项

0
[待解决问题]
 1             X509Certificate2 myX509 = new X509Certificate2(@"d:\Test.pfx",
 2                 "Test@2013");
 3 
 4             RSACryptoServiceProvider pubRSA = myX509.PublicKey.Key as RSACryptoServiceProvider;
 5             RSACryptoServiceProvider priRSA = myX509.PrivateKey as RSACryptoServiceProvider;
 6 
 7             byte[] source = Encoding.Unicode.GetBytes(Console.ReadLine());
 8             byte[] encrypt = null;
 9             byte[] decrypt = null;
10             encrypt = pubRSA.Encrypt(source, false);
11             Console.WriteLine("Encrypt:{0}", Encoding.Unicode.GetString(encrypt, 0, encrypt.Length));
12             decrypt = priRSA.Decrypt(encrypt, false);
13             Console.WriteLine("Decrypt:{0}", Encoding.Unicode.GetString(decrypt, 0, decrypt.Length));

 

异常提示:

执行到第12行时:解密时抛出异常

未处理 System.Security.Cryptography.CryptographicException
  Message=不正确的项。

  Source=mscorlib
  StackTrace:
       在 System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
       在 System.Security.Cryptography.Utils._DecryptKey(SafeKeyHandle hPubKey, Byte[] key, Int32 dwFlags)
       在 System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
       在 Test.Program.Main(String[] args) 位置 F:\VS2010\GobalTestProject\Test\Program.cs:行号 385
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:

 

请高人指点,私钥解密失败,感激。

< >
分享
所有回答(1)
0

楼主解答了嘛,求解

Hsuwen | 园豆:10 (初学一级) | 2017-09-12 20:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册