Код: Выделить всё
public void ExportECKeyPairToPfx(string pfxPath, string password)
{
// Generate EC key pair
var curve = SecNamedCurves.GetByName("secp256r1"); // P-256 curve
var domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
var keyPairGenerator = new ECKeyPairGenerator();
var keyGenParams = new KeyGenerationParameters(new SecureRandom(), 256);
keyPairGenerator.Init(keyGenParams);
var keyPair = keyPairGenerator.GenerateKeyPair();
// Create self-signed certificate
var certificateGenerator = new X509V3CertificateGenerator();
var serialNumber = BigInteger.ProbablePrime(120, new SecureRandom());
var issuerDN = new X509Name("CN=Self Signed Certificate");
var subjectDN = new X509Name("CN=Self Signed Certificate");
certificateGenerator.SetSerialNumber(serialNumber);
certificateGenerator.SetIssuerDN(issuerDN);
certificateGenerator.SetSubjectDN(subjectDN);
certificateGenerator.SetNotBefore(DateTime.UtcNow.Date);
certificateGenerator.SetNotAfter(DateTime.UtcNow.Date.AddYears(1));
certificateGenerator.SetPublicKey(keyPair.Public);
// Sign the certificate with the private key
var signatureFactory = new Asn1SignatureFactory("SHA256WITHECDSA", keyPair.Private);
var certificate = certificateGenerator.Generate(signatureFactory);
// Create PFX (PKCS#12) store and add the certificate with private key
var store = new Pkcs12StoreBuilder().Build();
var certificateEntry = new X509CertificateEntry(certificate);
store.SetKeyEntry("EC Key", new AsymmetricKeyEntry(keyPair.Private), new[] { certificateEntry });
// Save to file
using (var fileStream = File.Create(pfxPath))
{
store.Save(fileStream, password.ToCharArray(), new SecureRandom());
}
}
Код: Выделить всё
public void ImportPfxToLocalMachine(string pfxPath, string password, StoreName storeName = StoreName.My)
{
try
{
// Read the certificate file
var pfxBytes = File.ReadAllBytes(pfxPath);
// Create X509Certificate2 object from PFX file
// Note: We use X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet
// to ensure the private key is stored in the machine store
var certificate = new X509Certificate2(
pfxBytes,
password,
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);
// Open the LocalMachine store
using (var store = new X509Store(storeName, StoreLocation.LocalMachine))
{
// Open store with ReadWrite rights
store.Open(OpenFlags.ReadWrite);
// Add certificate to store
store.Add(certificate);
Console.WriteLine($"Certificate with thumbprint {certificate.Thumbprint} successfully imported to LocalMachine\\{storeName}");
store.Close();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error importing certificate: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner Exception: {ex.InnerException.Message}");
}
throw;
}
}
Код: Выделить всё
public void GetCertificateByThumbprint(string thumbprint, StoreName storeName = StoreName.My)
{
using (var store = new X509Store(storeName, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
// Find certificate by thumbprint
var certificates = store.Certificates.Find(
X509FindType.FindByThumbprint,
thumbprint,
false); // false = don't validate certificate
store.Close();
if (certificates.Count == 0)
throw new Exception($"Certificate with thumbprint {thumbprint} not found.");
var certificate = certificates[0];
certificate.GetECDsaPrivateKey(); //ERROR HERE
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... th-ec-keys
Мобильная версия