Example #1
0
        public virtual void Init(bool forEncryption, ICipherParameters param)
        {
            this.mForEncryption = forEncryption;

            if (forEncryption)
            {
                ParametersWithRandom rParam = (ParametersWithRandom)param;

                mECKey    = (ECKeyParameters)rParam.Parameters;
                mECParams = mECKey.Parameters;

                ECPoint s = ((ECPublicKeyParameters)mECKey).Q.Multiply(mECParams.H);
                if (s.IsInfinity)
                {
                    throw new ArgumentException("invalid key: [h]Q at infinity");
                }

                mRandom = rParam.Random;
            }
            else
            {
                mECKey    = (ECKeyParameters)param;
                mECParams = mECKey.Parameters;
            }

            mCurveLength = (mECParams.Curve.FieldSize + 7) / 8;
        }
Example #2
0
        public virtual void Init(bool forSigning, ICipherParameters parameters)
        {
            SecureRandom providedRandom = null;

            if (forSigning)
            {
                if (!(parameters is ECPrivateKeyParameters))
                {
                    throw new InvalidKeyException("EC private key required for signing");
                }

                this.key = (ECPrivateKeyParameters)parameters;
            }
            else
            {
                if (!(parameters is ECPublicKeyParameters))
                {
                    throw new InvalidKeyException("EC public key required for verification");
                }

                this.key = (ECPublicKeyParameters)parameters;
            }

            this.random = InitSecureRandom(forSigning && !kCalculator.IsDeterministic, providedRandom);
        }
Example #3
0
        //creating  json structure to hit Active Ledger
        #region Json structure for onBoarding keys

        public static JObject GetTxJsonForOnboardingKeys(AsymmetricCipherKeyPair keypair, string keyType)
        {
            JObject json         = new JObject();
            JObject sigsIdentity = new JObject();
            JObject tx           = GenerateTx.GetTxForBoarding(keypair, keyType);

            json.Add("$selfsign", true);
            string tx_str = Helper.ConvertJsonToString(tx);

            //converting transaction in to byte Array
            byte[] originalData = Helper.ConvertStringToByteArray(tx_str);
            //signing the transaction
            if (keyType == "RSA")
            {
                RsaKeyParameters priKey     = (RsaKeyParameters)keypair.Private;
                byte[]           signedData = GenerateSignature.GetSignatureRSA(originalData, priKey);
                sigsIdentity.Add("identity", Helper.ConvertByteArrayToBase64String(signedData));
            }
            else
            {
                ECKeyParameters priECKey   = (ECKeyParameters)keypair.Private;
                byte[]          signedData = GenerateSignature.GetSignatureEC(originalData, priECKey);
                sigsIdentity.Add("identity", Helper.ConvertByteArrayToBase64String(signedData));
            }

            json.Add("$sigs", sigsIdentity);
            json.Add("$tx", tx);
            return(json);
        }
Example #4
0
 public ECKey()
 {
     var generator = new ECKeyPairGenerator("EC");
     generator.Init(new ECKeyGenerationParameters(CURVE, _secureRandom));
     var pair = generator.GenerateKeyPair();
     _Key = (ECPrivateKeyParameters)pair.Private;
 }
Example #5
0
        public virtual void Init(
            bool forSigning,
            ICipherParameters parameters)
        {
            if (forSigning)
            {
                if (parameters is ParametersWithRandom)
                {
                    ParametersWithRandom rParam = (ParametersWithRandom)parameters;

                    this.random = rParam.Random;
                    parameters  = rParam.Parameters;
                }
                else
                {
                    this.random = new SecureRandom();
                }

                if (!(parameters is ECPrivateKeyParameters))
                {
                    throw new InvalidKeyException("EC private key required for signing");
                }

                this.key = (ECPrivateKeyParameters)parameters;
            }
            else
            {
                if (!(parameters is ECPublicKeyParameters))
                {
                    throw new InvalidKeyException("EC public key required for verification");
                }

                this.key = (ECPublicKeyParameters)parameters;
            }
        }
 protected bool Equals(ECKeyParameters other)
 {
     if (parameters.Equals(other.parameters))
     {
         return(Equals((AsymmetricKeyParameter)other));
     }
     return(false);
 }
