Esempio n. 1
0
        } // End Sub Test

        public static string SignData(string msg, Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters privKey)
        {
            try
            {
                byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(msg);


                // https://github.com/neoeinstein/bouncycastle/blob/master/crypto/src/security/SignerUtilities.cs
                // algorithms["SHA-256/ECDSA"] = "SHA-256withECDSA";
                // algorithms["SHA-384/ECDSA"] = "SHA-384withECDSA";
                // algorithms["SHA-512/ECDSA"] = "SHA-512withECDSA";

                Org.BouncyCastle.Crypto.ISigner signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner("SHA-256withECDSA");
                signer.Init(true, privKey);
                signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
                byte[] sigBytes = signer.GenerateSignature();

                return(System.Convert.ToBase64String(sigBytes));
            }
            catch (System.Exception exc)
            {
                System.Console.WriteLine("Signing Failed: " + exc.ToString());
                return(null);
            }
        } // End Function SignData
Esempio n. 2
0
        public static System.Security.Cryptography.ECDsa GetMsEcdsaProvider()
        {
            string namedCurve = "prime256v1";

            Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator pGen =
                new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator();

            Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters genParam =
                new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(
                    Org.BouncyCastle.Asn1.X9.X962NamedCurves.GetOid(namedCurve),
                    new Org.BouncyCastle.Security.SecureRandom()
                    );

            pGen.Init(genParam);

            Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = pGen.GenerateKeyPair();

            Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters pub =
                (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)keyPair.Public;

            Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters priv =
                (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)keyPair.Private;


            System.Security.Cryptography.ECParameters pars = new ECParameters();
            //string str = priv.Parameters.Curve.ToString();
            //System.Console.WriteLine(str);

            //pars.Curve = new ECCurve();


            //pars.D = priv.D.ToByteArray();
            //pars.Q = new System.Security.Cryptography.ECPoint();
            //pars.Q.X = pub.Q.X.GetEncoded();
            //pars.Q.Y = pub.Q.Y.GetEncoded();

            //System.Security.Cryptography.ECDsa.Create(pars);


            // The CngKey can be created by importing the key using the Der encoded bytes:
            Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfo bcKeyInfo =
                Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private)
            ;

            byte[] pkcs8Blob   = bcKeyInfo.GetDerEncoded();
            CngKey importedKey = CngKey.Import(pkcs8Blob, CngKeyBlobFormat.Pkcs8PrivateBlob);

            return(new System.Security.Cryptography.ECDsaCng(importedKey));
        }
Esempio n. 3
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp)
            : base()
        {
            this.m_privKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)kp.Private;
            this.m_pubKey  = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)kp.Public;
            //this.KeySizeValue = keySize;

            //var x = (Org.BouncyCastle.Crypto.Parameters.ECKeyParameters)kp.Public;

            // var x = (Org.BouncyCastle.Crypto.Parameters.DsaPublicKeyParameters)kp.Public;
            // var y = (Org.BouncyCastle.Crypto.Parameters.DsaPrivateKeyParameters)kp.Private;

            // this.KeySizeValue = x.Y.BitCount;
            // this.KeySizeValue = y.X.BitCount;
            this.KeySizeValue = this.m_privKey.Parameters.Curve.FieldSize;
        } // End Constructor
Esempio n. 4
0
        internal Secp256k1KeyPair(Org.BouncyCastle.Math.BigInteger privateKey)
        {
            this.privateKey = privateKey;

            this.k1Params  = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
            this.publicKey = new Secp256k1PublicKey(k1Params, k1Params.G.Multiply(privateKey));

            signer = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner(
                new Org.BouncyCastle.Crypto.Signers.HMacDsaKCalculator(
                    new Org.BouncyCastle.Crypto.Digests.Sha256Digest()));
            var parameters = new Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters(
                this.privateKey,
                new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(k1Params.Curve, k1Params.G, k1Params.N, k1Params.H));

            signer.Init(true, parameters);
        }
Esempio n. 5
0
        } // End Function ReadPrivateKey

        public static void TestSignature()
        {
            System.Console.WriteLine("Attempting to load cert...");
            System.Security.Cryptography.X509Certificates.X509Certificate2 thisCert = null; // LoadCertificate();

            System.Console.WriteLine(thisCert.IssuerName.Name);
            System.Console.WriteLine("Signing the text - Mary had a nuclear bomb");

            byte[] pkcs12Bytes = thisCert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pkcs12, "dummy");
            Org.BouncyCastle.Pkcs.Pkcs12Store pkcs12 = new Org.BouncyCastle.Pkcs.Pkcs12StoreBuilder().Build();

            pkcs12.Load(new System.IO.MemoryStream(pkcs12Bytes, false), "dummy".ToCharArray());

            Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters privKey = null;
            foreach (string alias in pkcs12.Aliases)
            {
                if (pkcs12.IsKeyEntry(alias))
                {
                    privKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)pkcs12.GetKey(alias).Key;
                    break;
                } // End if (pkcs12.IsKeyEntry(alias))
            }     // Next alias

            string signature = SignData("Mary had a nuclear bomb", privKey);

            System.Console.WriteLine("Signature: " + signature);

            System.Console.WriteLine("Verifying Signature");

            Org.BouncyCastle.X509.X509Certificate bcCert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(thisCert);
            if (VerifySignature((Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)bcCert.GetPublicKey(), signature, "Mary had a nuclear bomb."))
            {
                System.Console.WriteLine("Valid Signature!");
            }
            else
            {
                System.Console.WriteLine("Signature NOT valid!");
            }
        } // End Sub TestSignature
        /// <summary>
        /// sign data
        /// </summary>
        /// <param name="macdata">soure data</param>
        /// <param name="pkInfo">private key</param>
        /// <returns>signature result</returns>
        public static byte[] SignData(Google.Protobuf.ByteString macdata, string pkInfo)
        {
            byte[] data = macdata.ToByteArray();
            string privateKey;

            if (!pkInfo.Contains("PRIVATE KEY"))
            {
                privateKey = ReadPK(pkInfo);
            }
            else
            {
                privateKey = pkInfo;
            }
            TextReader ptr = new StringReader(privateKey);

            Org.BouncyCastle.OpenSsl.PemReader pem = new Org.BouncyCastle.OpenSsl.PemReader(ptr);
            Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters ecdsaPrivateKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)pem.ReadObject();
            string curveName = "P-256";
            var    nistCurve = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName(curveName);

            var sign = nistCurve.Sign(ecdsaPrivateKey, data);

            return(sign);
        }
Esempio n. 7
0
        }   // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters privKey)
            : base()
        {
            this.m_privKey    = privKey;
            this.KeySizeValue = this.m_privKey.Parameters.Curve.FieldSize;
        } // End Constructor
 public JwtKey(Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters ecPrivateKey)
 {
     this.EcPrivateKey = ecPrivateKey;
 }
Esempio n. 9
0
        } // End Constructor

        public BouncyECDSA(Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair kp)
        {
            this.m_privKey = (Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters)kp.Private;
            this.m_pubKey  = (Org.BouncyCastle.Crypto.Parameters.ECPublicKeyParameters)kp.Public;
        } // End Constructor
Esempio n. 10
0
 public BouncyECDSA(Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters privKey)
 {
     this.m_privKey = privKey;
 } // End Constructor