示例#1
0
        private SM2()
        {
            EccParam = CurveParameter;


            EccP  = new BigInteger(EccParam[0], 16);
            EccA  = new BigInteger(EccParam[1], 16);
            EccB  = new BigInteger(EccParam[2], 16);
            EccN  = new BigInteger(EccParam[3], 16);
            EccGx = new BigInteger(EccParam[4], 16);
            EccGy = new BigInteger(EccParam[5], 16);


            ECFieldElement ecc_gx_fieldelement = new FpFieldElement(EccP, EccGx);
            ECFieldElement ecc_gy_fieldelement = new FpFieldElement(EccP, EccGy);

            EccCurve  = new FpCurve(EccP, EccA, EccB);
            EccPointG = new FpPoint(EccCurve, ecc_gx_fieldelement, ecc_gy_fieldelement);

            EccBcSpec = new ECDomainParameters(EccCurve, EccPointG, EccN);

            ECKeyGenerationParameters ecc_ecgenparam;

            ecc_ecgenparam = new ECKeyGenerationParameters(EccBcSpec, new SecureRandom());

            EccKeyPairGenerator = new ECKeyPairGenerator();
            EccKeyPairGenerator.Init(ecc_ecgenparam);
        }
示例#2
0
文件: SM2.cs 项目: zxlwdgithub/guomi
        private SM2()
        {
            ecc_param = sm2_param;

            ECFieldElement ecc_gx_fieldelement;
            ECFieldElement ecc_gy_fieldelement;

            ecc_p  = new BigInteger(ecc_param[0], 16);
            ecc_a  = new BigInteger(ecc_param[1], 16);
            ecc_b  = new BigInteger(ecc_param[2], 16);
            ecc_n  = new BigInteger(ecc_param[3], 16);
            ecc_gx = new BigInteger(ecc_param[4], 16);
            ecc_gy = new BigInteger(ecc_param[5], 16);


            ecc_gx_fieldelement = new FpFieldElement(ecc_p, ecc_gx);
            ecc_gy_fieldelement = new FpFieldElement(ecc_p, ecc_gy);

            ecc_curve   = new FpCurve(ecc_p, ecc_a, ecc_b);
            ecc_point_g = new FpPoint(ecc_curve, ecc_gx_fieldelement, ecc_gy_fieldelement);

            ecc_bc_spec = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);

            ECKeyGenerationParameters ecc_ecgenparam;

            ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());

            ecc_key_pair_generator = new ECKeyPairGenerator();
            ecc_key_pair_generator.Init(ecc_ecgenparam);
        }
示例#3
0
文件: Util.cs 项目: anica87/Darwin
        public static ECPublicKeyParameters PublicKeyFromBytes(byte[] key)
        {
            /*
             * Convert public key into a public key object
             * The public key comprises two co-ordinates X followed by Y, both 32 bytes
             */

            byte[] public_key_x = new byte[32];
            byte[] public_key_y = new byte[32];

            Array.Copy(key, 0, public_key_x, 0, public_key_x.Length);
            Array.Copy(key, 32, public_key_y, 0, public_key_y.Length);

            BigInteger bi_x = new BigInteger(1, public_key_x);
            BigInteger bi_y = new BigInteger(1, public_key_y);

            // the key needs to relate to a specific curve
            X9ECParameters ecP    = X962NamedCurves.GetByName("prime256v1");
            var            ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());

            FpCurve c = (FpCurve)ecP.Curve;

            var fe_x = new FpFieldElement(c.Q, bi_x);
            var fe_y = new FpFieldElement(c.Q, bi_y);

            // point q represents the x,y co-ordinate of the public key
            ECPoint q = new FpPoint(c, fe_x, fe_y);

            return(new ECPublicKeyParameters("ECDSA", q, ecSpec));
        }
