// generate keys for ESD private void textButtonGenerateKeys_Click(object sender, EventArgs e) { int p, q, m, d, n, x, y; double g; string hashAlgorithm = GetHashAlgorithm(); string RSA = "RSA"; string DSA = "DSA"; try { if (String.Compare(hashAlgorithm, RSA) == 0) // hash algorithm is RSA { p = Convert.ToInt32(textBoxP.Text); q = Convert.ToInt32(textBoxQ.Text); if (RSAProvider.IsTheNumberSimple(p) && RSAProvider.IsTheNumberSimple(q)) // P and Q is simple numbers? { textBoxD.Visible = true; labelD.Visible = true; textBoxN.Visible = true; labelN.Visible = true; m = (p - 1) * (q - 1); n = RSAProvider.CalculateN(p, q); d = RSAProvider.CalculateD(m); textBoxD.Text = d.ToString(); textBoxN.Text = n.ToString(); } else // P or Q is not simple numbers { MessageBox.Show("Numbers is 'p' or 'q' is not simple!", "Warning!"); textBoxP.Text = ""; textBoxQ.Text = ""; textBoxD.Text = ""; textBoxN.Text = ""; } } if (String.Compare(hashAlgorithm, DSA) == 0) // hash algorithm is DSA { q = Convert.ToInt32(textBoxQ.Text); if (DSAProvider.IsTheNumberSimple(q)) // P is simple number? { textBoxP.Visible = true; labelP.Visible = true; textBoxD.Visible = true; labelD.Visible = true; textBoxN.Visible = true; labelN.Visible = true; hash = DSAProvider.GetSHA256Hash(textBoxTextTransmission.Text); Random rand = new Random(); x = rand.Next(0, q); p = DSAProvider.CalculateP(q); g = DSAProvider.CalculateG(p, q); y = DSAProvider.CalculateY(p, g, x); string ESD = DSAProvider.Encode(hash, p, q, g, x); bool result = DSAProvider.DecodeAndEqual(hash, p, q, y, g, ESD); if (!result) // if keys gives wrong results { throw new Exception(); } textBoxP.Text = p.ToString(); textBoxD.Text = y.ToString(); textBoxN.Text = x.ToString() + " " + g.ToString(); } else // Q is not simple number { MessageBox.Show("Numbers is 'q' is not simple!", "Warning!"); textBoxP.Text = ""; textBoxQ.Text = ""; textBoxD.Text = ""; textBoxN.Text = ""; } } } catch (Exception exception) { textButtonGenerateKeys.PerformClick(); // generate new ESD Console.WriteLine(exception + "\n\n" + exception.Message); } }
// transfer of information to the server private void textButtonTransmitInfo_Click(object sender, EventArgs e) { int p, q, d, m, e_, n, g, x, y; string hashAlgorithm = ""; string RSA = "RSA"; string DSA = "DSA"; if (!CheckInputText()) { textButtonTransmitInfo.Visible = false; MessageBox.Show("Please enter text for transmission"); } else // checking input text is true { try { if (client != null) { hashAlgorithm = GetHashAlgorithm(); if (String.Compare(hashAlgorithm, RSA) == 0) // hash algorithm is RSA { // hashing text hash = RSAProvider.GetMd5Hash(textBoxTextTransmission.Text); textBoxHashedText.Text = hash; // variables for doing ESD p = Convert.ToInt32(textBoxP.Text); q = Convert.ToInt32(textBoxQ.Text); d = Convert.ToInt32(textBoxD.Text); n = Convert.ToInt32(textBoxN.Text); m = (p - 1) * (q - 1); e_ = RSAProvider.CalculateE(d, m); ESD = RSAProvider.Encode(hash, e_, n); // transfer of information to the server client.TransmitInfo(hash, ESD, d, n, hashAlgorithm); } if (String.Compare(hashAlgorithm, DSA) == 0) // hash algorithm is DSA { // hashing text hash = DSAProvider.GetSHA256Hash(textBoxTextTransmission.Text); textBoxHashedText.Text = hash; // variables for doing ESD p = Convert.ToInt32(textBoxP.Text); q = Convert.ToInt32(textBoxQ.Text); y = Convert.ToInt32(textBoxD.Text); string[] data = textBoxN.Text.Split(new Char[] { ' ' }); x = Convert.ToInt32(data[0]); g = Convert.ToInt32(data[1]); ESD = DSAProvider.Encode(hash, p, q, g, x); // transfer of information to the server client.TransmitInfo(hash, ESD, q, y, g, hashAlgorithm); } textButtonCheckInfo.Visible = true; } else { MessageBox.Show("Not connected to server"); } } catch (Exception exception) { MessageBox.Show(exception + "\n\n" + exception.Message); } } }