private BigInteger GeneratePrimeNumber() { BigInteger number = random.Next(1000, 5000); while (!GCD.IsSimple(number)) { number = random.Next(1000, 5000); } return(number); }
public string Decrypt(BigInteger[,] encryptedText) { byte[] decryptedText = new byte[encryptedText.GetUpperBound(0) + 1]; for (int i = 0; i < encryptedText.GetUpperBound(0) + 1; i++) { BigInteger inverseA = GCD.ModInverse(BigInteger.Pow(encryptedText[i, 0], (int)x), p); decryptedText[i] = Convert.ToByte(BigInteger.ModPow(BigInteger.Multiply(encryptedText[i, 1], inverseA), 1, p).ToString()); } return(Encoding.ASCII.GetString(decryptedText)); }
private BigInteger GenerateCoprimeNumber(BigInteger coprime) { BigInteger number = random.Next(2, (int)coprime - 1), nod = GCD.GetNOD(number, coprime); while (nod != 1) { number = random.Next(2, (int)coprime - 1); nod = GCD.GetNOD(number, coprime); } return(number); }
public RSA() { p = GeneratePrimeNumber(); q = GeneratePrimeNumber(); n = p * q; fi = (p - 1) * (q - 1); e = GenerateCoprimeNumber(fi); d = GCD.ModInverse(e, fi); Console.WriteLine(new string('-', 40) + "RSA" + new string('-', 40)); Console.WriteLine($"p = {p}, q = {q}, n = p * q = {p * q}, fi(n) = {fi}, e = {e}, d = {d}\n"); Console.WriteLine($"PUBLIC KEY: (e, n) = ({e}, {n})"); Console.WriteLine($"PRIVATE KEY: (d, n) = ({d}, {n})"); Console.WriteLine(); }