示例#4
0
        private SM2(bool sm2Test)
        {
            this.sm2Test = sm2Test;
            cipher_sm    = new Cipher();
            //if (sm2Test)//如果为对
            //    ecc_param = sm2_test_param;//使用国际密码管理局给的测试参数
            //else
            ecc_param = sm2_param;    //否则使用国密标准256位曲线参数
            ECFieldElement ecc_gx_fieldelement;
            ECFieldElement ecc_gy_fieldelement;

            ecc_p  = new BigInteger(ecc_param[0], 16);
            ecc_a  = new BigInteger(ecc_param[1], 16);
            ecc_b  = new BigInteger(ecc_param[2], 16);
            ecc_n  = new BigInteger(ecc_param[3], 16);
            ecc_gx = new BigInteger(ecc_param[4], 16);
            ecc_gy = new BigInteger(ecc_param[5], 16);
            ecc_gx_fieldelement = new FpFieldElement(ecc_p, ecc_gx);                                //选定椭圆曲线上基点G的x坐标
            ecc_gy_fieldelement = new FpFieldElement(ecc_p, ecc_gy);                                //选定椭圆曲线上基点G的坐标
            ecc_curve           = new FpCurve(ecc_p, ecc_a, ecc_b);                                 //生成椭圆曲线
            ecc_point_g         = new FpPoint(ecc_curve, ecc_gx_fieldelement, ecc_gy_fieldelement); //生成基点G
            ecc_bc_spec         = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);            //椭圆曲线,g点坐标,阶n.
            ECKeyGenerationParameters ecc_ecgenparam;

            ecc_ecgenparam         = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
            ecc_key_pair_generator = new ECKeyPairGenerator();
            ecc_key_pair_generator.Init(ecc_ecgenparam);
        }
示例#5
0
        public SM2CryptoServiceProvider()
        {
            ecc_param = sm2_param;

            ecc_p  = new BigInteger(sm2_param[0], 16);
            ecc_a  = new BigInteger(sm2_param[1], 16);
            ecc_b  = new BigInteger(sm2_param[2], 16);
            ecc_n  = new BigInteger(sm2_param[3], 16);
            ecc_gx = new BigInteger(sm2_param[4], 16);
            ecc_gy = new BigInteger(sm2_param[5], 16);

            //ecc_curve = new FpCurve(ecc_p, ecc_a, ecc_b, ecc_gx, ecc_gy);

            var ecc_gx_fieldelement = new FpFieldElement(ecc_p, ecc_gx); //ecc_curve.FromBigInteger(ecc_gx);//
            var ecc_gy_fieldelement = new FpFieldElement(ecc_p, ecc_gy); // ecc_curve.FromBigInteger(ecc_gy);//

            ecc_curve   = new FpCurve(ecc_p, ecc_a, ecc_b);              // ecc_curve.CreatePoint(ecc_gx, ecc_gy);//
            ecc_point_g = new FpPoint(ecc_curve, ecc_gx_fieldelement, ecc_gy_fieldelement);

            ecc_bc_spec = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);

            var ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());

            ecc_key_pair_generator = new ECKeyPairGenerator();
            ecc_key_pair_generator.Init(ecc_ecgenparam);
        }
 public virtual bool Equals(FpFieldElement other)
 {
     if (q.Equals(other.q))
     {
         return(base.Equals(other));
     }
     return(false);
 }
示例#7
0
 /**
  * Creates the points on the curve with literature values.
  */
 internal static void createPoints()
 {
     for (int i = 0; i < pointSource.Length / 2; i++)
     {
         FpFieldElement x = new FpFieldElement(q, new BigInteger(
                                                   pointSource[2 * i].ToString()));
         FpFieldElement y = new FpFieldElement(q, new BigInteger(
                                                   pointSource[2 * i + 1].ToString()));
         p[i] = new FpPoint(curve, x, y);
     }
 }
