gpt4 book ai didi

c# - 获取 PrivateKey null X509Certificate2 c#

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:38 25 4
gpt4 key购买 nike

It is a console application and I'm using .net framework 4. When I try to get the private key from the certificate I get it as null any idea why is this happening?

string text = "some text"
SHA1 sha1 = SHA1CryptoServiceProvider.Create();
Byte[] textToBytes = //ASCIIEncoding.Default.GetBytes(texto);
UTF8Encoding.UTF8.GetBytes(text);
Byte[] hash = sha1.ComputeHash(textToBytes);
//Path to the certificate file.
string sFile = @"C:\myCer.cer";
//Get the bytes array.
byte[] byteCer = File.ReadAllBytes(sFile);
//Create the certificate with bytes and the password.
X509Certificate2 myCert2 = new X509Certificate2(byteCer, "Password", X509KeyStorageFlags.MachineKeySet);

byte[] Encrypted = rsa1.Encrypt(hash, true);
string cypherText = Convert.ToBase64String(Encrypted);
RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)myCert2.PrivateKey;//I get the privateKey as null.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("SHA1");
byte[] SignedHashValue = RSAFormatter.CreateSignature(hash);

最佳答案

通常扩展名为“.cer”的文件只是证书,它是公钥和一些相关的元数据。 PFX/PKCS#12 文件也可以包含私钥。

您可以检查 myCert2.HasPrivateKey 以确定是否为证书加载了任何私钥。由于 myCert2.PrivateKey 应该仅在 HasPrivateKey 为 false 时返回 null,我很确定您会发现系统根本不知道您的私钥证书。

关于c# - 获取 PrivateKey null X509Certificate2 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com