Example #7
0
 public ECKeyPairGenerator(string algorithm)
 {
     if (algorithm == null)
     {
         throw new ArgumentNullException("algorithm");
     }
     this.algorithm = ECKeyParameters.VerifyAlgorithmName(algorithm);
 }
 public ECKeyPairGenerator(string algorithm)
 {
     //IL_000e: Unknown result type (might be due to invalid IL or missing references)
     if (algorithm == null)
     {
         throw new ArgumentNullException("algorithm");
     }
     this.algorithm = ECKeyParameters.VerifyAlgorithmName(algorithm);
 }
Example #9
0
        //Bouncy castle method to generate EC signature using SHA256WithECDSA algorithm
        #region GetSignatureEC Method

        public static byte[] GetSignatureEC(byte[] plainText, ECKeyParameters privateKey)
        {
            var signer = SignerUtilities.GetSigner("SHA256WithECDSA");

            signer.Init(true, privateKey);
            signer.BlockUpdate(plainText, 0, plainText.Length);
            //returning generated signature
            return(signer.GenerateSignature());
        }
Example #10
0
 /// <summary>
 /// Us
 /// </summary>
 /// <param name="key"></param>
 /// <param name="isPrivate"></param>
 public CryptoKey(byte[] key, bool isPrivate)
 {
     if (isPrivate)
     {
         Key = new ECPrivateKeyParameters(new BigInteger(1, key), CURVE);
     }
     else
     {
         Key = new ECPublicKeyParameters("EC", Secp256k1.Curve.DecodePoint(key), CURVE);
     }
 }
Example #11
0
 public ECKeyPair(byte[] key, bool isPrivate)
 {
     if (isPrivate)
     {
         _key = new ECPrivateKeyParameters(new BigInteger(1, key), DomainParameter);
     }
     else
     {
         _key = new ECPublicKeyParameters("EC", Secp256K1.Curve.DecodePoint(key), DomainParameter);
     }
 }
Example #12
0
        public virtual void Init(bool forSigning, ICipherParameters parameters)
        {
            ICipherParameters baseParam;

            byte[] userID;

            if (parameters is ParametersWithID)
            {
                baseParam = ((ParametersWithID)parameters).Parameters;
                userID    = ((ParametersWithID)parameters).GetID();

                if (userID.Length >= 8192)
                {
                    throw new ArgumentException("SM2 user ID must be less than 2^16 bits long");
                }
            }
            else
            {
                baseParam = parameters;
                // the default value, string value is "1234567812345678"
                userID = Hex.Decode("31323334353637383132333435363738");
            }

            if (forSigning)
            {
                if (baseParam is ParametersWithRandom)
                {
                    ParametersWithRandom rParam = (ParametersWithRandom)baseParam;

                    ecKey    = (ECKeyParameters)rParam.Parameters;
                    ecParams = ecKey.Parameters;
                    kCalculator.Init(ecParams.N, rParam.Random);
                }
                else
                {
                    ecKey    = (ECKeyParameters)baseParam;
                    ecParams = ecKey.Parameters;
                    kCalculator.Init(ecParams.N, new SecureRandom());
                }
                pubPoint = CreateBasePointMultiplier().Multiply(ecParams.G, ((ECPrivateKeyParameters)ecKey).D).Normalize();
            }
            else
            {
                ecKey    = (ECKeyParameters)baseParam;
                ecParams = ecKey.Parameters;
                pubPoint = ((ECPublicKeyParameters)ecKey).Q;
            }

            digest.Reset();
            z = GetZ(userID);

            digest.BlockUpdate(z, 0, z.Length);
        }
Example #13
0
 public ECKey(byte[] vch, bool isPrivate)
 {
     if (isPrivate)
     {
         _Key = new ECPrivateKeyParameters(new NBitcoin.BouncyCastle.Math.BigInteger(1, vch), DomainParameter);
     }
     else
     {
         var q = Secp256k1.Curve.DecodePoint(vch);
         _Key = new ECPublicKeyParameters("EC", q, DomainParameter);
     }
 }
Example #14
0
 protected ECKey(byte[] bytes, bool isPrivate)
 {
     if (isPrivate)
     {
         this.key = new ECPrivateKeyParameters(new BigInteger(1, bytes), DomainParameter);
     }
     else
     {
         var q = Secp256k1.Curve.DecodePoint(bytes);
         this.key = new ECPublicKeyParameters("EC", q, DomainParameter);
     }
 }
Example #15
0
 public ECKey(byte[] vch, bool isPrivate)
 {
     if (isPrivate)
     {
         this._Key = new ECPrivateKeyParameters(new BigInteger(1, vch), this.DomainParameter);
     }
     else
     {
         var q = Secp256k1.Curve.DecodePoint(vch);
         this._Key = new ECPublicKeyParameters("EC", q, this.DomainParameter);
     }
 }
