public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey) { var p = Subscriber.P; var g = Subscriber.G; var q = Subscriber.Q; var x = Subscriber.ClosedKey; var y = Subscriber.OpenedKey; var k = PrimeNumberGenerator.GeneratePrimeNumber(1, (q - 1).ToInt32()); // 3; //hash = 9; var r = (g.ВСтепень(k) % p) % q; var inverseK = (CryptoFormula.АлгоритмЕвклида(q, k, out var euErr) ?? throw new Exception(euErr)); var s = inverseK * (hash + ((x * r) % q)) % q; var result = new DSAResultOfEncryptionHash(r, s, Subscriber.OpenedKey, hash) { P = Subscriber.P.Value, Q = Subscriber.G.Value, G = Subscriber.Q.Value, K = k, X = Subscriber.ClosedKey.Value, }; return(result); }
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; }
public static (BigInteger P, BigInteger G) GenerateKeys(Random random) { PrimeNumberGenerator.PrimeRandom = random; var p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000); // (1000000, int.MaxValue); // var g = random.Next(2, p - 1); return(p, g); }
private void BtSetRandomSubscriberParams(object sender, EventArgs e) { CatchException(() => { tbClosedKey.Text = PrimeNumberGenerator .GeneratePrimeNumber(3, Convert.ToInt32(tbParamQ.Text) - 1, _random) .ToString(); tbSubscriberName.Text = HelpfulMethods.GetRandomName(_random); }); }
public IResultOfEncryptionHash EncryptHashCode(BigInteger hash, WrappedInteger recipientOpenedKey) { var p = Subscriber.P; var g = Subscriber.G; //var k = 9; WrappedInteger k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32()); while (CryptoFormula.НайтиНОД(k, p - 1) != 1) { k = PrimeNumberGenerator.GeneratePrimeNumber(1, (p - 2).ToInt32()); } k %= p - 1; hash %= p - 1; var a = g.ВСтепень(k) % p; var b = (CryptoFormula.АлгоритмЕвклида(p - 1, k, out var euErr) ?? throw new Exception(euErr)) * ((p - 1) + (hash - ((Subscriber.ClosedKey * a) % (p - 1)))) % (p - 1); return(new ElgamalResultOfEncryptionHash(a, b, Subscriber.OpenedKey, hash)); }
public static (BigInteger P, BigInteger Q, BigInteger G) GenerateParams(Random random) { PrimeNumberGenerator.PrimeRandom = random; var p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000); // (1000000, int.MaxValue); // var q = (p - 1).азложитьНаПростыеМножители().Max(); var h = random.Next(1, p - 2); var getG = new Func <BigInteger>(() => { return(h.ВСтепень((p - 1) / q) % p); }); var g = getG(); while (g < 2) { p = PrimeNumberGenerator.GeneratePrimeNumber(100, 1000); q = (p - 1).азложитьНаПростыеМножители().Max(); h = random.Next(1, p - 2); g = getG(); } return(p, q, g); }
private void BtSetRandomSubscriberParams(object sender, EventArgs e) { tbClosedKey.Text = PrimeNumberGenerator.GeneratePrimeNumber(10, 100, _random).ToString(); tbSubscriberName.Text = HelpfulMethods.GetRandomName(_random); }