/// <summary> /// Task 1,5. Generating of public and private keys. /// </summary> /// <param name="keyLength">Key length</param> /// <param name="keyChange">For p and q length cnanging</param> /// <returns>KeysHolder</returns> private static KeysHolder GenerateKeys(int keyLength, int keyChange) { var generator = new PrimeNumbersGenerator(); // generating of prime numbers var pNumeric = GeneratePrimeNumber(keyLength - keyChange, generator); var qNumeric = GeneratePrimeNumber(keyLength + keyChange, generator); Console.WriteLine(String.Format("Prime numbers p = {0} and q = {1} are generated.", pNumeric, qNumeric)); //// generating of composite numbers (on purpose) //var pNumeric = GenerateNotPrimeNumber(keyLength, generator); //var qNumeric = GenerateNotPrimeNumber(keyLength, generator); //Console.WriteLine(String.Format("Составные числа p = {0} и q = {1} сгенерированы.", pNumeric, qNumeric)); // Calculating public exp, private exp and cypher module var keysToReturn = CalculateKeysValues(keyLength, pNumeric, qNumeric, generator); FileWorker.CreateNEWFile(keysToReturn.publicKey, "public.txt"); FileWorker.CreateNEWFile(keysToReturn.privateKey, "private.txt"); FileWorker.CreateNEWFile(keysToReturn.cypherModule, "cyphermodule.txt"); Console.WriteLine("The keys are generated and placed in public.txt, private.txt, cyphermodule.txt"); return(keysToReturn); }
/// <summary> /// Generating decimal prime number /// </summary> /// <param name="keyLength">Key length </param> /// <returns>Big integer</returns> private static BigInteger GeneratePrimeNumber(int keyLength, PrimeNumbersGenerator generator) { List <short> mask = MaskGenerator.GetMask(keyLength / 2); // generating prime number var primeNumber = generator.Generate(mask); return(MathHelper.GetNumberFromList(primeNumber)); }
/// <summary> /// ПУНКТ 3. Генерация составного числа (проверка работоспособности шифра RSA с составными числами вместо простых /// </summary> /// <param name="keyLength">Длина ключа</param> /// <returns>Возвращает большое целое число</returns> private static BigInteger GenerateNotPrimeNumber(int keyLength, PrimeNumbersGenerator generator) { // Подготаваливаем затравку для последующей генерации в ней простого числа. List <short> zatravkaMaska = MaskGenerator.GetMask(keyLength / 2); // простое число var primeNumber = generator.GenerateNotPrime(zatravkaMaska); return(MathHelper.GetNumberFromList(primeNumber)); }
/// <summary> /// Getting public exp by phi and depending on key length /// </summary>Euler function</param> /// <param name="keyLength">Key length</param> /// <returns>Big integer</returns> public BigInteger GetPublicExp(BigInteger phi, int keyLength, PrimeNumbersGenerator generator) { int eLength = keyLength / 3; // e length var e = MaskGenerator.GetMask(eLength); BigInteger eNumeric = MathHelper.GetNumberFromList(e);; var isValid = false; // is number e valid? // Check if GCD(e, phi) equals one. If so, we found public exp isValid = MathHelper.EvklidAlgorithm(eNumeric, phi) == 1 ? true : false; //If number e in binary format has one in most fignificant bit and one in low bit and it is not valid then generate prime number while (!isValid) { e = generator.Generate(e); eNumeric = MathHelper.GetNumberFromList(e); // Check if GCD(e, phi) equals one. If so, we found public exp isValid = MathHelper.EvklidAlgorithm(eNumeric, phi) == 1 ? true : false; } return(eNumeric); }
/// <summary> /// Calculating public exp, private exp and cypher module /// </summary> /// <param name="keyLength">Key length</param> /// <param name="pNumeric">First decimal prime number</param> /// <param name="qNumeric">Second decimal prime number</param> /// <param name="generator">prime numbers generator</param> /// <returns>Возвращает структуру, хранящую ключи и модуль</returns> private static KeysHolder CalculateKeysValues(int keyLength, BigInteger pNumeric, BigInteger qNumeric, PrimeNumbersGenerator generator) { var keysToReturn = new KeysHolder(); // Cypher module var N = pNumeric * qNumeric; keysToReturn.cypherModule = N; // Euler function var phi = (pNumeric - 1) * (qNumeric - 1); var exponentHelper = new ExponentHelper(); // public exp var e = exponentHelper.GetPublicExp(phi, keyLength, generator); keysToReturn.publicKey = e; // private exp var d = exponentHelper.GetPrivateExp(e, phi); keysToReturn.privateKey = d; return(keysToReturn); }