2

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

Stavros Zotalis
  • 718
  • 4
  • 24
  • 2
    This code ought to work. Please look for typos: `sky echange` and I think it should be `-pvk Test.pvk`. I can't tell if they're copy/paste errors or originals. Did MakeCert respond with 'Succeeded' ? – H H Oct 13 '12 at 13:08
  • Lol, I am ashamed it was typo and it took me over 2 hours... sky eXchange... But the makecert responded that the execution was successful. It obviously ignores any wrong flags... – Stavros Zotalis Oct 13 '12 at 13:22
  • That means it works now? – H H Oct 13 '12 at 13:25
  • @HenkHolterman could you make that an answer and Blim, could you accept it so that it won't stay open? – Maarten Bodewes Oct 13 '12 at 20:00
  • Maybe Blim can investigate the (lack of) error messages from MakeCert a little further and post a self-answer. – H H Oct 14 '12 at 09:04

0 Answers0