示例#8
0
 public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor)
     : base(q)
 {
     m_q        = q;
     m_r        = FpFieldElement.CalculateResidue(q);
     m_infinity = new FpPoint(this, null, null);
     m_a        = FromBigInteger(a);
     m_b        = FromBigInteger(b);
     m_order    = order;
     m_cofactor = cofactor;
     m_coord    = 4;
 }
    public override bool Equals(object obj)
    {
        if (obj == this)
        {
            return(true);
        }
        FpFieldElement fpFieldElement = obj as FpFieldElement;

        if (fpFieldElement == null)
        {
            return(false);
        }
        return(Equals(fpFieldElement));
    }
        public static int GetByteLength(
            ECFieldElement fe)
        {
            if (fe is FpFieldElement)
            {
                FpFieldElement ep = (FpFieldElement)fe;

                return((ep.Q.BitLength + 7) / 8);
            }

            F2mFieldElement em = (F2mFieldElement)fe;

            return((em.M + 7) / 8);
        }
        public static BigInteger CalculateSharedKey(BigInteger BIx, BigInteger BIy, ECPrivateKeyParameters privateKey)
        {
            IBasicAgreement aKeyAgree = AgreementUtilities.GetBasicAgreement("ECDH");

            aKeyAgree.Init(privateKey);

            X9ECParameters     ecP    = NistNamedCurves.GetByName("P-521");
            ECDomainParameters ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());

            FpCurve c = (FpCurve)ecSpec.Curve;

            ECFieldElement x = new FpFieldElement(c.Q, BIx);
            ECFieldElement y = new FpFieldElement(c.Q, BIy);
            ECPoint        q = new FpPoint(ecP.Curve, x, y);

            ECPublicKeyParameters publicKey = new ECPublicKeyParameters("ECDH", q, SecObjectIdentifiers.SecP521r1);

            BigInteger k1 = aKeyAgree.CalculateAgreement(publicKey);

            return(k1);
        }
示例#12
0
文件: Setup.cs 项目: sai-dhanush/ibe
        public FpPoint Exctract(string ID, bool decrypt = false)
        {
            if (decrypt)
            {
                string sStr = File.ReadAllText("mk");
                s = int.Parse(sStr);
            }

            //  y^2 = x^3 + 117050x^2 + x
            BigInteger x = GeneralFunctions.H1hash(ID, p);
            BigInteger y = x.Pow(3).Add(x.Pow(2).Multiply(new BigInteger("117050", 10))).Add(x).Pow(2).ModInverse(p);

            FpFieldElement x_Qid = new FpFieldElement(q, x);
            FpFieldElement y_Qid = new FpFieldElement(q, y);
            FpPoint        Qid   = new FpPoint(E, x_Qid, y_Qid);

            FpPoint d_id = (FpPoint)Qid.Multiply(new BigInteger(s.ToString(), 10));

            // privatni ključ
            return(d_id);
        }
示例#13
0
文件: Setup.cs 项目: sai-dhanush/ibe
        public Setup()
        {
            n = 3;

            do
            {
                Random r = new Random();
                s = r.Next(1, int.MaxValue - 1);
            } while (s == 0);

            // p i q
            p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10);
            //q = p.Pow(n);
            q = p;

            k = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494337", 10);

            // E - krivulja secp256k1 - y ^ 2 = x ^ 3 + 0*x + 7
            BigInteger a = new BigInteger("0", 10);
            BigInteger b = new BigInteger("7", 10);

            E = new FpCurve(q, a, b);

            // P
            BigInteger     x1 = new BigInteger("55066263022277343669578718895168534326250603453777594175500187360389116729240", 10);
            BigInteger     y1 = new BigInteger("32670510020758816978083085130507043184471273380659243275938904335757337482424", 10);
            FpFieldElement x  = (FpFieldElement)E.FromBigInteger(x1); // new FpFieldElement(q, x1);
            FpFieldElement y  = (FpFieldElement)E.FromBigInteger(y1); // new FpFieldElement(q, y1);

            P = new FpPoint(E, x, y);

            BigInteger mtp = new BigInteger(s.ToString(), 10);

            Ppub = (FpPoint)P.Multiply(mtp);

            File.WriteAllText("mk", s.ToString() + Environment.NewLine);
        }
