Esempio n. 1
0
        private void button_Encoder_Click(object sender, EventArgs e)
        {
            // Field fill check
            if (textBox_OriginalMessage.Text.Length <= 0 || textBox_OriginalSymmetricKey.Text.Length <= 0 ||
                textBox_P.Text.Length <= 0 || textBox_Q.Text.Length <= 0)
            {
                MessageBox.Show("Fields are not filled");
                return;
            }

            string message = textBox_OriginalMessage.Text;            // Message A
            string codedMessage;                                      // Encrypted message A

            string        symKey = textBox_OriginalSymmetricKey.Text; // Session key
            List <string> codedSymKey;                                // Encrypted session key

            // A: create session key
            byte[] keyByte   = ASCIIEncoding.ASCII.GetBytes(symKey);
            RC4    symEncode = new RC4(keyByte);


            // A: Encoding message A using a symmetric algorithm

            byte[] messageBytes     = ASCIIEncoding.ASCII.GetBytes(message);
            byte[] codedMessageByte = symEncode.Encode(messageBytes, messageBytes.Length);
            codedMessage            = Convert.ToBase64String(codedMessageByte);
            label_CodedMessage.Text = codedMessage;


            // B: Generate and display RSA public key

            Int64 p = Convert.ToInt64(textBox_P.Text);
            Int64 q = Convert.ToInt64(textBox_Q.Text);

            if (!asymEncoderDecoder.GeneratePublicAndPrivateKey(p, q))
            {
                //MessageBox.Show("Symmetric key is not encoded");
                return;
            }

            Int64 N = asymEncoderDecoder.GetN();

            label_N_public.Text = Convert.ToString("N: " + N);

            Int64 E = asymEncoderDecoder.GetE();

            label_E.Text = Convert.ToString("E: " + E);


            // A: Encrypt symmetric key with RSA public key

            codedSymKey = asymEncoderDecoder.Encrypt(symKey);
            label_CodedSymmetricalKey.ResetText();
            foreach (string item in codedSymKey)
            {
                label_CodedSymmetricalKey.Text += item + " ";
            }

            button_Decoder.Enabled = true;
        }
Esempio n. 2
0
 public byte[] Decode(byte[] allbuf, int len)
 {
     byte[] buf = new byte[len];
     Array.Copy(allbuf, buf, len);
     if (dec == null)
     {
         return(buf);
     }
     byte[] ret = new byte[buf.Length];
     for (int i = 0; i < ret.Length; i++)
     {
         ret[i] = dec.Encode(buf[i]);
     }
     return(ret);
 }
Esempio n. 3
0
        public byte[] Encode(byte[] allbuf)
        {
            if (enc == null)
            {
                return(allbuf);
            }

            byte[] buf = allbuf;
            buf = mppc.Pack(buf);
            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = enc.Encode(buf[i]);
            }

            return(buf);
        }