From a Windows From desktop application, Visual Studio 2008, framework 3.5
I get errors trying to decrypt a message. I tried it different ways based on articles I found on the net, but I still have the same issues
Heres common code to both approaches:
UserCert =
X509Certificate2UI.SelectFromCollection(.....)RSACryptoServiceProvider
pk = (RSACryptoServiceProvider)UserCert.PrivateKey;....
UserName = "Some Text";
...
byte
[] dcn = Encrypt(UserName, pkpub);...
Try #1
string
ptst = UserCert.PrivateKey.ToXmlString(true); <----------------- throws Key not valid for use in specified statebyte
[] data = Encoding.UTF8.GetBytes(dataIn);byte[] decrypted;using (var rsaPublicPrivate = newRSACryptoServiceProvider()){
false);rsaPublicPrivate.FromXmlString(key);
decrypted = rsaPublicPrivate.Decrypt(data,
}
> mscorlib.dll!System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(int hr = -2146893813) Line 114 C#
mscorlib.dll!System.Security.Cryptography.RSACryptoServiceProvider.ExportParameters(bool includePrivateParameters) Line 211 C#
mscorlib.dll!System.Security.Cryptography.RSA.ToXmlString(bool includePrivateParameters = true) Line 138 C#
Try #2
RSACryptoServiceProvider
pk = (RSACryptoServiceProvider)UserCert.PrivateKey;byte
[] decrypted;decrypted = pk.Decrypt(data,
true); <-------------- throws Invalid type specified.
mscorlib.dll!System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(int hr = -2146893814) Line 114 C#
mscorlib.dll!System.Security.Cryptography.RSACryptoServiceProvider.KeySize.get() Line 143 + 0xd bytes C#
mscorlib.dll!System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(byte[] rgb = {byte[128]}, bool fOAEP = true) Line 340 + 0xa bytes C#