コード例 #1
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);
            }
        }
コード例 #2
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);
        }