Exemplo n.º 1
0
        public byte[] SignData(Dictionary <string, object> privateKey, byte[] data)
        {
            var d = BigInt.Parse(privateKey.GetString("d"));
            var n = BigInt.Parse(privateKey.GetString("n"));

            var m = new BigInt(data);

            var s      = BigInt.ModPow(m, d, n);
            var result = s.ToByteArray();

            return(result);
        }
Exemplo n.º 2
0
        public byte[] Encrypt(Dictionary <string, object> publicKey, byte[] data)
        {
            var e = BigInt.Parse(publicKey.GetString("e"));
            var n = BigInt.Parse(publicKey.GetString("n"));

            var m = new BigInt(data);

            var c      = BigInt.ModPow(m, e, n);
            var result = c.ToByteArray();

            return(result);
        }
Exemplo n.º 3
0
        public byte[] Decrypt(Dictionary <string, object> privateKey, byte[] data)
        {
            var d = BigInt.Parse(privateKey.GetString("d"));
            var n = BigInt.Parse(privateKey.GetString("n"));

            var c = new BigInt(data);

            var m      = BigInt.ModPow(c, d, n);
            var result = m.ToByteArray();

            return(result);
        }
Exemplo n.º 4
0
        public static bool IsProbablyPrime(this BigInteger value, int witnesses = 10)
        {
            if (value <= 1 || value.IsEven)
            {
                return(false);
            }

            if (witnesses <= 0)
            {
                witnesses = 10;
            }

            BigInteger d = value - 1;
            int        s = 0;

            while (d % 2 == 0)
            {
                d /= 2;
                s += 1;
            }

            Byte[]     bytes = new Byte[value.ToByteArray().LongLength];
            BigInteger a;

            for (int i = 0; i < witnesses; i++)
            {
                do
                {
                    Rng.GetBytes(bytes);

                    a = new BigInteger(bytes);
                } while (a < 2 || a >= value - 2);

                BigInteger x = BigInteger.ModPow(a, d, value);
                if (x == 1 || x == value - 1)
                {
                    continue;
                }

                for (int r = 1; r < s; r++)
                {
                    x = BigInteger.ModPow(x, 2, value);

                    if (x == 1)
                    {
                        return(false);
                    }
                    if (x == value - 1)
                    {
                        break;
                    }
                }

                if (x != value - 1)
                {
                    return(false);
                }
            }

            return(true);
        }