示例#14
0
        public Cypher GetCypher(string message)
        {
            BigInteger     x     = GeneralFunctions.H1hash(ID, prim);
            BigInteger     y     = x.Pow(3).Add(new BigInteger("7", 10)).Pow(2).ModInverse(prim);
            FpFieldElement x_Qid = new FpFieldElement(E.Q, x);
            FpFieldElement y_Qid = new FpFieldElement(E.Q, y);
            FpPoint        Qid   = new FpPoint(E, x_Qid, y_Qid);

            int r = 0;

            do
            {
                Random rnd = new Random();
                r = rnd.Next(1, int.MaxValue - 1);
            } while (r == 0);

            FpPoint rP = (FpPoint)P.Multiply(new BigInteger(r.ToString(), 10));

            BigInteger gid = GeneralFunctions.Pair(Qid, Ppub, k, prim);

            gid = gid.ModPow(new BigInteger(r.ToString(), 10), prim);

            char[] M      = message.ToCharArray();
            char[] cArray = new char[M.Length];
            char[] hash   = GeneralFunctions.H2hash(gid, prim).ToCharArray();
            for (int i = 0; i < message.Length; i++)
            {
                cArray[i] = (char)(M[i] ^ hash[i % hash.Length]);
            }

            string c = new String(cArray);

            return(new Cypher {
                U = rP, V = c
            });
        }
示例#15
0
 protected bool Equals(
  FpFieldElement other)
 {
     return q.Equals(other.q) && base.Equals(other);
 }
示例#16
0
        // D.1.4 91
        /**
           * return a sqrt root - the routine verifies that the calculation
           * returns the right value - if none exists it returns null.
           */
        public override ECFieldElement Sqrt()
        {
            if (!q.TestBit(0)) {
            throw Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("even value of q");
             }

             // p mod 4 == 3
             if (q.TestBit(1)) {
            // TODO Can this be optimised (inline the Square?)
            // z = g^(u+1) + p, p = 4u + 3
            ECFieldElement z = new FpFieldElement(q, x.ModPow(q.ShiftRight(2).Add(BigInteger.One), q));
            FpFieldElement zSquare = (FpFieldElement)z.Square();
            return zSquare.Equals(this) ? z : null;
             }

             // p mod 4 == 1
             BigInteger qMinusOne = q.Subtract(BigInteger.One);

             BigInteger legendreExponent = qMinusOne.ShiftRight(1);
             if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One))) {
            return null;
             }

             BigInteger u = qMinusOne.ShiftRight(2);
             BigInteger k = u.ShiftLeft(1).Add(BigInteger.One);

             BigInteger Q = this.x;
             BigInteger fourQ = Q.ShiftLeft(2).Mod(q);

             BigInteger U, V;
             do {
            System.Random rand = new System.Random();
            BigInteger P;
            do {
               P = new BigInteger(q.BitLength, rand);
            } while (P.CompareTo(q) >= 0
                     || !(P.Multiply(P).Subtract(fourQ).ModPow(legendreExponent, q).Equals(qMinusOne)));

            BigInteger[] result = fastLucasSequence(q, P, Q, k);
            U = result[0];
            V = result[1];

            if (V.Multiply(V).Mod(q).Equals(fourQ)) {
               // Integer division by 2, mod q
               if (V.TestBit(0)) {
                  V = V.Add(q);
               }

               V = V.ShiftRight(1);

               //O-FIXME: Debug.Assert(V.Multiply(V).Mod(q).Equals(x));

               return new FpFieldElement(q, V);
            }
             } while (U.Equals(BigInteger.One) || U.Equals(qMinusOne));

             return null;
        }
示例#17
0
 /**
  * return a sqrt root - the routine verifies that the calculation returns the right value - if
  * none exists it returns null.
  */
 public override ECFieldElement Sqrt()
 {
     ECFieldElement root = new FpFieldElement(Q, ToBigInteger()).Sqrt();
     return root == null ? null : new SecP224R1FieldElement(root.ToBigInteger());
 }
