Beispiel #1
0
        public static PublicKey Generate(int p, int q)
        {
            var n   = p * q;
            var phi = (p - BigInt.BigInt.One) * (q - BigInt.BigInt.One);

            BigInt.BigInt e = 3;

            while (BigInt.BigInt.GreatestCommonDivisor(e, phi) != BigInt.BigInt.One)
            {
                e += BigInt.BigInt.One;
            }

            return(new PublicKey(e, n, phi));
        }
Beispiel #2
0
 public void BigIntSum()
 {
     int maxValue = 100;
     for (int i = 0; i < maxValue; i++)
     {
         for (int j = 0; j < maxValue; j++)
         {
             BigInt.BigInt first = new BigInt.BigInt(i.ToString());
             BigInt.BigInt second = new BigInt.BigInt(j.ToString());
             BigInt.BigInt resultSum = first + second;
             uint result = (uint) (i + j);
             Assert.AreEqual(result.ToString(), resultSum.Value);
         }
     }
 }
Beispiel #3
0
        public async Task <BigInt.BigInt[]> Encrypt(string text, PublicKey publicKey, IProgress <double> progress = default)
        {
            var textBytes = StringUtils.ConvertToNumbers(text.ToLower(), _alphabet);
            var encrypted = new BigInt.BigInt[textBytes.Length];

            await Task.Run(() =>
            {
                for (var i = 0; i < textBytes.Length; i++)
                {
                    encrypted[i] = RsaEncrypting.EncryptNumber(textBytes[i], publicKey);
                    progress?.Report(100.0 / text.Length *i);
                }

                progress?.Report(100);
            });

            return(encrypted);
        }
Beispiel #4
0
        public void TestBigValue()
        {
            // ------------------------------------------
            Stopwatch sw = Stopwatch.StartNew();
            string firstString = "1234567891234567891234567893216549872222222222222222222222222222222222222222222222222222222233333333333333333333333333333" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "99999999999999999999999999999999999999999999999999999" +
                                 "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
            string secondString = "555555555555555555555555555555555555555555555777777777777777777777772222222222222222222222222222222222222225465465465464654654646" +
                                  "555555555555555555555555555555555555555555555543242324444444444444444444444444444444444499999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999" +
                                  "";

            BigInteger bigIntegerFirst = BigInteger.Parse(firstString);
            BigInteger bigIntegerSecond = BigInteger.Parse(secondString);
            BigInteger expected = bigIntegerFirst + bigIntegerSecond;

            BigInt.BigInt firstBigINt = new BigInt.BigInt(firstString);
            BigInt.BigInt secondBigINt = new BigInt.BigInt(secondString);

            var result = firstBigINt + secondBigINt;

            sw.Stop();

            Assert.AreEqual(expected.ToString(), result.Value);

            //            Debug.WriteLine("{0} + {1}.  Time taken: {2}ms", i, j, sw.Elapsed.TotalMilliseconds);
        }
Beispiel #5
0
 public void BigIntConstructorSuccessTest()
 {
     string correctBigInt = "123456789111444444444444444444444444444444444";
     BigInt.BigInt bigInt = new BigInt.BigInt(correctBigInt);
 }
Beispiel #6
0
 public void BigIntConstructorFailTest()
 {
     string correctBigInt = "123456789fsf111'";
     BigInt.BigInt bigInt = new BigInt.BigInt(correctBigInt);
 }
Beispiel #7
0
 public static int DecryptNumber(BigInt.BigInt number, PrivateKey privateKey) =>
 int.Parse((BigInt.BigInt.Pow(number, privateKey.D) % privateKey.N).ToString());
Beispiel #8
0
 public PublicKey(BigInt.BigInt e, BigInt.BigInt n, BigInt.BigInt phi)
 {
     E   = e;
     N   = n;
     Phi = phi;
 }
 public PrivateKey(BigInt.BigInt d, BigInt.BigInt n)
 {
     D = d;
     N = n;
 }