/// <summary> /// Create instance of SimpleElGamal class and count private key. /// Base and modulo should be large enough to correctly encrypt and decrypt given messages. /// </summary> /// <param name="exponentAlice">Exponent of Alice</param> /// <param name="exponentBob">Exponent of Bob</param> /// <param name="base">Shared base value. Must be larger than modulo.</param> /// <param name="modulo">Shared modulo. Must be less than base.</param> public SimpleElGamal(ushort exponentAlice, ushort exponentBob, ushort @base, ushort modulo) { if (@base >= modulo) { throw new ArgumentException("Base shouldn't be larger than modulo"); } Modulo = modulo; var halfKeyAlice = NumericUtilities.SolveModulo(@base, exponentAlice, modulo); var halfKeyBob = NumericUtilities.SolveModulo(@base, exponentBob, modulo); // Discrete logarithm problem. // In real situation Alice wouldn't know about Bob's exponent and vice versa. // They change only "half parts of their own keys". var aliceKey = NumericUtilities.SolveModulo(halfKeyBob, exponentAlice, modulo); var bobKey = NumericUtilities.SolveModulo(halfKeyAlice, exponentBob, modulo); if (aliceKey != bobKey) { throw new Exception("Alice's and Bob's keys are not equal!"); } Key = aliceKey; }
/// <summary> /// Decrypt value. /// </summary> /// <param name="value">Value to decrypt</param> /// <returns>Decrypted value</returns> public uint Decrypt(uint value) => NumericUtilities.SolveModulo(value, PrivateKey, Modulo);
/// <summary> /// Encrypt value. /// </summary> /// <param name="value">Value to encrypt</param> /// <returns>Encrypted value</returns> public uint Encrypt(uint value) => NumericUtilities.SolveModulo(value, PublicKey, Modulo);