示例#18
0
        private static void Main(string[] args)
        {
            string id     = "*****@*****.**";
            string poruka = "moram porati posluku";
            Cypher sifrat;

            if (args.Length < 2)
            {
                test();
                Console.WriteLine("\n");
                upute();
                return;
            }

            // namjesti postavke prvo
            Setup setup = new Setup();

            if (args[0] == "-f")
            {
                string put = args[1];
                if (!File.Exists(put))
                {
                    poruka = File.ReadAllText(put);
                    if (args.Length != 3)
                    {
                        upute();
                        return;
                    }

                    id = args[args.Length - 1];

                    encode(poruka, id, setup);
                }
                else
                {
                    Console.WriteLine("File does not exists!\n");
                    upute();
                    return;
                }
            }

            string sif;
            string xs;
            string ys;

            if (args[1] == "-d")
            {
                if (args[1] == "-f" && args.Length == 6)
                {
                    string put = args[2];
                    sif = File.ReadAllText(put);

                    id = args[args.Length - 1];
                    xs = args[3];
                    ys = args[4];
                }
                else if (args.Length > 6 || args.Length != 5)
                {
                    upute();
                    return;
                }
                else
                {
                    sif = args[1];
                    xs  = args[2];
                    ys  = args[3];
                    id  = args[args.Length - 1];
                }

                BigInteger x1 = new BigInteger(xs, 10);
                BigInteger y1 = new BigInteger(ys, 10);

                FpFieldElement x = (FpFieldElement)setup.E.FromBigInteger(x1);
                FpFieldElement y = (FpFieldElement)setup.E.FromBigInteger(y1);

                FpPoint point = new FpPoint(setup.E, x, y);

                sifrat = new Cypher {
                    U = point, V = sif
                };

                decode(sifrat, id, setup);
            }
            else
            {
                poruka = "";
                for (int i = 1; i < args.Length - 2; i++)
                {
                    poruka += args[i] + " ";
                }
                poruka += args[args.Length - 2];

                id = args[args.Length - 1];

                encode(poruka, id, setup);
            }

            Console.ReadKey();
        }
示例#19
0
        public static bool Verify(byte[] msg, byte[] signData, byte[] certData)
        {
            var x5092 = new System.Security.Cryptography.X509Certificates.X509Certificate2(certData);

            byte[] certPK = x5092.GetPublicKey();

            certPK = ByteUtils.SubBytes(certPK, 1, 64);

            byte[] certPKX = ByteUtils.SubBytes(certPK, certPK.Length - 32 - 32, 32);
            byte[] certPKY = ByteUtils.SubBytes(certPK, certPK.Length - 32, 32);


            System.String strcertPKX = ByteUtils.ByteArrayToHex(certPKX);
            System.String strcertPKY = ByteUtils.ByteArrayToHex(certPKY);
            BigInteger    biX        = new BigInteger(strcertPKX, 16);
            BigInteger    biY        = new BigInteger(strcertPKY, 16);


            ECFieldElement x = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biX);
            ECFieldElement y = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biY);

            ECPoint userKey = new FpPoint(SM2CryptoServiceProvider.Instance.ecc_curve, x, y);


            SM3Digest sm3 = new SM3Digest();

            byte[] z = SM2CryptoServiceProvider.Instance.Sm2GetZ(Encoding.Default.GetBytes(SM2CryptoServiceProvider.Instance.userId), userKey);
            sm3.BlockUpdate(z, 0, z.Length);


            byte[] p = msg;
            sm3.BlockUpdate(p, 0, p.Length);

            byte[] md = new byte[32];
            sm3.DoFinal(md, 0);


            byte[] btR = ByteUtils.SubBytes(signData, 0, 32);
            byte[] btS = ByteUtils.SubBytes(signData, 32, 32);


            System.String strR = ByteUtils.ByteArrayToHex(btR);
            System.String strS = ByteUtils.ByteArrayToHex(btS);
            BigInteger    r    = new BigInteger(strR, 16);
            BigInteger    s    = new BigInteger(strS, 16);

            // e_
            BigInteger e = new BigInteger(1, md);
            // t
            BigInteger t = r.Add(s).Mod(SM2CryptoServiceProvider.Instance.ecc_n);

            if (t.Equals(BigInteger.Zero))
            {
                return(false);
            }

            // x1y1
            ECPoint x1y1 = SM2CryptoServiceProvider.Instance.ecc_point_g.Multiply(s);

            x1y1 = x1y1.Add(userKey.Multiply(t));

            // R
            BigInteger R = e.Add(x1y1.X.ToBigInteger()).Mod(SM2CryptoServiceProvider.Instance.ecc_n);

            return(r.Equals(R));
        }
