private void encrypt_Click(object sender, RoutedEventArgs e) { KeyGen key = new KeyGen(); int p = key.GenerateP(); int g = key.GenerateG(p); int x = key.GenerateX(p); int y = key.FastExponentiation(p, g, x); cipher_a.Text = ""; cipher_b.Text = ""; string src = source.Text; Gamal elGamal = new Gamal(); int[] res = new int[2]; for (int i = 0; i < src.Length; i++) { int symbol = Convert.ToInt32(source.Text[i]); res = elGamal.Encrypt(p, g, y, symbol); cipher_a.Text += Convert.ToString(res[0]); cipher_b.Text += Convert.ToString(res[1]); cipher_a.Text += " "; cipher_b.Text += " "; } keyP.Text = Convert.ToString(p); keyG.Text = Convert.ToString(g); keyY.Text = Convert.ToString(y); keyX.Text = Convert.ToString(x); }
public char Decrypt(int p, int x, int a, int b) { KeyGen key = new KeyGen(); int buf = key.FastExponentiation((BigInteger)p, (BigInteger)a, (BigInteger)(p - 1 - x)); char message = (char)(key.Mul(b, buf, p)); return message; }
public int[] Encrypt(int p, int g, int y, int src) { KeyGen key = new KeyGen(); Random rnd = new Random(); int k = rnd.Next(1, p - 1); while (!key.IsCoprime(k, p - 1)) { k = rnd.Next(1, p - 1); } int m = src; int a = key.FastExponentiation((BigInteger)p, (BigInteger)g, (BigInteger)k); int b = key.FastExponentiation((BigInteger)p, (BigInteger)y, (BigInteger)k); b = key.Mul(b, m, p); int[] res = new int[2]; res[0] = a; res[1] = b; return res; }