I create the following files using the commands:
makecert -n "CN=TEST_CER" -sv "Test.pvk" -r -sky echange -pe test.cer
pvk2pfx -pvk Eltapk.pvk -spc test.cer -pfx test.pfx -pi PVKPASS -po PFXPASS -f
Then i use the test.pfx in the following test application :
X509Certificate2 cer2 = new X509Certificate2("test.pfx", "PFXPASS");
RSACryptoServiceProvider PrivateKey = cer2.PrivateKey as RSACryptoServiceProvider;
RSACryptoServiceProvider PublicKey = cer2.PublicKey.Key as RSACryptoServiceProvider;
string test = "test123";
byte[] testb = Encoding.UTF8.GetBytes(test);
testb = PublicKey.Encrypt(testb, true);
testb = PrivateKey.Decrypt(testb, true);
test = Encoding.UTF8.GetString(testb);
And I get CryptographicException: Bad Key.
I do use -sky exchange and -pe.
What I am doing wrong?
P.S. PrivateKey.Encrypt also fails with the same Exception