示例#20
0
        // D.1.4 91

        /**
         * return a sqrt root - the routine verifies that the calculation
         * returns the right value - if none exists it returns null.
         */
        public override ECFieldElement Sqrt()
        {
            if (!q.TestBit(0))
            {
                throw Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("even value of q");
            }

            // p mod 4 == 3
            if (q.TestBit(1))
            {
                // TODO Can this be optimised (inline the Square?)
                // z = g^(u+1) + p, p = 4u + 3
                ECFieldElement z       = new FpFieldElement(q, x.ModPow(q.ShiftRight(2).Add(BigInteger.One), q));
                FpFieldElement zSquare = (FpFieldElement)z.Square();
                return(zSquare.Equals(this) ? z : null);
            }

            // p mod 4 == 1
            BigInteger qMinusOne = q.Subtract(BigInteger.One);

            BigInteger legendreExponent = qMinusOne.ShiftRight(1);

            if (!(x.ModPow(legendreExponent, q).Equals(BigInteger.One)))
            {
                return(null);
            }

            BigInteger u = qMinusOne.ShiftRight(2);
            BigInteger k = u.ShiftLeft(1).Add(BigInteger.One);

            BigInteger Q     = this.x;
            BigInteger fourQ = Q.ShiftLeft(2).Mod(q);

            BigInteger U, V;

            do
            {
                System.Random rand = new System.Random();
                BigInteger    P;
                do
                {
                    P = new BigInteger(q.BitLength, rand);
                } while (P.CompareTo(q) >= 0 ||
                         !(P.Multiply(P).Subtract(fourQ).ModPow(legendreExponent, q).Equals(qMinusOne)));

                BigInteger[] result = fastLucasSequence(q, P, Q, k);
                U = result[0];
                V = result[1];

                if (V.Multiply(V).Mod(q).Equals(fourQ))
                {
                    // Integer division by 2, mod q
                    if (V.TestBit(0))
                    {
                        V = V.Add(q);
                    }

                    V = V.ShiftRight(1);

                    //O-FIXME: Debug.Assert(V.Multiply(V).Mod(q).Equals(x));

                    return(new FpFieldElement(q, V));
                }
            } while (U.Equals(BigInteger.One) || U.Equals(qMinusOne));

            return(null);
        }
