public void ElGamalDec2() { ElGamal algorithm = new ElGamal(); int plain = algorithm.Decrypt(6066, 899, 118, 6323); Assert.AreEqual(plain, 111); }
/// <summary> /// Encryptes the session key stored in the SessionKey property /// and saves the results in the EncryptedSessionKey property. /// </summary> /// <remarks>This method also calles EncodeSessionKey so that it /// does not have been called before calling EncryptSessionKey. /// <p></p> /// Please note: calling this function takes some time, because /// asymmetrical encryption takes some time! /// </remarks> /// <param name="pkpPacket">An PublicKeyPacket to which /// the sessionkey should be encrypted to.</param> public void EncryptSessionKey(PublicKeyPacket pkpPacket) { EncodeSessionKey(pkpPacket.KeyMaterial[0].bitCount()); AsymmetricCipher acCipher = new RSA(); switch (aaPublicAlgorithm) { case AsymAlgorithms.ElGama_Encrypt_Sign: case AsymAlgorithms.ElGamal_Encrypt_Only: acCipher = new ElGamal(); break; case AsymAlgorithms.RSA_Encrypt_Only: case AsymAlgorithms.RSA_Encrypt_Sign: acCipher = new RSA(); break; default: throw new System.Exception("The chosen public key algorithm is not yet implemented!"); } this.bIsUpdated = true; biEncryptedSessionKey = acCipher.Encrypt(new BigInteger(this.bEncodedSessionKey), pkpPacket); }
public void ElGamalDec1() { ElGamal algorithm = new ElGamal(); int plain = algorithm.Decrypt(2781, 437, 191, 7187); Assert.AreEqual(plain, 19); }
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); }
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); }
public static ElGamalKey CreatePublic( ReadOnlySpan <byte> source, out int publicKeySize) { var elgamalParameters = ReadOpenPgpPublicKey(source, out publicKeySize); return(new ElGamalKey(ElGamal.Create(elgamalParameters))); }
// ########################################################################################## /// <summary> Konstruktor obiektu klasy Server. </summary> /// <param name="username"> Nazwa serwera, która będzie zwracana jako informacja. </param> /// <param name="ip"> Adres protokołu internetowego serwera. </param> /// <param name="port"> Port protokołu sieciowego serwera. </param> public Server(string username, string ip, int port) { this.Username = username; this.ServerIP = ip; this.Port = port; this.encryptionServicesRSA = new ERSA(); this.encryptionServicesElGamal = new ElGamal(); }
private void encryptButton_Click(object sender, EventArgs e) { string message = this.plainTextTextBox.Text; if (String.IsNullOrWhiteSpace(message)) { MessageBox.Show("A Plain Text is Required", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { try { if (this.ReferenceToOtherAssignment3User.MyPublicKey == null) { MessageBox.Show("Request the Other Guy's Public Key", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { ElGamal elGamal = new ElGamal(this.PrimalityTestToUse, this.SecurityToken); List <Tuple <BigInteger, BigInteger> > cipherTextChunks = elGamal.EncryptWithChunking(message, this.ReferenceToOtherAssignment3User.MyPublicKey); this.CipherTextChunks = cipherTextChunks; if (cipherTextChunks.Count == 1) { this.gammaLabel.Text = cipherTextChunks[0].Item1.ToString(); this.deltaLabel.Text = cipherTextChunks[0].Item2.ToString(); this.cipherTextLabel.Text = "(" + cipherTextChunks[0].Item1.ToString() + "/" + cipherTextChunks[0].Item2.ToString() + ")"; } else { this.gammaLabel.Text = "Multiple Chunks..."; this.deltaLabel.Text = "Multiple Chunks..."; this.cipherTextLabel.Text = "Multiple Chunks..."; } this.sendCipherTextButton.Enabled = true; this.sendCipherTextButton.Focus(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
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); }
private void generateKeysButton_Click(object sender, EventArgs e) { elGamal = new ElGamal(this.PrimalityTestToUse, this.SecurityToken); elGamal.KeyGeneration(KeySize); ElGamalPublicKey publicKey = elGamal.PublicKey; this.pLabel.Text = publicKey.P.ToString(); this.alphaLabel.Text = publicKey.Alpha.ToString(); this.alphaToALabel.Text = publicKey.AlphaToA.ToString(); ElGamalPrivateKey privateKey = elGamal.PrivateKey; this.aLabel.Text = privateKey.A.ToString(); this.MyPublicKey = publicKey; this.MyPrivateKey = privateKey; }
public ElGamalPublicKey RequestElGamalPublicKey() { elGamal = new ElGamal(this.PrimalityTestToUse, this.SecurityToken); elGamal.KeyGeneration(KeySize); ElGamalPublicKey publicKey = elGamal.PublicKey; this.p2Label.Text = publicKey.P.ToString(); this.alphaLabel.Text = publicKey.Alpha.ToString(); this.alphaToALabel.Text = publicKey.AlphaToA.ToString(); ElGamalPrivateKey privateKey = elGamal.PrivateKey; this.aLabel.Text = privateKey.A.ToString(); //this.qLabel.Text = privateKey.Q.ToString(); //this.dLabel.Text = privateKey.D.ToString(); //this.phiLabel.Text = privateKey.Phi.ToString(); return(publicKey); }
public static ElGamalKey CreatePrivate( ReadOnlySpan <byte> password, ReadOnlySpan <byte> source, out int bytesRead) { var elgamalParameters = ReadOpenPgpPublicKey(source, out bytesRead); byte[] xArray = new byte[source.Length - bytesRead]; try { S2kBasedEncryption.DecryptSecretKey(password, source.Slice(bytesRead), xArray, out int bytesWritten); elgamalParameters.X = MPInteger.ReadInteger(xArray, out int xConsumed).ToArray(); bytesRead = source.Length; return(new ElGamalKey(ElGamal.Create(elgamalParameters))); } finally { CryptographicOperations.ZeroMemory(xArray); CryptographicOperations.ZeroMemory(elgamalParameters.X); } }
private BigInteger[][] GenerateEncryptionKey(int iKeySize) { ElGamal egKeyGenerator = new ElGamal(); return(egKeyGenerator.Generate(iKeySize)); }
/// <summary> /// Decrypts the session key stored in the EncryptedSessionKey /// property and saves the decrypted key in the EncodedSessionKey /// property. /// </summary> /// <remarks>This function also calls DecodeSessionKey so that the /// decrypted and decoded sessionkey is stored in the /// SessionKey property.</remarks> /// <param name="tskKey">A transportable secret key that is used to /// decrypt the encrypted session key.</param> /// <param name="strPassphrase">The passphrase used to decrypt the /// encrypted key material of the given transportable secret /// key.</param> public void DecryptSessionKey(TransportableSecretKey tskKey, string strPassphrase) { AsymmetricCipher acCipher = new RSA(); switch (aaPublicAlgorithm) { case AsymAlgorithms.ElGama_Encrypt_Sign: case AsymAlgorithms.ElGamal_Encrypt_Only: acCipher = new ElGamal(); break; case AsymAlgorithms.RSA_Encrypt_Only: case AsymAlgorithms.RSA_Encrypt_Sign: acCipher = new RSA(); break; default: throw new System.Exception("The chosen public key algorithm is not yet implemented!"); } bool bFound = false; SecretKeyPacket skpKey = new SecretKeyPacket(); IEnumerator ieSubkeys = tskKey.SubKeys.GetEnumerator(); while (ieSubkeys.MoveNext()) { if (!(ieSubkeys.Current is SecretKeyPacket)) { throw new System.Exception("Expected a secret key packet, but did not find one!"); } skpKey = (SecretKeyPacket)ieSubkeys.Current; if (skpKey.PublicKey.KeyID == lKeyID) { bFound = true; continue; } } // check if the message was encrypted with the primary key if (!bFound) { if (tskKey.PrimaryKey.PublicKey.KeyID == lKeyID) { skpKey = tskKey.PrimaryKey; } else { //theoretically we should never see this exception, as //encrytped message makes sure we only get fitting secret //keys, but just in case someone calls this directly, we //throw an exception throw new System.Exception("No fitting secret key found!"); } } BigInteger biKey = acCipher.Decrypt(this.biEncryptedSessionKey, skpKey, strPassphrase); this.bEncodedSessionKey = biKey.getBytes(); DecodeSessionKey(); }
public ElGamalKey(ElGamal elGamal) { this.elGamal = elGamal; }
/// <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); } } }