Пример #1
0
        public RSA(int bits)
        {
            p = Helper.GenerateBigIntegerPrimes(bits);
            q = Helper.GenerateBigIntegerPrimes(bits);
            Console.WriteLine("p generated " + p);
            Console.WriteLine("q generated " + q);
            n = p.Multiply(q);
            Console.WriteLine("n = " + n);
            BigInteger p1 = p.Subtract(new BigInteger("1"));
            BigInteger q1 = q.Subtract(new BigInteger("1"));

            fn = p1.Multiply(q1);
            Console.WriteLine("Функция Эйлера = " + fn);
            int[]  er   = new[] { 17, 257, 65537 };
            Random rand = new Random((int)System.DateTime.Now.Ticks);

            e = new BigInteger(er[rand.Next(0, er.Length)].ToString());
            Console.WriteLine("e = " + e);

            d = e.ModInverse(fn);
            Console.WriteLine("d = " + d);

            Console.WriteLine("Public Key: " + e + ", " + n);
            Console.WriteLine("Private Key: " + d + ", " + n);
        }
Пример #2
0
        public Rsa()
        {
            _rnd = new Random();

            _p = BigInteger.ProbablePrime(BitLength, _rnd);
            _q = BigInteger.ProbablePrime(BitLength, _rnd);

            var fi = _p.Subtract(BigInteger.One).Multiply(_q.Subtract(BigInteger.One));

            _e = CalculateE(fi);
            _d = CalculateD(_e, fi);
        }
Пример #3
0
        static public Org.BouncyCastle.Math.BigInteger GenerateBigIntegerPrimes(int bits)
        {
            Org.BouncyCastle.Security.SecureRandom ran = new Org.BouncyCastle.Security.SecureRandom();
            Org.BouncyCastle.Math.BigInteger       c   = new Org.BouncyCastle.Math.BigInteger(bits, ran);

            for (; ;)
            {
                if (c.IsProbablePrime(100) == true)
                {
                    break;
                }
                c = c.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            }
            return(c);
        }
Пример #4
0
        static public Org.BouncyCastle.Math.BigInteger genpr2(int bits, long initialRand)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            Org.BouncyCastle.Security.SecureRandom ran = new Org.BouncyCastle.Security.SecureRandom(
                new Org.BouncyCastle.Math.BigInteger(initialRand.ToString()).ToByteArray());
#pragma warning restore CS0618 // Type or member is obsolete

            Org.BouncyCastle.Math.BigInteger c = new Org.BouncyCastle.Math.BigInteger(bits, ran);

            for (;;)
            {
                if (c.IsProbablePrime(1) == true)
                {
                    break;
                }

                c = c.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            }
            return(c);
        }
Пример #5
0
        /// <summary>
        /// Check is the key is valid see RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt
        /// </summary>
        public static bool IsValidPublicKey(Org.BouncyCastle.Math.BigInteger y, Org.BouncyCastle.Math.BigInteger p, Org.BouncyCastle.Math.BigInteger q)
        {
            Org.BouncyCastle.Math.BigInteger bn;

            // y must lie in [2,p-1]
            // check y < 2 then failed
            bn = new Org.BouncyCastle.Math.BigInteger("2");
            if (y.CompareTo(bn) < 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at least 2");
                return(false);
            }

            // y must lie in [2,p-1]
            bn = new Org.BouncyCastle.Math.BigInteger(p.ToString());
            bn = bn.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            if (y.CompareTo(bn) > 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at most p-2");
                return(false);
            }


            // Verify with Sophie-Germain prime
            //
            // This is a nice test to make sure the public key position is calculated
            // correctly. This test will fail in about 50% of the cases if applied to
            // random data.
            bn = y.ModPow(q, p);
            if (bn.CompareTo(new Org.BouncyCastle.Math.BigInteger("1")) != 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key does not fulfill y^q mod p = 1");
                return(false);
            }

            return(true);
        }
