gpt4 book ai didi

c# - 如何在不使用过时的 BouncyCaSTLe 1.7.0 代码的情况下生成自签名证书?

转载 作者:太空狗 更新时间:2023-10-29 18:15:55 26 4
gpt4 key购买 nike

我有以下代码生成一个很好的自签名证书,效果很好,但我想更新到最新的 BouncyCaSTLe (1.8.1.0),我收到有关过时用法的警告:

var persistedCertificateFilename = "ClientCertificate.pfx";
if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["PersistedCertificateFilename"])) { persistedCertificateFilename = ConfigurationManager.AppSettings["PersistedCertificateFilename"].Trim(); }
if (persistCertificateToDisk)
{
if (File.Exists(persistedCertificateFilename))
{
var certBytes = File.ReadAllBytes(persistedCertificateFilename);
this.clientCertificate = new X509Certificate2(certBytes, (string) null, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
}
}

if (this.clientCertificate == null)
{
// Initialize the new secure keys
KeyGenerator keyGenerator = KeyGenerator.Create();
KeyPair keyPair = keyGenerator.GenerateKeyPair();
this.privateKey = keyPair.ToEncryptedPrivateKeyString(privateKeySecret);
this.publicKey = keyPair.ToPublicKeyString();

// Client certificate permissions
var certificatePermissions = new ArrayList()
{
KeyPurposeID.IdKPCodeSigning,
KeyPurposeID.IdKPServerAuth,
KeyPurposeID.IdKPTimeStamping,
KeyPurposeID.IdKPOcspSigning,
KeyPurposeID.IdKPClientAuth
};

// Initialize the certificate generation
var certificateGenerator = new X509V3CertificateGenerator();
BigInteger serialNo = BigInteger.ProbablePrime(128, new Random());
certificateGenerator.SetSerialNumber(serialNo);
certificateGenerator.SetSubjectDN(GetLicenseeDN());
certificateGenerator.SetIssuerDN(GetLicencerDN());
certificateGenerator.SetNotAfter(DateTime.Now.AddYears(100));
certificateGenerator.SetNotBefore(DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0)));
//ISignatureFactory signatureFactory = new Asn1SignatureFactory("SHA512WITHRSA", keyPair.PrivateKey); // ??
certificateGenerator.SetSignatureAlgorithm("SHA512withRSA");
certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage, false, new ExtendedKeyUsage(certificatePermissions));
var subjectKeyIdentifier = new SubjectKeyIdentifier(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.PublicKey));
certificateGenerator.AddExtension(X509Extensions.SubjectKeyIdentifier.Id, false, subjectKeyIdentifier);
certificateGenerator.SetPublicKey(keyPair.PublicKey);
var result = certificateGenerator.Generate(keyPair.PrivateKey);
var secure = new SecureString();
foreach (char c in privateKeySecret)
{
secure.AppendChar(c);
}

X509KeyStorageFlags flags = X509KeyStorageFlags.MachineKeySet;
if (persistCertificateToDisk) { flags |= X509KeyStorageFlags.Exportable; flags |= X509KeyStorageFlags.PersistKeySet; }
this.clientCertificate = new X509Certificate2(Org.BouncyCastle.Security.DotNetUtilities.ToX509Certificate(result).Export(X509ContentType.Cert), secure, flags);

// This section allows us to use this certificate on Azure (no file access required)
CspParameters cspParams;
const int PROVIDER_RSA_FULL = 1;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = new Guid().ToString();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
var rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);
cspParams.CryptoKeySecurity = new CryptoKeySecurity();
cspParams.CryptoKeySecurity.SetAccessRule(rule);

// Set the private key
var tempRcsp = (RSACryptoServiceProvider) Org.BouncyCastle.Security.DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters) keyPair.PrivateKey);
var rcsp = new RSACryptoServiceProvider(cspParams);
rcsp.ImportCspBlob(tempRcsp.ExportCspBlob(true));
this.clientCertificate.PrivateKey = rcsp;

if (persistCertificateToDisk)
{
if (!File.Exists(persistedCertificateFilename))
{
File.WriteAllBytes(persistedCertificateFilename, this.clientCertificate.Export(X509ContentType.Pkcs12, (string) null));
}
}
}

具体来说,警告是:

'X509V3CertificateGenerator.SetSignatureAlgorithm(string)' is obsolete: 'Not needed if Generate used with an ISignatureFactory'

'X509V3CertificateGenerator.Generate(AsymmetricKeyParameter)' is obsolete: 'Use Generate with an ISignatureFactory'

所以,我的问题是:

  1. 我需要担心这些警告吗?
  2. 如果是这样,哪些线路会发生变化?
  3. 如果我更新此代码,是否有性能优势?

注意:如果有人好奇,我将其保存到磁盘的原因是每次实例化客户端时这段代码都会创建一个证书,由于最小 key 大小为 2048 和性能,这尤其苛刻1.7.0 的。

最佳答案

我也为此苦苦挣扎了一段时间。我终于有了解决方案。让我们来看看其中一个错误:

'X509V3CertificateGenerator.Generate(AsymmetricKeyParameter)' is obsolete: 'Use Generate with an ISignatureFactory'

您基本上是这样使用(我在做同样的事情)Generate 方法:

var certificate = certificateGenerator.Generate(issuerCertificate.PrivateKey, random);

其中 certificateGenerator 是类 CertificateContainer 的实例错误提示:'Use Generate with an ISignatureFactory'
因此,为此让我们首先为 ISignatureFactory 创建一个实例。

ISignatureFactory signatureFactory = new Asn1SignatureFactory("SHA512WITHRSA", issuerKeyPair.Private, random);

为了在此之前正常工作,您还应该声明以下内容:

var randomGenerator = new CryptoApiRandomGenerator();
var random = new SecureRandom(randomGenerator);
AsymmetricCipherKeyPair subjectKeyPair = default(AsymmetricCipherKeyPair);
var keyGenerationParameters = new KeyGenerationParameters(random, keyStrength);

keyPairGenerator.Init(keyGenerationParameters);
subjectKeyPair = keyPairGenerator.GenerateKeyPair();
AsymmetricCipherKeyPair issuerKeyPair = subjectKeyPair;

现在在这些更改之后,将方法 Generate 更改为:

var certificate = certificateGenerator.Generate(issuerCertificate.PrivateKey, random);

到:

var certificate = certificateGenerator.Generate(signatureFactory);

希望对你有帮助。

关于c# - 如何在不使用过时的 BouncyCaSTLe 1.7.0 代码的情况下生成自签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942094/

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