private void rdCsrKeyDsa_CheckedChanged(object sender, EventArgs e) { if (rdCsrKeyEcdsa.Checked) { if (gbKeyBits.Text == "Key Size") { var newItems = CustomNamedCurves.Names .Cast <string>() .Where(x => CustomNamedCurves.GetOid(x) != null) .Select(x => x == "secp256r1" ? $"{x} (recommended)" : x) .ToArray(); var recommended = newItems.Single(x => x.EndsWith("(recommended)")); gbKeyBits.Text = "Curve"; cbCsKeyBits.Items.Clear(); cbCsKeyBits.Items.AddRange(newItems); cbCsKeyBits.SelectedItem = recommended; } } else { if (gbKeyBits.Text != "Key Size") { var newItems = new[] { "1024 bits", "2048 bits (recommended)", "4096 bits" }; gbKeyBits.Text = "Key Size"; cbCsKeyBits.Items.Clear(); cbCsKeyBits.Items.AddRange(newItems); cbCsKeyBits.SelectedItem = newItems[1]; } } }
public void TestMultiply() { ArrayList nameList = new ArrayList(); CollectionUtilities.AddRange(nameList, ECNamedCurveTable.Names); CollectionUtilities.AddRange(nameList, CustomNamedCurves.Names); string[] names = (string[])nameList.ToArray(typeof(string)); Array.Sort(names); ISet oids = new HashSet(); foreach (string name in names) { DerObjectIdentifier oid = ECNamedCurveTable.GetOid(name); if (oid == null) { oid = CustomNamedCurves.GetOid(name); } if (oid != null) { if (oids.Contains(oid)) { continue; } oids.Add(oid); } RandMult(name); } }
public IKey GenerateKey(int?keySize = null) { var generator = GeneratorUtilities.GetKeyPairGenerator("ECDSA"); var generatorParams = new ECKeyGenerationParameters( CustomNamedCurves.GetOid(curveName), new SecureRandom()); generator.Init(generatorParams); var keyPair = generator.GenerateKeyPair(); return(new AsymmetricCipherKey(Algorithm, keyPair)); }
public void InvalidCurve() { var provider = new KeyAlgorithmProvider(); var generator = GeneratorUtilities.GetKeyPairGenerator("ECDSA"); var generatorParams = new ECKeyGenerationParameters( CustomNamedCurves.GetOid("secp160r1"), new SecureRandom()); generator.Init(generatorParams); var keyPair = generator.GenerateKeyPair(); var der = PrivateKeyInfoFactory.CreatePrivateKeyInfo((keyPair.Private)).GetDerEncoded(); Assert.Throws <NotSupportedException>(() => provider.GetKey(der)); }
public void CreateProblemKey() { var userAgent = Util.GetUserAgent(); var certes = new CertesACMEProvider(Util.GetAppDataFolder() + "\\certes", userAgent); var keyFound = false; newKey = null; var attempts = 0; while (!keyFound) { var generator = GeneratorUtilities.GetKeyPairGenerator("ECDSA"); var generatorParams = new ECKeyGenerationParameters( CustomNamedCurves.GetOid("P-256"), new SecureRandom() ); generator.Init(generatorParams); var keyPair = generator.GenerateKeyPair(); var publicKey = (ECPublicKeyParameters)keyPair.Public; var xBytes = publicKey.Q.AffineXCoord.ToBigInteger().ToByteArrayUnsigned(); var yBytes = publicKey.Q.AffineYCoord.ToBigInteger().ToByteArrayUnsigned(); if (xBytes.Length != yBytes.Length) { System.Diagnostics.Debug.WriteLine($"Problem key found in {attempts} attempts"); keyFound = true; var pem = ""; using (var sr = new StringWriter()) { var pemWriter = new PemWriter(sr); pemWriter.WriteObject(keyPair); pem = sr.ToString(); } System.Diagnostics.Debug.WriteLine($"{pem}"); newKey = KeyFactory.FromPem(pem); } attempts++; } //certes.InitProvider().Wait(); }
// Based on RFCs 5480 and 5915, a named curve is used whenever possible. // Curve25519 is supported by BC, but not yet standardized and thus does not have an oid. public AsymmetricCipherKeyPair GenerateEcKeyPair(string curve) { if (curve.Equals("curve25519")) { return(GenerateCurve25519()); } DerObjectIdentifier curveOid = ECNamedCurveTable.GetOid(curve) ?? CustomNamedCurves.GetOid(curve); if (curveOid == null) { throw new ArgumentException($"Curve {curve} is not supported."); } var keyGenerationParameters = new ECKeyGenerationParameters(curveOid, secureRandom.Generator); var keyPairGenerator = new ECKeyPairGenerator(); keyPairGenerator.Init(keyGenerationParameters); return(keyPairGenerator.GenerateKeyPair()); }
// Based on RFCs 5480 and 5915, a named curve is used whenever possible. public IEcKey GetPublicKey(byte[] q, string curve) { ECPublicKeyParameters ecPublicKeyParameter; if (curve == "curve25519") { ecPublicKeyParameter = GetNonStandardCurve(q, curve); } else { DerObjectIdentifier curveOid = ECNamedCurveTable.GetOid(curve) ?? CustomNamedCurves.GetOid(curve); X9ECParameters curveParameters = CustomNamedCurves.GetByOid(curveOid) ?? ECNamedCurveTable.GetByOid(curveOid); ECPoint qPoint = curveParameters.Curve.DecodePoint(q); ecPublicKeyParameter = new ECPublicKeyParameters("EC", qPoint, curveOid); } byte[] publicKeyContent = GetPublicKey(ecPublicKeyParameter); int keyLength = GetKeyLength(ecPublicKeyParameter); string curveName = curveNameMapper.MapCurveToName(ecPublicKeyParameter.Parameters.Curve); return(new EcKey(publicKeyContent, AsymmetricKeyType.Public, keyLength, curveName)); }