GetSeed() public méthode

public GetSeed ( ) : byte[]
Résultat byte[]
		private void doTestParams(
			byte[]	ecParameterEncoded,
			bool	compress)
		{
//			string keyStorePass = "******";
			Asn1Sequence seq = (Asn1Sequence) Asn1Object.FromByteArray(ecParameterEncoded);
			X9ECParameters x9 = new X9ECParameters(seq);
			AsymmetricCipherKeyPair kp = null;
			bool success = false;
			while (!success)
			{
				IAsymmetricCipherKeyPairGenerator kpg = GeneratorUtilities.GetKeyPairGenerator("ECDSA");
//				kpg.Init(new ECParameterSpec(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed()));
				ECDomainParameters ecParams = new ECDomainParameters(
					x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
				kpg.Init(new ECKeyGenerationParameters(ecParams, new SecureRandom()));
				kp = kpg.GenerateKeyPair();
				// The very old Problem... we need a certificate chain to
				// save a private key...
				ECPublicKeyParameters pubKey = (ECPublicKeyParameters) kp.Public;

				if (!compress)
				{
					//pubKey.setPointFormat("UNCOMPRESSED");
					pubKey = SetPublicUncompressed(pubKey);
				}

				byte[] x = pubKey.Q.AffineXCoord.ToBigInteger().ToByteArrayUnsigned();
				byte[] y = pubKey.Q.AffineYCoord.ToBigInteger().ToByteArrayUnsigned();
				if (x.Length == y.Length)
				{
					success = true;
				}
			}

			// The very old Problem... we need a certificate chain to
			// save a private key...

			X509CertificateEntry[] chain = new X509CertificateEntry[] {
				new X509CertificateEntry(GenerateSelfSignedSoftECCert(kp, compress))
			};

//			KeyStore keyStore = KeyStore.getInstance("BKS");
//			keyStore.load(null, keyStorePass.ToCharArray());
			Pkcs12Store keyStore = new Pkcs12StoreBuilder().Build();

			keyStore.SetCertificateEntry("ECCert", chain[0]);

			ECPrivateKeyParameters privateECKey = (ECPrivateKeyParameters) kp.Private;
			keyStore.SetKeyEntry("ECPrivKey", new AsymmetricKeyEntry(privateECKey), chain);

			// Test ec sign / verify
			ECPublicKeyParameters pub = (ECPublicKeyParameters) kp.Public;
//			string oldPrivateKey = new string(Hex.encode(privateECKey.getEncoded()));
			byte[] oldPrivateKeyBytes = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateECKey).GetDerEncoded();
			string oldPrivateKey = Hex.ToHexString(oldPrivateKeyBytes);
//			string oldPublicKey = new string(Hex.encode(pub.getEncoded()));
			byte[] oldPublicKeyBytes = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub).GetDerEncoded();
			string oldPublicKey = Hex.ToHexString(oldPublicKeyBytes);
			ECPrivateKeyParameters newKey = (ECPrivateKeyParameters)
				keyStore.GetKey("ECPrivKey").Key;
			ECPublicKeyParameters newPubKey = (ECPublicKeyParameters)
				keyStore.GetCertificate("ECCert").Certificate.GetPublicKey();

			if (!compress)
			{
				// TODO Private key compression?
				//newKey.setPointFormat("UNCOMPRESSED");
				//newPubKey.setPointFormat("UNCOMPRESSED");
				newPubKey = SetPublicUncompressed(newPubKey);
			}

//			string newPrivateKey = new string(Hex.encode(newKey.getEncoded()));
			byte[] newPrivateKeyBytes = PrivateKeyInfoFactory.CreatePrivateKeyInfo(newKey).GetDerEncoded();
			string newPrivateKey = Hex.ToHexString(newPrivateKeyBytes);
//			string newPublicKey = new string(Hex.encode(newPubKey.getEncoded()));
			byte[] newPublicKeyBytes = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(newPubKey).GetDerEncoded();
			string newPublicKey = Hex.ToHexString(newPublicKeyBytes);

			if (!oldPrivateKey.Equals(newPrivateKey))
//			if (!privateECKey.Equals(newKey))
			{
				Fail("failed private key comparison");
			}

			if (!oldPublicKey.Equals(newPublicKey))