Example #16
0
        /// <summary>
        /// Method for creating a new key pair. Should only be used after creating a new secure random seed.
        /// </summary>
        public void GenerateKey()
        {
            var gen         = new ECKeyPairGenerator("EC");
            var keyGenParam = new KeyGenerationParameters(SecureRandom, 256);

            gen.Init(keyGenParam);
            var keyPair      = gen.GenerateKeyPair();
            var privateBytes = ((ECPrivateKeyParameters)keyPair.Private).D.ToByteArray();

            if (privateBytes.Length != 32)
            {
                GenerateKey();
            }
            Key = new ECPrivateKeyParameters(new BigInteger(1, privateBytes), CURVE);
        }
Example #17
0
        public static bool ECVerify(string signedData, string signature, string publicKey)
        {
            try
            {
                byte[] r = Convert.FromBase64String(signature);
                byte[] s = Encoding.UTF8.GetBytes(signedData);

                PemReader pemReader                = new PemReader(new StringReader(publicKey));
                AsymmetricKeyParameter pKey        = (AsymmetricKeyParameter)pemReader.ReadObject();
                ECKeyParameters        publicECKey = (ECKeyParameters)pKey;

                ISigner sig = SignerUtilities.GetSigner("Sha512WithECDSA");
                sig.Init(false, publicECKey);
                sig.BlockUpdate(s, 0, s.Length);
                return(sig.VerifySignature(r));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #18
0
        public static string ECSign(string toBeSigned, string privateKey)
        {
            try
            {
                byte[] r = Encoding.UTF8.GetBytes(toBeSigned);

                PemReader pemReader                  = new PemReader(new StringReader(privateKey));
                AsymmetricCipherKeyPair keyPair      = (AsymmetricCipherKeyPair)pemReader.ReadObject();
                ECKeyParameters         privateECKey = (ECKeyParameters)keyPair.Private;

                ISigner sig = SignerUtilities.GetSigner("Sha512WithECDSA");
                sig.Init(true, privateECKey);
                sig.BlockUpdate(r, 0, r.Length);
                byte[] signedBytes = sig.GenerateSignature();

                return(Convert.ToBase64String(signedBytes));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #19
0
 public virtual void Init(bool forSigning, ICipherParameters parameters)
 {
     this.forSigning = forSigning;
     if (forSigning)
     {
         if (parameters is ParametersWithRandom)
         {
             ParametersWithRandom rParam = (ParametersWithRandom)parameters;
             this.secureRandom = rParam.Random;
             this.key          = (ECPrivateKeyParameters)rParam.Parameters;
         }
         else
         {
             this.secureRandom = new SecureRandom();
             this.key          = (ECPrivateKeyParameters)parameters;
         }
     }
     else
     {
         this.key = (ECPublicKeyParameters)parameters;
     }
 }
        public void Init(bool forSigning, ICipherParameters parameters)
        {
            if (forSigning)
            {
                if (parameters is ParametersWithRandom)
                {
                    ParametersWithRandom rParams = (ParametersWithRandom)parameters;
                    this.random = rParams.Random;
                    parameters  = rParams.Parameters;
                }
                else
                {
                    this.random = new SecureRandom();
                }

                this.operationKey = (ECPrivateKeyParameters)parameters;
            }
            else
            {
                this.operationKey = (ECPublicKeyParameters)parameters;
            }
        }
Example #21
0
 public BouncyCastleEcdsaSecurityKey(ECKeyParameters keyParameters)
 {
     KeyParameters = keyParameters;
     CryptoProviderFactory.CustomCryptoProvider = new CustomCryptoProvider();
 }
Example #22
0
 private static void ECCAgreement(string file_akey_pub, string file_akey_prv, string file_output)
 {
     ECKeyParameters public_key  = LoadPEMFile <ECKeyParameters>(file_akey_pub, default, false);
Example #23
0
 public static string GetStringFromKey(this ECKeyParameters key) =>
 Convert.ToBase64String(key.PublicKeyParamSet.GetEncoded());
 public static bool ValidateCurve(ECKeyParameters key) =>
 key.Parameters.Equals(Domain);
 public ECKeyGenerationParameters(DerObjectIdentifier publicKeyParamSet, SecureRandom random)
     : this(ECKeyParameters.LookupParameters(publicKeyParamSet), random)
 {
     this.publicKeyParamSet = publicKeyParamSet;
 }