} // End Sub ExportKeyPair // https://stackoverflow.com/questions/22008337/generating-keypair-using-bouncy-castle // https://stackoverflow.com/questions/14052485/converting-a-public-key-in-subjectpublickeyinfo-format-to-rsapublickey-format-ja // https://stackoverflow.com/questions/10963756/get-der-encoded-public-key // http://www.programcreek.com/java-api-examples/index.php?api=org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory public static void CerKeyInfo(Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair) { Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo pkInfo = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private); string privateKey = System.Convert.ToBase64String(pkInfo.GetDerEncoded()); // and following for public: Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo info = Org.BouncyCastle.X509.SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public); string publicKey = System.Convert.ToBase64String(info.GetDerEncoded()); System.Console.WriteLine(privateKey); System.Console.WriteLine(publicKey); } // End Sub CerKeyInfo
public static System.Security.Cryptography.ECDsa GetMsEcdsaProvider() { string namedCurve = "prime256v1"; Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator pGen = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator(); Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters genParam = new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters( Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetOid(namedCurve), new Org.BouncyCastle.Security.SecureRandom() ); pGen.Init(genParam); Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = pGen.GenerateKeyPair(); Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pub = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)keyPair.Public; Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters priv = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)keyPair.Private; System.Security.Cryptography.ECParameters pars = new ECParameters(); //string str = priv.Parameters.Curve.ToString(); //System.Console.WriteLine(str); //pars.Curve = new ECCurve(); //pars.D = priv.D.ToByteArray(); //pars.Q = new System.Security.Cryptography.ECPoint(); //pars.Q.X = pub.Q.X.GetEncoded(); //pars.Q.Y = pub.Q.Y.GetEncoded(); //System.Security.Cryptography.ECDsa.Create(pars); // The CngKey can be created by importing the key using the Der encoded bytes: Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo bcKeyInfo = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private) ; byte[] pkcs8Blob = bcKeyInfo.GetDerEncoded(); CngKey importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob); return(new System.Security.Cryptography.ECDsaCng(importedKey)); }
public static void SaveKey(AsymmetricKeyParameter key, string url) { Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo privateKeyInfo = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(key); Org.BouncyCastle.Utilities.IO.Pem.PemObject pemObj = new Org.BouncyCastle.Utilities.IO.Pem.PemObject("PRIVATE KEY", privateKeyInfo.ToAsn1Object().GetEncoded()); TextWriter textkey = new StringWriter(); PemWriter pemkey = new PemWriter(textkey); pemkey.WriteObject(pemObj); pemkey.Writer.Flush(); string pri = textkey.ToString(); pemkey.WriteObject(key); pemkey.Writer.Flush(); byte[] priInfoByte = System.Text.Encoding.UTF8.GetBytes(textkey.ToString()); FileStream fs = new FileStream(url, FileMode.Create, FileAccess.Write); fs.Write(priInfoByte, 0, priInfoByte.Length); fs.Close(); }
// System.Security.Cryptography.X509Certificates.X509Certificate2.Import (string fileName); // https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.import?view=netframework-4.7.2 // https://gist.github.com/yutopio/a217a4af63cf6bcf0a530c14c074cf8f // https://gist.githubusercontent.com/yutopio/a217a4af63cf6bcf0a530c14c074cf8f/raw/42b2f8cb27f6d22b7e22d65da5bbd0f1ce9b2fff/cert.cs // https://stackoverflow.com/questions/44755155/store-pkcs12-container-pfx-with-bouncycastle // https://github.com/Worlaf/RSADemo/blob/328692e28e48db92340d55563480c8724d916384/RSADemo_WinForms/frmRsaDemo.cs public static void Create( string fileName , Org.BouncyCastle.X509.X509Certificate certificate , Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey , string password = "") { // create certificate entry Org.BouncyCastle.Pkcs.X509CertificateEntry certEntry = new Org.BouncyCastle.Pkcs.X509CertificateEntry(certificate); Org.BouncyCastle.Asn1.X509.X509Name name = new Org.BouncyCastle.Asn1.X509.X509Name(certificate.SubjectDN.ToString()); string friendlyName = name .GetValueList(Org.BouncyCastle.Asn1.X509.X509Name.O) .OfType <string>() .FirstOrDefault(); if (System.StringComparer.InvariantCultureIgnoreCase.Equals("Skynet Earth Inc.", friendlyName)) { friendlyName = "Skynet Certification Authority"; } // get bytes of private key. Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo keyInfo = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey); //byte[] keyBytes = keyInfo.ToAsn1Object().GetEncoded(); Org.BouncyCastle.Pkcs.Pkcs12StoreBuilder builder = new Org.BouncyCastle.Pkcs.Pkcs12StoreBuilder(); builder.SetUseDerEncoding(true); Org.BouncyCastle.Pkcs.Pkcs12Store store = builder.Build(); store.SetCertificateEntry(friendlyName, certEntry); // create store entry store.SetKeyEntry( //keyFriendlyName friendlyName , new Org.BouncyCastle.Pkcs.AsymmetricKeyEntry(privateKey) , new Org.BouncyCastle.Pkcs.X509CertificateEntry[] { certEntry } ); byte[] pfxBytes = null; using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { // Cert is contained in store // null: no password, "": an empty passwords // note: Linux needs empty password on null... store.Save(stream, password == null ? "".ToCharArray() : password.ToCharArray(), new Org.BouncyCastle.Security.SecureRandom()); // stream.Position = 0; pfxBytes = stream.ToArray(); } // End Using stream #if WITH_MS_PFX WithMsPfx(pfxBytes, fileName, password); #else byte[] result = Org.BouncyCastle.Pkcs.Pkcs12Utilities.ConvertToDefiniteLength(pfxBytes); // this.StoreCertificate(System.Convert.ToBase64String(result)); using (System.IO.BinaryWriter writer = new System.IO.BinaryWriter(System.IO.File.Open(fileName, System.IO.FileMode.Create))) { writer.Write(result); } // End Using writer #endif } // End Sub Create