//			if (!pub.Equals(newPubKey))
			{
				Fail("failed public key comparison");
			}
		}
        public static AsymmetricKeyParameter CreateKey(
			PrivateKeyInfo keyInfo)
        {
            AlgorithmIdentifier algID = keyInfo.AlgorithmID;
            if (algID.ObjectID.Equals(PkcsObjectIdentifiers.RsaEncryption))
            {
                RsaPrivateKeyStructure keyStructure = new RsaPrivateKeyStructure(
                    (Asn1Sequence)keyInfo.PrivateKey);
                return (new RsaPrivateCrtKeyParameters(
                    keyStructure.Modulus,
                    keyStructure.PublicExponent,
                    keyStructure.PrivateExponent,
                    keyStructure.Prime1,
                    keyStructure.Prime2,
                    keyStructure.Exponent1,
                    keyStructure.Exponent2,
                    keyStructure.Coefficient));
            }
            else if (algID.ObjectID.Equals(PkcsObjectIdentifiers.DhKeyAgreement))
            {
                DHParameter para = new DHParameter((Asn1Sequence)algID.Parameters);
                DerInteger derX = (DerInteger)keyInfo.PrivateKey;
                return new DHPrivateKeyParameters(derX.Value, new DHParameters(para.P, para.G));
            }
            else if (algID.ObjectID.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                ElGamalParameter  para = new ElGamalParameter((Asn1Sequence)algID.Parameters);
                DerInteger derX = (DerInteger)keyInfo.PrivateKey;
                return new ElGamalPrivateKeyParameters(derX.Value, new ElGamalParameters(para.P, para.G));
            }
            else if (algID.ObjectID.Equals(X9ObjectIdentifiers.IdDsa))
            {
                DsaParameter para = DsaParameter.GetInstance(algID.Parameters);
                DerInteger derX = (DerInteger) keyInfo.PrivateKey;
                return new DsaPrivateKeyParameters(derX.Value, new DsaParameters(para.P, para.Q, para.G));
            }
            else if (algID.ObjectID.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                X962Parameters para = new X962Parameters((Asn1Object)algID.Parameters);
                ECDomainParameters dParams = null;

                if (para.IsNamedCurve)
                {
                    DerObjectIdentifier oid = (DerObjectIdentifier) para.Parameters;
                    X9ECParameters ecP = X962NamedCurves.GetByOid(oid);

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

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

                    dParams = new ECDomainParameters(
                        ecP.Curve,
                        ecP.G,
                        ecP.N,
                        ecP.H,
                        ecP.GetSeed());
                }
                else
                {
                    X9ECParameters ecP = new X9ECParameters(
                        (Asn1Sequence) para.Parameters);
                    dParams = new ECDomainParameters(
                        ecP.Curve,
                        ecP.G,
                        ecP.N,
                        ecP.H,
                        ecP.GetSeed());
                }

                ECPrivateKeyStructure ec = new ECPrivateKeyStructure((Asn1Sequence)keyInfo.PrivateKey);

                return new ECPrivateKeyParameters(ec.GetKey(), dParams);
            }
            else if (algID.ObjectID.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                throw new NotImplementedException();
            }
            else if (algID.ObjectID.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence) algID.Parameters);

                DerOctetString derX = (DerOctetString) keyInfo.PrivateKey;
                byte[] keyEnc = derX.GetOctets();
                byte[] keyBytes = new byte[keyEnc.Length];

                for (int i = 0; i != keyEnc.Length; i++)
                {
                    keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // was little endian
                }

                BigInteger x = new BigInteger(1, keyBytes);

                return new Gost3410PrivateKeyParameters(x, algParams.PublicKeyParamSet);
            }
            else
            {
                throw new SecurityUtilityException("algorithm identifier in key not recognised");
            }
        }
		private bool VerifyEcDsa(IDigest digest, X9ECParameters curveParameter, byte[] buffer, int length, byte[] signature)
		{
			int digestSize = digest.GetDigestSize();

			ECDomainParameters dParams = new ECDomainParameters(
				curveParameter.Curve,
				curveParameter.G,
				curveParameter.N,
				curveParameter.H,
				curveParameter.GetSeed());

			ECPoint q = dParams.Curve.CreatePoint(new BigInteger(1, PublicKey, 0, digestSize), new BigInteger(1, PublicKey, digestSize, digestSize), false);

			ECPublicKeyParameters parameters = new ECPublicKeyParameters(q, dParams);

			var signer = new ECDsaSigner();
			signer.Init(false, parameters);

			digest.BlockUpdate(buffer, 0, length);
			byte[] hash = new byte[digest.GetDigestSize()];
			digest.DoFinal(hash, 0);

			return signer.VerifySignature(hash, new BigInteger(1, signature, 0, digestSize), new BigInteger(1, signature, digestSize, digestSize));
		}
        public static AsymmetricKeyParameter CreateKey(
			SubjectPublicKeyInfo keyInfo)
        {
            AlgorithmIdentifier algID = keyInfo.AlgorithmID;

            if (algID.ObjectID.Equals(PkcsObjectIdentifiers.RsaEncryption)
                || algID.ObjectID.Equals(X509ObjectIdentifiers.IdEARsa))
            {
                RsaPublicKeyStructure pubKey = RsaPublicKeyStructure.GetInstance(keyInfo.GetPublicKey());

                return new RsaKeyParameters(false, pubKey.Modulus, pubKey.PublicExponent);
            }
            else if (algID.ObjectID.Equals(PkcsObjectIdentifiers.DhKeyAgreement)
                || algID.ObjectID.Equals(X9ObjectIdentifiers.DHPublicNumber))
            {
                DHParameter para = new DHParameter((Asn1Sequence)keyInfo.AlgorithmID.Parameters);
                DerInteger derY = (DerInteger)keyInfo.GetPublicKey();

                return new DHPublicKeyParameters(derY.Value, new DHParameters(para.P, para.G));
            }
            else if (algID.ObjectID.Equals(OiwObjectIdentifiers.ElGamalAlgorithm))
            {
                ElGamalParameter para = new ElGamalParameter((Asn1Sequence)keyInfo.AlgorithmID.Parameters);
                DerInteger derY = (DerInteger)keyInfo.GetPublicKey();

                return new ElGamalPublicKeyParameters(derY.Value, new ElGamalParameters(para.P, para.G));
            }
            else if (algID.ObjectID.Equals(X9ObjectIdentifiers.IdDsa)
                || algID.ObjectID.Equals(OiwObjectIdentifiers.DsaWithSha1))
            {
                DsaParameter para = DsaParameter.GetInstance(keyInfo.AlgorithmID.Parameters);
                DerInteger derY = (DerInteger)keyInfo.GetPublicKey();

                return new DsaPublicKeyParameters(derY.Value, new DsaParameters(para.P, para.Q, para.G));
            }
            else if (algID.ObjectID.Equals(X9ObjectIdentifiers.IdECPublicKey))
            {
                X962Parameters para = new X962Parameters((Asn1Object)keyInfo.AlgorithmID.Parameters);
                ECDomainParameters dParams = null;

                if (para.IsNamedCurve)
                {
                    DerObjectIdentifier oid = (DerObjectIdentifier)para.Parameters;
                    X9ECParameters ecP = X962NamedCurves.GetByOid(oid);

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

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

                    dParams = new ECDomainParameters(
                        ecP.Curve,
                        ecP.G,
                        ecP.N,
                        ecP.H,
                        ecP.GetSeed());
                }
                else
                {
                    X9ECParameters ecP = new X9ECParameters((Asn1Sequence)para.Parameters.ToAsn1Object());

                    dParams = new ECDomainParameters(
                        ecP.Curve,
                        ecP.G,
                        ecP.N,
                        ecP.H,
                        ecP.GetSeed());
                }

                DerBitString bits = keyInfo.PublicKeyData;
                byte[] data = bits.GetBytes();
                Asn1OctetString key = new DerOctetString(data);

                X9ECPoint derQ = new X9ECPoint(dParams.Curve, key);

                return new ECPublicKeyParameters(derQ.Point, dParams);
            }
            else if (algID.ObjectID.Equals(CryptoProObjectIdentifiers.GostR3410x2001))
            {
                Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence) algID.Parameters);

                Asn1OctetString key;
                try
                {
                    key = (Asn1OctetString) keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                byte[] keyEnc = key.GetOctets();
                byte[] x = new byte[32];
                byte[] y = new byte[32];

                for (int i = 0; i != y.Length; i++)
                {
                    x[i] = keyEnc[32 - 1 - i];
                }

                for (int i = 0; i != x.Length; i++)
                {
                    y[i] = keyEnc[64 - 1 - i];
                }

                ECDomainParameters ecP = ECGost3410NamedCurves.GetByOid(gostParams.PublicKeyParamSet);

                if (ecP == null)
                    return null;

                ECCurve curve = ecP.Curve;
                ECPoint q;

                if (curve is FpCurve)
                {
                    FpCurve curveFp = (FpCurve) curve;
                    q = new FpPoint(
                        curveFp,
                        new FpFieldElement(curveFp.Q, new BigInteger(1, x)),
                        new FpFieldElement(curveFp.Q, new BigInteger(1, y)));
                }
                else
                {
                    F2mCurve curveF2m = (F2mCurve) curve;
                    q = new F2mPoint(
                        curveF2m,
                        new F2mFieldElement(curveF2m.M, curveF2m.K1, curveF2m.K2, curveF2m.K3, new BigInteger(1, x)),
                        new F2mFieldElement(curveF2m.M, curveF2m.K1, curveF2m.K2, curveF2m.K3, new BigInteger(1, y)),
                        false);
                }

                return new ECPublicKeyParameters(q, gostParams.PublicKeyParamSet);
            }
            else if (algID.ObjectID.Equals(CryptoProObjectIdentifiers.GostR3410x94))
            {
                Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters(
                    (Asn1Sequence) algID.Parameters);

                DerOctetString derY;
                try
                {
                    derY = (DerOctetString) keyInfo.GetPublicKey();
                }
                catch (IOException)
                {
                    throw new ArgumentException("invalid info structure in GOST3410 public key");
                }

                byte[] keyEnc = derY.GetOctets();
                byte[] keyBytes = new byte[keyEnc.Length];

                for (int i = 0; i != keyEnc.Length; i++)
                {
                    keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // was little endian
                }

                BigInteger y = new BigInteger(1, keyBytes);

                return new Gost3410PublicKeyParameters(y, algParams.PublicKeyParamSet);
            }
            else
            {
                throw new SecurityUtilityException("algorithm identifier in key not recognised: " + algID.ObjectID);
            }
        }
Exemple #5
0
 public ScriptEngine()
 {
     this.curve = SecNamedCurves.GetByName("secp256k1");
     this.domainParameters = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
 }