Example #1
0
        public void decrypt(ref byte[] cipher)
        {
            #region Origin aglorigth
            //BigInteger c = new BigInteger(crypt);
            //BigInteger e = MathUlti.fastExponent(c, SECRET_KEY, N);
            //System.Array.Clear(crypt, 0, crypt.Length);
            //e.ToByteArray().CopyTo(crypt, 0);
            //return crypt;
            #endregion

            //System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();

            //Chinese Remainder Theorem
            BigInteger c = new BigInteger(cipher);
            //Debug.showLog("1", sw.ElapsedMilliseconds);
            m1 = MathUlti.fastExponent(c, dP, p);
            //Debug.showLog("2", sw.ElapsedMilliseconds);
            m2 = MathUlti.fastExponent(c, dQ, q);
            //Debug.showLog("3", sw.ElapsedMilliseconds);
            h = (m1 > m2) ? (qInv * (m1 - m2)) % p : ((qInv * (m1 - m2 + p)) % p);
            m = m2 + (h * q);
            //Debug.showLog("4", sw.ElapsedMilliseconds);
            System.Array.Clear(cipher, 0, cipher.Length);
            //Debug.showLog("5", sw.ElapsedMilliseconds);
            m.ToByteArray().CopyTo(cipher, 0);
            //Debug.showLog("6", sw.ElapsedMilliseconds);
            //sw.Stop();
        }
Example #2
0
        public void encrypt(ref byte[] plain)
        {
            BigInteger e = new BigInteger(plain);
            BigInteger c = MathUlti.fastExponent(e, PUBLIC_KEY, N);

            System.Array.Clear(plain, 0, plain.Length);
            c.ToByteArray().CopyTo(plain, 0);
        }
Example #3
0
 public void testIsPrime()
 {
     Assert.AreEqual(MathUlti.isPrime(17, 10), true, "isPrime wrong");
     Assert.AreEqual(MathUlti.isPrime(23, 10), true, "isPrime wrong");
     Assert.AreEqual(MathUlti.isPrime(45, 10), false, "isPrime wrong");
     Assert.AreEqual(MathUlti.isPrime(53, 10), true, "isPrime wrong");
     Assert.AreEqual(MathUlti.isPrime(71, 10), true, "isPrime wrong");
 }
Example #4
0
 public void testFastExponent()
 {
     for (int i = 0; i < input.Length / 3; i += 3)
     {
         a = MathUlti.fastExponent(input[i], input[i + 1], input[i + 2]);
         b = BigInteger.ModPow(input[i], input[i + 1], input[i + 2]);
         Assert.AreEqual(a, b, "fastExponent wrong");
     }
 }
Example #5
0
        public bool readKeyStorage(string keyFile)
        {
            try
            {
                using (StreamReader sr = new StreamReader(keyFile))
                {
                    string   line    = sr.ReadToEnd();
                    string[] element = line.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                    for (int i = 0; i < element.Length; i++)
                    {
                        switch (element[i])
                        {
                        case "PUBLIC_KEY:": PUBLIC_KEY = BigInteger.Parse(element[i + 1], NumberStyles.AllowHexSpecifier); break;

                        case "SECRET_KEY:": SECRET_KEY = BigInteger.Parse(element[i + 1], NumberStyles.AllowHexSpecifier); break;

                        case "N:": N = BigInteger.Parse(element[i + 1], NumberStyles.AllowHexSpecifier); break;

                        // Chinese Remainder Theorem
                        case "p:": p = BigInteger.Parse(element[i + 1], NumberStyles.AllowHexSpecifier); break;

                        case "q:": q = BigInteger.Parse(element[i + 1], NumberStyles.AllowHexSpecifier); break;
                        }
                    }
                    if (p < q)
                    {
                        MathUlti.swapBigInteger(ref p, ref q);
                    }
                    dP   = MathUlti.moduloInverse(PUBLIC_KEY, p - 1);
                    dQ   = MathUlti.moduloInverse(PUBLIC_KEY, q - 1);
                    qInv = MathUlti.moduloInverse(q, p);

                    KEY_SIZE = (N.ToByteArray().Length) * 8;
                }

                Debug.showLog("dP", dP);
                Debug.showLog("dQ", dQ);
                Debug.showLog("N", N);
                Debug.showLog("KEY_SIZE", KEY_SIZE);
                Debug.showLog("PUBLIC_KEY", PUBLIC_KEY);
                Debug.showLog("SECRET_KEY", SECRET_KEY);

                return(true);
            }
            catch (Exception e)
            {
                Debug.showLog("func readKeyStorage", e.Message);
                return(false);
            }
        }
Example #6
0
        private void generateKeyPair()
        {
            p = MathUlti.generatePrime(KEY_SIZE / 2);
            q = MathUlti.generatePrime(KEY_SIZE / 2);
            if (p < q)
            {
                MathUlti.swapBigInteger(ref p, ref q);
            }
            N          = BigInteger.Multiply(p, q);
            n          = BigInteger.Multiply(p - 1, q - 1);
            PUBLIC_KEY = 65537;//MathUlti.getPublicKey(KEY_SIZE, n);
            SECRET_KEY = MathUlti.moduloInverse(PUBLIC_KEY, n);

            dP   = MathUlti.moduloInverse(PUBLIC_KEY, p - 1);
            dQ   = MathUlti.moduloInverse(PUBLIC_KEY, q - 1);
            qInv = MathUlti.moduloInverse(q, p);

            Debug.showLog("dP", dP);
            Debug.showLog("dQ", dQ);
            Debug.showLog("N", N);
            Debug.showLog("PUBLIC_KEY", PUBLIC_KEY);
            Debug.showLog("SECRET_KEY", SECRET_KEY);
        }