ECKeyPairGenerator.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Generators » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Generators » ECKeyPairGenerator.cs
using System;
using System.Globalization;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;

namespace Org.BouncyCastle.Crypto.Generators{
    public class ECKeyPairGenerator
    : IAsymmetricCipherKeyPairGenerator
    {
    private readonly string algorithm;

    private ECDomainParameters parameters;
    private DerObjectIdentifier publicKeyParamSet;
        private SecureRandom random;

    public ECKeyPairGenerator()
      : this("EC")
    {
    }

    public ECKeyPairGenerator(
      string algorithm)
    {
      if (algorithm == null)
        throw new ArgumentNullException("algorithm");

      this.algorithm = VerifyAlgorithmName(algorithm);
    }

    public void Init(
            KeyGenerationParameters parameters)
        {
      if (parameters is ECKeyGenerationParameters)
      {
        ECKeyGenerationParameters ecP = (ECKeyGenerationParameters) parameters;

        this.publicKeyParamSet = ecP.PublicKeyParamSet;
        this.parameters = ecP.DomainParameters;
      }
      else
      {
        DerObjectIdentifier oid;
        switch (parameters.Strength)
        {
          case 192:
            oid = X9ObjectIdentifiers.Prime192v1;
            break;
          case 224:
            oid = SecObjectIdentifiers.SecP224r1;
            break;
          case 239:
            oid = X9ObjectIdentifiers.Prime239v1;
            break;
          case 256:
            oid = X9ObjectIdentifiers.Prime256v1;
            break;
          case 384:
            oid = SecObjectIdentifiers.SecP384r1;
            break;
          case 521:
            oid = SecObjectIdentifiers.SecP521r1;
            break;
          default:
            throw new InvalidParameterException("unknown key size.");
        }

        X9ECParameters ecps = FindECCurveByOid(oid);

        this.parameters = new ECDomainParameters(
          ecps.Curve, ecps.G, ecps.N, ecps.H, ecps.GetSeed());
      }

      this.random = parameters.Random;
    }

    /**
         * Given the domain parameters this routine Generates an EC key
         * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
         */
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            BigInteger n = parameters.N;
            BigInteger d;

            do
            {
                d = new BigInteger(n.BitLength, random);
            }
            while (d.SignValue == 0 || (d.CompareTo(n) >= 0));

            ECPoint q = parameters.G.Multiply(d);

      if (publicKeyParamSet != null)
      {
        return new AsymmetricCipherKeyPair(
          new ECPublicKeyParameters(algorithm, q, publicKeyParamSet),
          new ECPrivateKeyParameters(algorithm, d, publicKeyParamSet));
      }

      return new AsymmetricCipherKeyPair(
        new ECPublicKeyParameters(algorithm, q, parameters),
        new ECPrivateKeyParameters(algorithm, d, parameters));
    }

    private string VerifyAlgorithmName(
      string algorithm)
    {
      string upper = algorithm.ToUpper(CultureInfo.InvariantCulture);

      switch (upper)
      {
        case "EC":
        case "ECDSA":
        case "ECDH":
        case "ECDHC":
        case "ECGOST3410":
        case "ECMQV":
          break;
        default:
          throw new ArgumentException("unrecognised algorithm: " + algorithm, "algorithm");
      }

      return upper;
    }

    internal static X9ECParameters FindECCurveByOid(DerObjectIdentifier oid)
    {
      // TODO ECGost3410NamedCurves support (returns ECDomainParameters though)

      X9ECParameters ecP = X962NamedCurves.GetByOid(oid);

      if (ecP == null)
      {
        ecP = SecNamedCurves.GetByOid(oid);

        if (ecP == null)
        {
          ecP = NistNamedCurves.GetByOid(oid);

          if (ecP == null)
          {
            ecP = TeleTrusTNamedCurves.GetByOid(oid);
          }
        }
      }

      return ecP;
    }

    internal static ECPublicKeyParameters GetCorrespondingPublicKey(
      ECPrivateKeyParameters privKey)
    {
      ECDomainParameters parameters = privKey.Parameters;
      ECPoint q = parameters.G.Multiply(privKey.D);

      if (privKey.PublicKeyParamSet != null)
      {
        return new ECPublicKeyParameters(privKey.AlgorithmName, q, privKey.PublicKeyParamSet);
      }

      return new ECPublicKeyParameters(privKey.AlgorithmName, q, parameters);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.