示例#21
0
        public static void TestBouncy(DHParameters par)
        {
            X9ECParameters     ecP    = NistNamedCurves.GetByName("P-521");
            ECDomainParameters ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed());
            ECKeyPairGenerator g      = new ECKeyPairGenerator();

            g.Init(new ECKeyGenerationParameters(ecSpec, new SecureRandom()));

            //
            // a side
            //
            AsymmetricCipherKeyPair aKeyPair  = g.GenerateKeyPair();
            IBasicAgreement         aKeyAgree = AgreementUtilities.GetBasicAgreement("ECDH");

            aKeyAgree.Init(aKeyPair.Private);
            ECPublicKeyParameters pubKey1 = (ECPublicKeyParameters)aKeyPair.Public;

            BigInteger asx1 = pubKey1.Q.X.ToBigInteger();

            byte[] axb = asx1.ToByteArray();

            BigInteger asy1 = pubKey1.Q.Y.ToBigInteger();

            byte[] ayb = asy1.ToByteArray();

            // client public key X co-ordinate Hex string
            StringBuilder axhex = new StringBuilder(axb.Length * 2);

            foreach (byte b in axb)
            {
                axhex.AppendFormat("{0:x2}", b);
            }
            string xHex = axhex.ToString();

            // client public key Y co-ordinate Hex string
            StringBuilder ayhex = new StringBuilder(ayb.Length * 2);

            foreach (byte b in ayb)
            {
                ayhex.AppendFormat("{0:x2}", b);
            }
            string yHex = ayhex.ToString();

            Console.WriteLine(string.Format("Alice X coordinate {0}", xHex));
            Console.WriteLine(string.Format("Alice Y coordinate {0}", yHex));

            //
            // b side
            //
            AsymmetricCipherKeyPair bKeyPair  = g.GenerateKeyPair();
            IBasicAgreement         bKeyAgree = AgreementUtilities.GetBasicAgreement("ECDH");

            aKeyAgree.Init(bKeyPair.Private);
            ECPublicKeyParameters pubKey2 = (ECPublicKeyParameters)aKeyPair.Public;

            BigInteger bsx1 = pubKey2.Q.X.ToBigInteger();

            byte[] bxb = bsx1.ToByteArray();

            BigInteger bsy1 = pubKey2.Q.Y.ToBigInteger();

            byte[] byb = bsy1.ToByteArray();

            // client public key X co-ordinate Hex string
            StringBuilder bxhex = new StringBuilder(bxb.Length * 2);

            foreach (byte b in bxb)
            {
                bxhex.AppendFormat("{0:x2}", b);
            }
            string xbHex = bxhex.ToString();

            // client public key Y co-ordinate Hex string
            StringBuilder byhex = new StringBuilder(byb.Length * 2);

            foreach (byte b in byb)
            {
                byhex.AppendFormat("{0:x2}", b);
            }
            string ybHex = byhex.ToString();

            Console.WriteLine(string.Format("Bob X coordinate {0}", xbHex));
            Console.WriteLine(string.Format("Bob Y coordinate {0}", ybHex));

            string BobXhex = xbHex;

            string BobYhex = ybHex;

            FpCurve c = (FpCurve)ecSpec.Curve;

            ECFieldElement        x         = new FpFieldElement(c.Q, new BigInteger(BobXhex, 16));
            ECFieldElement        y         = new FpFieldElement(c.Q, new BigInteger(BobYhex, 16));
            ECPoint               q         = new FpPoint(ecP.Curve, x, y);
            ECPublicKeyParameters publicKey = new ECPublicKeyParameters("ECDH", q, SecObjectIdentifiers.SecP521r1);

            BigInteger k1 = aKeyAgree.CalculateAgreement(publicKey);

            byte[] genKey = k1.ToByteArray();

            StringBuilder genKeySB = new StringBuilder(genKey.Length * 2);

            foreach (byte b in genKey)
            {
                genKeySB.AppendFormat("{0:x2}", b);
            }
            string genratedKey = genKeySB.ToString();

            Console.WriteLine(string.Format("Generated Key {0}", genratedKey));

            //calc sha-256 now
            IDigest hash = new Sha256Digest();

            byte[] result = new byte[hash.GetDigestSize()];
            hash.BlockUpdate(genKey, 0, genKey.Length);
            hash.DoFinal(result, 0);

            StringBuilder share = new StringBuilder(result.Length * 2);

            foreach (byte b in result)
            {
                share.AppendFormat("{0:x2}", b);
            }
            string sharedKey = share.ToString();

            Console.WriteLine(string.Format("Shared Key {0}", sharedKey));

            Console.ReadLine();
        }
示例#22
0
 protected bool Equals(
     FpFieldElement other)
 {
     return(q.Equals(other.q) && base.Equals(other));
 }