예제 #1
0
        public void generateNewKeys(string keyName, string passphrase, int moduleSize)
        {
            BigInteger testValue = 0;

            do
            {
                p   = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2);
                q   = PrimeNumberGenerator.GeneratePrimeNumber(moduleSize / 2);
                n   = BigInteger.Multiply(p, q);
                phi = BigInteger.Multiply(BigInteger.Subtract(p, 1), BigInteger.Subtract(q, 1));

                e = PrimeNumberGenerator.GenerateMutuallyPrimeNumber(phi, moduleSize / 2);
                d = PrimeNumberGenerator.MultiplicativelyInverseNumber(phi, e);

                if (BigInteger.Compare(e, 0) < 0)
                {
                    continue;
                }
                if (BigInteger.Compare(d, 0) < 0)
                {
                    continue;
                }

                publicKey  = new RSAPublicKey(e, n);
                privateKey = new RSAPrivateKey(d, n);
                privateKey.EncryptByPassphrase(passphrase);

                testValue = privateKey.Decrypt(publicKey.Encrypt(TEST_VALUE));
            } while (!BigInteger.Equals(testValue, TEST_VALUE));

            _keyName = keyName;
        }
예제 #2
0
 public void Clear()
 {
     p          = 0;
     q          = 0;
     n          = 0;
     phi        = 0;
     e          = 0;
     d          = 0;
     privateKey = null;
     publicKey  = null;
     _keyName   = "";
 }
예제 #3
0
        public static RSAPrivateKey LoadFromFile(string path)
        {
            RSAPrivateKey privateKey = new RSAPrivateKey();

            try
            {
                using (StreamReader sr = File.OpenText(path))
                {
                    privateKey.encryptedD = sr.ReadLine();
                    privateKey.encryptedN = sr.ReadLine();
                }
            }
            catch (Exception)
            {
                throw new Exception("Неккоректный формат файла!");
            }

            return(privateKey);
        }