Org.BouncyCastle.Crypto.Generators.Gost3410KeyPairGenerator.GenerateKeyPair C# (CSharp) Метод

GenerateKeyPair() публичный Метод

public GenerateKeyPair ( ) : AsymmetricCipherKeyPair
Результат AsymmetricCipherKeyPair
		public AsymmetricCipherKeyPair GenerateKeyPair()
		{
			SecureRandom random = param.Random;
			Gost3410Parameters gost3410Params = param.Parameters;

			BigInteger q = gost3410Params.Q;
			BigInteger x;
			do
			{
				x = new BigInteger(256, random);
			}
			while (x.SignValue < 1 || x.CompareTo(q) >= 0);

			BigInteger p = gost3410Params.P;
			BigInteger a = gost3410Params.A;

			// calculate the public key.
			BigInteger y = a.ModPow(x, p);

			if (param.PublicKeyParamSet != null)
			{
				return new AsymmetricCipherKeyPair(
					new Gost3410PublicKeyParameters(y, param.PublicKeyParamSet),
					new Gost3410PrivateKeyParameters(x, param.PublicKeyParamSet));
			}

			return new AsymmetricCipherKeyPair(
				new Gost3410PublicKeyParameters(y, gost3410Params),
				new Gost3410PrivateKeyParameters(x, gost3410Params));
		}
	}

Usage Example

Пример #1
0
			public ITestResult Perform()
			{
				BigInteger r = new BigInteger("860d82c60e9502cd00c0e9e1f6563feafec304801974d745c5e02079946f729e",16);
				BigInteger s = new BigInteger("7ef49264ef022801aaa03033cd97915235fbab4c823ed936b0f360c22114688a",16);
				Gost3410ParametersGenerator pGen = new Gost3410ParametersGenerator();

				pGen.Init(1024, 2, init_random);

				Gost3410Parameters parameters = pGen.GenerateParameters();

				if (!pValue.Equals(parameters.P) || !qValue.Equals(parameters.Q))
				{
					return new SimpleTestResult(false, Name + ": p or q wrong");
				}

				Gost3410KeyPairGenerator Gost3410KeyGen = new Gost3410KeyPairGenerator();
				Gost3410KeyGenerationParameters genParam = new Gost3410KeyGenerationParameters(keyRandom, parameters);

				Gost3410KeyGen.Init(genParam);

				AsymmetricCipherKeyPair pair = Gost3410KeyGen.GenerateKeyPair();

				ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

				Gost3410Signer Gost3410 = new Gost3410Signer();

				Gost3410.Init(true, param);

				BigInteger[] sig = Gost3410.GenerateSignature(hashmessage);

				if (!r.Equals(sig[0]))
				{
					return new SimpleTestResult(false, Name
						+ ": r component wrong." + SimpleTest.NewLine
						+ " expecting: " + r.ToString(16) + SimpleTest.NewLine
						+ " got      : " + sig[0].ToString(16));
				}

				if (!s.Equals(sig[1]))
				{
					return new SimpleTestResult(false, Name
						+ ": s component wrong." + SimpleTest.NewLine
						+ " expecting: " + s.ToString(16) + SimpleTest.NewLine
						+ " got      : " + sig[1].ToString(16));
				}

				Gost3410.Init(false, pair.Public);

				if (Gost3410.VerifySignature(hashmessage, sig[0], sig[1]))
				{
					return new SimpleTestResult(true, Name + ": Okay");
				}
				else
				{
					return new SimpleTestResult(false, Name + ": verification fails");
				}
			}
All Usage Examples Of Org.BouncyCastle.Crypto.Generators.Gost3410KeyPairGenerator::GenerateKeyPair
Gost3410KeyPairGenerator