Ejemplo n.º 1
0
        public void ElGamalEnc2()
        {
            ElGamal     algorithm = new ElGamal();
            List <long> cipher    = algorithm.Encrypt(6323, 4736, 2231, 58, 111);//118

            Assert.AreEqual(cipher[0], 6066);
            Assert.AreEqual(cipher[1], 899);
        }
Ejemplo n.º 2
0
        public void ElGamalEnc1()
        {
            ElGamal     algorithm = new ElGamal();
            List <long> cipher    = algorithm.Encrypt(7187, 4842, 4464, 19, 19);//191

            Assert.AreEqual(cipher[0], 2781);
            Assert.AreEqual(cipher[1], 437);
        }
Ejemplo n.º 3
0
        public byte[] EncryptSessionInfo(ReadOnlySpan <byte> sessionInfo)
        {
            var encryptedData = elGamal.Encrypt(sessionInfo, RSAEncryptionPadding.Pkcs1);
            var g             = encryptedData.Slice(0, encryptedData.Length / 2);
            var p             = encryptedData.Slice(encryptedData.Length / 2);
            var mp            = new byte[MPInteger.GetMPEncodedLength(g) + MPInteger.GetMPEncodedLength(p)];

            MPInteger.TryWriteInteger(g, mp, out var gWritten);
            MPInteger.TryWriteInteger(p, mp.AsSpan(gWritten), out var _);
            return(mp);
        }
Ejemplo n.º 4
0
        public void Test_Elgamal_Encrypt_Simple()
        {
            // Arrange
            var nonce     = Constants.ONE_MOD_Q;
            var secret    = Constants.TWO_MOD_Q;
            var keyPair   = ElGamalKeyPair.FromSecret(secret);
            var publicKey = keyPair.PublicKey;
            var vote      = 1UL;

            // Act
            var ciphertext = ElGamal.Encrypt(vote, nonce, publicKey);
            var plaintext  = ciphertext.Decrypt(keyPair.SecretKey);

            // Assert
            Assert.That(plaintext == vote);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Encrypt the Master Key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void encryptMasterKeyButton_Click(object sender, EventArgs e)
        {
            string message = this.masterKeyTextBox.Text;

            if (String.IsNullOrWhiteSpace(message))
            {
                MessageBox.Show("A Master Key is Required", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (message.Length < 3)
            {
                MessageBox.Show("The Master Key Must be at Least 3 Characters Long", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    string cipherText;

                    switch (this.AsymmetricSchemeToUse)
                    {
                    case AsymmetricScheme.RSA:

                        RSA rsa = new RSA(this.PrimalityTestToUse, this.SecurityToken);
                        cipherText = rsa.Encrypt(message, this.OtherUsersRSAPublicKey);
                        this.encryptedMasterKeyLabel.Text = cipherText;

                        break;

                    case AsymmetricScheme.ElGamal:

                        ElGamal elGamal = new ElGamal(this.PrimalityTestToUse, this.SecurityToken);

                        Tuple <BigInteger, BigInteger> cipherText2 = elGamal.Encrypt(message, this.OtherUsersElGamalPublicKey);

                        this.gammaLabel.Text = cipherText2.Item1.ToString();
                        this.deltaLabel.Text = cipherText2.Item2.ToString();

                        this.encryptedMasterKeyLabel.Text = "(" + cipherText2.Item1.ToString() + " / " + cipherText2.Item2.ToString() + ")";

                        break;
                    }


                    // Encrypt the Master Key



                    this.requestPublicKeyButton.Enabled = false;
                    this.encryptMasterKeyButton.Enabled = false;
                    this.decryptMasterKeyButton.Enabled = false;
                    this.sendMasterKeyButton.Enabled    = true;
                    this.sendMasterKeyButton.Focus();
                    this.masterKeyLabel.Text      = this.masterKeyTextBox.Text;
                    this.masterKeyTextBox.Visible = false;
                    this.masterKeyLabel.Visible   = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }