public static void SetPublicKey(PublicKey Key, int CharsPerBlockInput, int CharsPerBlockOutput) { _PublicKey = Key; _PlainTextCharsPerBlock = CharsPerBlockInput; _CipherCharactersPerBlock = CharsPerBlockOutput; _PublicKeySet = true; }
/// <summary> /// This should not return void /// </summary> public static RsaKey GenerateKeyPair(int K, int L) { // generate 2 random prime numbers P = Number.GetPrimeNumberInInterval(K, L, _Alphabet.Length); Q = Number.GetPrimeNumberInInterval(K, L, _Alphabet.Length); // compute n = pq N = BigInteger.Multiply(P, Q); int ck = K; int cl = L; while (N <= BigInteger.Pow(_Alphabet.Length, K) || N >= BigInteger.Pow(_Alphabet.Length, L)) { // generate 2 random prime numbers if (N <= BigInteger.Pow(_Alphabet.Length, K)) { cl++; ck++; } else { ck--; cl--; } P = Number.GetPrimeNumberInInterval(ck, cl, _Alphabet.Length); Q = Number.GetPrimeNumberInInterval(ck, cl, _Alphabet.Length); // compute n = pq N = BigInteger.Multiply(P, Q); } //{ // throw new ApplicationException("N nu e in interval "); //} // compute Phi(n) = (p - 1)(q - 1) Phi = BigInteger.Multiply(BigInteger.Subtract(P, BigInteger.One), BigInteger.Subtract(Q, BigInteger.One)); // randomly select 1 < e < Phi(n) E = Number.GetRandomNumberInIntervalGcdedWithNumberIsOne(BigInteger.One, Phi, N); // compute d = e ^ -1 mod phi //D = BigInteger.ModPow(E, Phi - 1, Phi); D = Number.ModInverse(E, Phi); // public key = (n, e) PublicKey publicKey = new PublicKey(N, E); // private key = d PrivateKey privateKey = new PrivateKey(D); RsaKey key = new RsaKey(publicKey, privateKey); _PrivateKeySet = _PublicKeySet = true; _PublicKey = publicKey; _PrivateKey = privateKey; _PlainTextCharsPerBlock = K; _CipherCharactersPerBlock = L; return key; }
public RsaKey(PublicKey Public, PrivateKey Private) { this.Private = Private; this.Public = Public; }