示例#1
0
        //Ep対応テスト
        //失敗率は1/(2^100)であることに注意
        public void MakeEpTest()
        {
            BigInteger p = IntegerCalclator.RandamBigInteger(KEYLENGTH);
            Ep         P = new Ep(p);

            Console.Write("Ep.x: ");
            Console.WriteLine(P.x.ToString());
            Console.Write("Ep.y: ");
            Console.WriteLine(P.y.ToString());
        }
示例#2
0
        //BigInteger型乱数のテスト
        public void RandomBIntTest()
        {
            BigInteger p = IntegerCalclator.RandamBigInteger(KEYLENGTH);

            Console.Write("BigIntegerNumber: ");
            Console.WriteLine(p.ToString());
            string b = IntegerCalclator.BigIntegerToBin(p);

            Console.Write("Binary: ");
            Console.WriteLine(b);

            Console.WriteLine("Complete.");
        }
示例#3
0
        static public void Encrypt(byte[] m, out Ep M1, out Ep M2)
        {
            //平文mをEpに対応付ける
            Ep M = ECC.MsgToEp(new BigInteger(m));

            //Mに掛ける回数kを設定
            BigInteger k = IntegerCalclator.RandamBigInteger(Program.KEYLENGTH);

            //暗号文の生成
            M1 = k * ECC.P;
            Ep temp = k * ECC.B;

            M2 = temp + M;
        }
示例#4
0
        static public Ep MsgToEp(BigInteger m)
        {
            BigInteger z;

            for (int index = 0; index < 100; index++)
            {
                BigInteger x = (100 * m) + index;
                z = x ^ 3 + ECC._a * x + ECC._b;
                if (IntegerCalclator.LegendreSymbol(z, ECC._p))
                {
                    BigInteger y = IntegerCalclator.SqrtMod(z, ECC._p);
                    return(new Ep(x, y));
                }
            }

            //エラー
            return(new Ep());
        }
示例#5
0
        //a回の群演算
        public static Ep operator *(BigInteger a, Ep P)
        {
            Ep     Q   = P;
            String bin = IntegerCalclator.BigIntegerToBin(a);

            for (int item = bin.Length - 1; item >= 0; item--)
            {
                //桁数を上げる度に
                Q = Q + Q;
                if (bin[item] == 1)
                {
                    Q = Q + P;
                }
            }
            Q += P;

            return(Q);
        }
示例#6
0
        //コンストラスタ:初期位置を入力
        public Ep(BigInteger m)
        {
            BigInteger z;
            BigInteger y = 0;
            BigInteger x = 0;

            for (int index = 0; index < 100; index++)
            {
                x = (100 * m) + index;
                z = (x ^ 3) + (ECC.a * x) + ECC.b;                //z=y^2に注意
                if (IntegerCalclator.LegendreSymbol(z, ECC.p))
                {
                    y = IntegerCalclator.SqrtMod(z, ECC.p);
                    break;
                }
            }
            this.x = x;
            this.y = y;
        }
示例#7
0
        //公開鍵と秘密鍵をランダムに生成する
        public void MakeKey()
        {
            //ランダムに公開鍵Pを生成
            BigInteger p = IntegerCalclator.
                           RandamBigInteger(KEYLENGTH);
            Ep P = new Ep(p);

            //ランダムに秘密鍵Kpを生成
            BigInteger Kp = IntegerCalclator.
                            RandamBigInteger(KEYLENGTH);

            //PとKpから公開鍵Bを生成
            //(通常の乗算ではなく
            //Kp回の楕円曲線上の群演算をしている事に注意)
            Ep B = Kp * P;

            //出力
            FileManager.OutputPublicKey(B, P, "PublicKey.bin");
            FileManager.OutputPrivateKey(Kp, "PrivateKey.bin");
        }
示例#8
0
        //平方剰余計算テスト
        public void SqrtModTest()
        {
            Console.Write("a= ");
            string     strA = Console.ReadLine();
            BigInteger a    = BigInteger.Parse(strA);

            Console.Write("p= ");
            string     strP = Console.ReadLine();
            BigInteger p    = BigInteger.Parse(strP);

            if (IntegerCalclator.LegendreSymbol(a, p))
            {
                BigInteger x = IntegerCalclator.SqrtMod(a, p);
                Console.Write("Answer: ");
                Console.WriteLine(x.ToString());
            }
            else
            {
                Console.WriteLine("aはpの倍数か平方非剰余です");
            }
        }