Esempio n. 1
0
        //сложение точки P c собой же
        public static ECPoint Double(ECPoint p)
        {
            ECPoint p2 = new ECPoint();
            p2.a = p.a;
            p2.b = p.b;
            p2.FieldChar = p.FieldChar;

            BigInteger dy = 3 * p.x * p.x + p.a;
            BigInteger dx = 2 * p.y;

            if (dx < 0)
                dx += p.FieldChar;
            if (dy < 0)
                dy += p.FieldChar;

            BigInteger m = (dy * modInverse(dx, p.FieldChar)) % p.FieldChar;
            p2.x = (m * m - p.x - p.x) % p.FieldChar;
            p2.y = (m * (p.x - p2.x) - p.y) % p.FieldChar;
            if (p2.x < 0)
                p2.x += p.FieldChar;
            if (p2.y < 0)
                p2.y += p.FieldChar;

            return p2;
        }
Esempio n. 2
0
        //сложение двух точек P1 и P2
        public static ECPoint operator +(ECPoint leftPoint, ECPoint rightPoint)
        {
            ECPoint resultPoint = new ECPoint();
            resultPoint.a = leftPoint.a;
            resultPoint.b = leftPoint.b;
            resultPoint.FieldChar = leftPoint.FieldChar;

            BigInteger dy = rightPoint.y - leftPoint.y;
            BigInteger dx = rightPoint.x - leftPoint.x;

            if (dx < 0)
                dx += leftPoint.FieldChar;
            if (dy < 0)
                dy += leftPoint.FieldChar;

            BigInteger m = (dy * modInverse(dx, leftPoint.FieldChar)) % leftPoint.FieldChar;
            if (m < 0)
                m += leftPoint.FieldChar;
            resultPoint.x = (m * m - leftPoint.x - rightPoint.x) % leftPoint.FieldChar;
            resultPoint.y = (m * (leftPoint.x - resultPoint.x) - leftPoint.y) % leftPoint.FieldChar;
            if (resultPoint.x < 0)
                resultPoint.x += leftPoint.FieldChar;
            if (resultPoint.y < 0)
                resultPoint.y += leftPoint.FieldChar;
            return resultPoint;
        }
Esempio n. 3
0
        //сложение двух точек P1 и P2
        public static ECPoint operator +(ECPoint p1, ECPoint p2)
        {
            ECPoint p3 = new ECPoint();
            p3.a = p1.a;
            p3.b = p1.b;
            p3.FieldChar = p1.FieldChar;

            BigInteger dy = p2.y - p1.y;
            BigInteger dx = p2.x - p1.x;

            if (dx < 0)
                dx += p1.FieldChar;
            if (dy < 0)
                dy += p1.FieldChar;

            BigInteger m = (dy * dx.modInverse(p1.FieldChar)) % p1.FieldChar;
            if (m < 0)
                m += p1.FieldChar;
            p3.x = (m * m - p1.x - p2.x) % p1.FieldChar;
            p3.y = (m * (p1.x - p3.x) - p1.y) % p1.FieldChar;
            if (p3.x < 0)
                p3.x += p1.FieldChar;
            if (p3.y < 0)
                p3.y += p1.FieldChar;
            return p3;
        }
Esempio n. 4
0
 public ECPoint(ECPoint p)
 {
     x = p.x;
     y = p.y;
     a = p.a;
     b = p.b;
     FieldChar = p.FieldChar;
 }
Esempio n. 5
0
 //Восстанавливаем координату y из координаты x и бита четности y 
 private ECPoint GDecompression()
 {
     byte y = xG[0];
     byte[] x=new byte[xG.Length-1];
     Array.Copy(xG, 1, x, 0, xG.Length - 1);
     BigInteger Xcord = new BigInteger(x);
     BigInteger temp = (Xcord * Xcord * Xcord + a * Xcord + b) % p;
     BigInteger beta = ModSqrt(temp, p);
     BigInteger Ycord = new BigInteger();
     if ((beta % 2) == (y % 2))
         Ycord = beta;
     else
         Ycord = p - beta;
     ECPoint G = new ECPoint();
     G.a = a;
     G.b = b;
     G.FieldChar = p;
     G.x = Xcord;
     G.y = Ycord;
     this.G = G;
     return G;
 }
Esempio n. 6
0
 //проверяем подпись 
 public bool SingVer(byte[] H, string sing, ECPoint Q)
 {
     string Rvector = sing.Substring(0, n.bitCount() / 4);
     string Svector = sing.Substring(n.bitCount() / 4, n.bitCount() / 4);
     BigInteger r = new BigInteger(Rvector, 16);
     BigInteger s = new BigInteger(Svector, 16);
     if ((r < 1) || (r > (n - 1)) || (s < 1) || (s > (n - 1)))
         return false;
     BigInteger alpha = new BigInteger(H);
     BigInteger e = alpha % n;
     if (e == 0)
         e = 1;
     BigInteger v = e.modInverse(n);
     BigInteger z1 = (s * v) % n;
     BigInteger z2 = n + ((-(r * v)) % n);
     this.G = GDecompression();
     ECPoint A = ECPoint.multiply(z1, G);
     ECPoint B = ECPoint.multiply(z2, Q);
     ECPoint C = A + B;
     BigInteger R = C.x % n;
     if (R == r)
         return true;
     else
         return false;
 }
Esempio n. 7
0
 //подписываем сообщение
 public string SingGen(byte[] h, BigInteger d)
 {
     BigInteger alpha = new BigInteger(h);
     BigInteger e = alpha % n;
     if (e == 0)
         e = 1;
     BigInteger k = new BigInteger();
     ECPoint C=new ECPoint();
     BigInteger r=new BigInteger();
     BigInteger s = new BigInteger();
     do
     {
         do
         {
             k.genRandomBits(n.bitCount(), new Random());
         } while ((k < 0) || (k > n));
         C = ECPoint.multiply(k, G);
         r = C.x % n;
         s = ((r * d) + (k * e)) % n;
     } while ((r == 0)||(s==0));
     string Rvector = padding(r.ToHexString(),n.bitCount()/4);
     string Svector = padding(s.ToHexString(), n.bitCount() / 4);
     return Rvector + Svector;
 }
Esempio n. 8
0
        //умножение точки на число x, по сути своей представляет x сложений точки самой с собой
        public static ECPoint Multiply(BigInteger x, ECPoint p)
        {
            ECPoint temp = p;
            x = x - 1;
            while (x != 0)
            {

                if ((x % 2) != 0)
                {
                    if ((temp.x == p.x) || (temp.y == p.y))
                        temp = Double(temp);
                    else
                        temp = temp + p;
                    x = x - 1;
                }
                x = x / 2;
                p = Double(p);
            }
            return temp;
        }
 public override string Encrypt(string text)
 {
     p = new BigInteger("6277101735386680763835789423207666416083908700390324961279", 10);
     a = new BigInteger("-3", 10);
     b = new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16);
     xG = FromHexStringToByte("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012");
     n = new BigInteger("ffffffffffffffffffffffff99def836146bc9b1b4d22831", 16);
     BigInteger d = GenPrivateKey(192);
     Q = GenPublicKey(d);
     GOST hash = new GOST(256);
     byte[] H = hash.GetHash(Encoding.Default.GetBytes(text));
     signature = GenerateSignature(H, d);
     return signature;
 }