Bad key CryptographicException when trying to Decrypt

允我心安 提交于 2019-12-10 22:56:03

问题


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

来源:https://stackoverflow.com/questions/12872657/bad-key-cryptographicexception-when-trying-to-decrypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!