Beispiel #1
0
        public void DoHandshake(string signedPrime, string signedGenerator)
        {
            if (IsInitiator) return;

            byte[] signedPrimeAsBytes = HexToBytes(signedPrime);
            Rsa.Verify(ref signedPrimeAsBytes);

            byte[] signedGeneratorAsBytes = HexToBytes(signedGenerator);
            Rsa.Verify(ref signedGeneratorAsBytes);

            DhPrime = new BigInteger(Encoding.Default.GetString(signedPrimeAsBytes), 10);
            DhGenerator = new BigInteger(Encoding.Default.GetString(signedGeneratorAsBytes), 10);

            if (DhPrime <= 2)
                throw new Exception("Prime cannot be <= 2!\nPrime: " + DhPrime);

            if (DhGenerator >= DhPrime)
                throw new Exception($"Generator cannot be >= Prime!\nPrime: {DhPrime}\nGenerator: {DhGenerator}");

            DhPrivate = new BigInteger(RandomHex(30), _bitSize);
            DhPublic = DhGenerator.ModPow(DhPrivate, DhPrime);
        }
Beispiel #2
0
        public void DoHandshake(Bitmap banner, string token)
        {
            IsBannerHandshake = true;
            var bannerData = new byte[banner.Width * banner.Height * 4];
            for (int y = 0, i = 0; y < banner.Height; y++)
            {
                for (int x = 0; x < banner.Width; x++)
                {
                    int pixelArgb = banner.GetPixel(x, y).ToArgb();
                    bannerData[i++] = (byte)((pixelArgb >> 24) & 255);
                    bannerData[i++] = (byte)((pixelArgb >> 16) & 255);
                    bannerData[i++] = (byte)((pixelArgb >> 8) & 255);
                    bannerData[i++] = (byte)(pixelArgb & 255);
                }
            }

            string bannerChunk = Xor(Decode(bannerData), token);
            int bannerSize = bannerChunk[0];
            bannerChunk = bannerChunk.Substring(1);
            DhPrime = new BigInteger(bannerChunk.Substring(0, bannerSize), 10);

            bannerChunk = bannerChunk.Substring(bannerSize);
            bannerSize = bannerChunk[0];
            bannerChunk = bannerChunk.Substring(1);
            DhGenerator = new BigInteger(bannerChunk.Substring(0, bannerSize), 10);

            DhPrivate = new BigInteger(RandomHex(30), _bitSize);
            DhPublic = DhGenerator.ModPow(DhPrivate, DhPrime);
        }
Beispiel #3
0
        public byte[] GetSharedKey(string publicKey)
        {
            if (!IsBannerHandshake)
            {
                byte[] paddedPublicKeyAsBytes = HexToBytes(publicKey);
                if (IsInitiator) Rsa.Decrypt(ref paddedPublicKeyAsBytes);
                else Rsa.Verify(ref paddedPublicKeyAsBytes);

                publicKey = Encoding.Default.GetString(paddedPublicKeyAsBytes);
            }

            var unpaddedPublicKey = new BigInteger(publicKey, 10);
            return unpaddedPublicKey.ModPow(DhPrivate, DhPrime).ToBytes();
        }
Beispiel #4
0
        /// <summary>
        /// Probabilistic prime test based on Rabin-Miller's test.
        /// </summary>
        /// <param name="confidence">The amount of times/iterations to check the primality of the current instance.</param>
        /// <returns>true if the current instance is a strong pseudo-prime to randomly chosen bases, otherwise false if not prime.</returns>
        public bool RabinMillerTest(int confidence)
        {
            BigInteger thisVal;
            if ((_data[MAX_LENGTH - 1] & 0x80000000) != 0)
                thisVal = -this;
            else
                thisVal = this;

            if (thisVal.DataLength == 1)
            {
                if (thisVal._data[0] == 0 || thisVal._data[0] == 1)
                    return false;
                else if (thisVal._data[0] == 2 || thisVal._data[0] == 3)
                    return true;
            }

            if ((thisVal._data[0] & 0x1) == 0)
                return false;

            BigInteger p_sub1 = thisVal - (new BigInteger(1));
            int s = 0;

            for (int index = 0; index < p_sub1.DataLength; index++)
            {
                uint mask = 0x01;

                for (int i = 0; i < 32; i++)
                {
                    if ((p_sub1._data[index] & mask) != 0)
                    {
                        index = p_sub1.DataLength;
                        break;
                    }
                    mask <<= 1;
                    s++;
                }
            }

            BigInteger t = p_sub1 >> s;

            int bits = thisVal.BitCount();
            BigInteger a = new BigInteger();
            Random rand = new Random();

            for (int round = 0; round < confidence; round++)
            {
                bool done = false;

                while (!done)
                {
                    int testBits = 0;

                    while (testBits < 2)
                        testBits = (int)(rand.NextDouble() * bits);

                    a.GenRandomBits(testBits, rand);

                    int byteLen = a.DataLength;

                    if (byteLen > 1 || (byteLen == 1 && a._data[0] != 1))
                        done = true;
                }

                BigInteger gcdTest = a.Gcd(thisVal);
                if (gcdTest.DataLength == 1 && gcdTest._data[0] != 1)
                    return false;

                BigInteger b = a.ModPow(t, thisVal);

                bool result = false;

                if (b.DataLength == 1 && b._data[0] == 1)
                    result = true;

                for (int j = 0; result == false && j < s; j++)
                {
                    if (b == p_sub1)
                    {
                        result = true;
                        break;
                    }

                    b = (b * b) % thisVal;
                }

                if (result == false)
                    return false;
            }
            return true;
        }
Beispiel #5
0
 private BigInteger DoPublic(BigInteger x) => x.ModPow(E, N);
Beispiel #6
0
        private BigInteger DoPrivate(BigInteger x)
        {
            if (P == null && Q == null)
                return x.ModPow(D, N);

            BigInteger xp = (x % P).ModPow(Dmp1, P);
            BigInteger xq = (x % Q).ModPow(Dmq1, Q);

            while (xp < xq) xp = xp + P;
            return ((((xp - xq) * (Iqmp)) % P) * Q) + xq;
        }