Пример #6
0
        private void Mine()
        {
            MinerInfo.minerStillRunning = true;


            var fromBiginteger  = new Org.BouncyCastle.Math.BigInteger(Helper.ConvertBigintegerHexToDecimal(from, hex));
            var untilBiginteger = new Org.BouncyCastle.Math.BigInteger(Helper.ConvertBigintegerHexToDecimal(until, hex));
            var difference      = fromBiginteger.Subtract(untilBiginteger).Abs();

            if (Helper.CompareNumbers(difference.ToString(), long.MaxValue.ToString()) > 0)
            {
                System.Windows.Forms.MessageBox.Show($"The length of job cant be more than {long.MaxValue}! Please use a smaller job length!");
                MinerInfo.minerStillRunning = false;
                return;
            }
            MinerInfo.lengthOfJob = Math.Abs(untilBiginteger.LongValue - fromBiginteger.LongValue);
            Task.Factory.StartNew(() =>
            {
                MinerInfo.minerThreadInfo = "Lookup size determine,generating hashset...";

                lookupSet = new HashSet <string>(10000);

                using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() != -1)
                    {
                        lookupSet.Add(sr.ReadLine());
                    }
                }
                stopwatch.Start();
                MinerInfo.minerThreadInfo = "";
                MinerInfo.minerThreadInfo = "Lookup size determine, generating hashset done, mining...";

                while (fromBiginteger.CompareTo(untilBiginteger) < 0 && increment || fromBiginteger.CompareTo(untilBiginteger) > 0 && !increment)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    var key = KeyPair.Create(fromBiginteger, compressed);
                    if (lookupSet.Contains(key.AddressBase58))
                    {
                        MinerInfo.minerThreadInfo = $"Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}";
                        foundKeyCount++;
                        if (form.InvokeRequired)
                        {
                            form.Invoke((MethodInvoker) delegate { textBox.Text += $"{Environment.NewLine}Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}, privatekey: {key.PrivateKey}{Environment.NewLine}"; });
                        }
                        else
                        {
                        }
                        StreamWriter streamWriter = new StreamWriter("keys.txt", true);
                        string keyline            = $"Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}, privatekey: {key.PrivateKey}";
                        streamWriter.WriteLine(keyline);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    if (increment)
                    {
                        fromBiginteger = fromBiginteger.Add(new Org.BouncyCastle.Math.BigInteger("1"));
                    }
                    else
                    {
                        fromBiginteger = fromBiginteger.Add(new Org.BouncyCastle.Math.BigInteger("-1"));
                    }
                    MinerInfo.currentlyProcessed++;
                    MinerInfo.countOfTriedKeys++;
                    if (MinerInfo.currentlyProcessed % 1000 == 0)
                    {
                        MinerInfo.minerThreadInfo = $"Currently Processed number: {fromBiginteger.ToString()}";
                    }
                }
                //int lengthOfOneJob = 0;
                //if (MinerInfo.lengthOfJob > 10000)
                //{
                //    lengthOfOneJob = 10000;
                //}
                //else
                //{
                //    lengthOfOneJob = (int)MinerInfo.lengthOfJob;
                //}
                //StringBuilder stringBuilder = new StringBuilder(chars.Length);
                //for (int i = 0; i < chars.Length; i++)
                //{
                //    stringBuilder.Append(chars[0]);
                //}
                //List<string> sequenceList = new List<string>(lengthOfOneJob);
                //var list = GetAllMatches(chars.ToCharArray(), lengthOfString);
                //foreach (var item in list)
                //{
                //    if (cancellationToken.IsCancellationRequested)
                //    {
                //        break;
                //    }
                //    sequenceList.Add(item);
                //    if (sequenceList.Count == lengthOfOneJob)
                //    {
                //        LookupKeys(sequenceList);
                //        sequenceList.Clear();
                //    }
                //    MinerInfo.currentlyProcessed++;
                //    if (MinerInfo.currentlyProcessed >= MinerInfo.lengthOfJob && MinerInfo.lengthOfJob > 10000)
                //    {
                //        LookupKeys(sequenceList);
                //        break;
                //    }
                //}
                //int count = list.Count();

                MinerInfo.minerThreadResults = $"{Environment.NewLine}Found keys: {foundKeyCount}, ckecked numbers: {MinerInfo.countOfTriedKeys} {Environment.NewLine}Additional data: - Combinations: {MinerInfo.lengthOfJob}, elapsed time: {stopwatch.Elapsed}, keys/second: {MinerInfo.countOfTriedKeys / stopwatch.Elapsed.TotalSeconds}";
                Dispose();
                MinerInfo.minerStillRunning = false;
            }, TaskCreationOptions.LongRunning);
        }
Пример #7
0
        /// <summary>
        /// Check is the key is valid see RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt
        /// </summary>
        public static bool IsValidPublicKey(Org.BouncyCastle.Math.BigInteger y, Org.BouncyCastle.Math.BigInteger p, Org.BouncyCastle.Math.BigInteger q)
        {
            Org.BouncyCastle.Math.BigInteger bn;

            // y must lie in [2,p-1]
            // check y < 2 then failed
            bn = new Org.BouncyCastle.Math.BigInteger("2");
            if (y.CompareTo(bn) < 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at least 2");
                return false;
            }

            // y must lie in [2,p-1]
            bn = new Org.BouncyCastle.Math.BigInteger(p.ToString());
            bn = bn.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            if (y.CompareTo(bn) > 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at most p-2");
                return false;
            }


            // Verify with Sophie-Germain prime
            //
            // This is a nice test to make sure the public key position is calculated
            // correctly. This test will fail in about 50% of the cases if applied to
            // random data.
            bn = y.ModPow(q, p);
            if (bn.CompareTo(new Org.BouncyCastle.Math.BigInteger("1")) != 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key does not fulfill y^q mod p = 1");
                return false;
            }

            return true;
        }