Exemplo n.º 1
0
 private bool Examination(int P, int Q, int E, int D)
 {
     if (P == Q)
     {
         MessageBox.Show("Error: P = Q");
         return(false);
     }
     else if (P <= 1)
     {
         MessageBox.Show("Error: p <= 1");
         return(false);
     }
     else if (Q <= 1)
     {
         MessageBox.Show("Error: q <= 1");
         return(false);
     }
     else if (P * Q <= 256)
     {
         MessageBox.Show("Error: n is too small");
         return(false);
     }
     else if (E <= 1)
     {
         MessageBox.Show("Error: e <= 1");
         return(false);
     }
     else if (D <= 1)
     {
         MessageBox.Show("Error: d <= 1");
         return(false);
     }
     else if (!ModulMath.IsSimple(P))
     {
         MessageBox.Show("Error: P is not simple");
         return(false);
     }
     else if (!ModulMath.IsSimple(Q))
     {
         MessageBox.Show("Error: Q is not simple");
         return(false);
     }
     else if (ModulMath.Euclid(E, (P - 1) * (Q - 1)) != 1)
     {
         MessageBox.Show("Error: E and w(n) are not coprime");
         return(false);
     }
     else if (ModulMath.Euclid(D, (P - 1) * (Q - 1)) != 1)
     {
         MessageBox.Show("Error: D and w(n) are not coprime");
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        public static string Encipher(string PlainText, Key key)
        {
            char[] PlainTextChars = PlainText.ToCharArray();
            for (int i = 0; i < PlainTextChars.Length; i++)
            {
                PlainTextChars[i] = (char)ModulMath.FastExp((Int32)PlainTextChars[i], key.x, key.n);
            }
            string CipherText = new string(PlainTextChars);

            return(CipherText);
        }
Exemplo n.º 3
0
        private void Generate_Click(object sender, EventArgs e)
        {
            int E = Int32.Parse(eField.Text);
            int P = Int32.Parse(pField.Text);
            int Q = Int32.Parse(qField.Text);
            int D = ModulMath.EuclidEx(E, (P - 1) * (Q - 1)).x;

            if (!this.Examination(P, Q, E, D))
            {
                return;
            }
            this.dField.Text   = D.ToString();
            this.nField.Text   = (P * Q).ToString();
            this.w_nField.Text = ((P - 1) * (Q - 1)).ToString();
        }
Exemplo n.º 4
0
        public Form1()
        {
            InitializeComponent();
            this.pField.Text = 53.ToString();
            this.qField.Text = 61.ToString();
            this.eField.Text = 71.ToString();

            int E = Int32.Parse(eField.Text);
            int P = Int32.Parse(pField.Text);
            int Q = Int32.Parse(qField.Text);
            int D = ModulMath.EuclidEx(E, (P - 1) * (Q - 1)).x;

            if (!this.Examination(P, Q, E, D))
            {
                return;
            }
            this.dField.Text   = D.ToString();
            this.nField.Text   = (P * Q).ToString();
            this.w_nField.Text = ((P - 1) * (Q - 1)).ToString();
        }
Exemplo n.º 5
0
 public static int Encipher(int number, Key key)
 {
     return((char)ModulMath.FastExp(number, key.x, key.n));
 }