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:
请高人指点,私钥解密失败,感激。
楼主解答了嘛,求解