Example #1
0
        public void magmaTest(bool E)
        {
            /*
             * FEDCBA9876543210
             * FFEEDDCCBBAA99887766554433221100F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
             * 4EE901E5C2D8CA3D
             */
            string ref_plain = textBoxIn.Text;
            string ref_key   = textBoxKey.Text;

            if (ref_key.Length == 64)
            {
                if (ref_plain.Length == 16)
                {
                    Magma cipher = new Magma();
                    cipher.SetKey(Utils.StringToByteArray(ref_key));
                    if (E)
                    {
                        byte[] result = cipher.Encrypt(Utils.StringToByteArray(ref_plain));
                        textBoxOut.Text = Utils.ByteArrayToString(result);
                    }
                    else
                    {
                        byte[] result = cipher.Dencrypt(Utils.StringToByteArray(ref_plain));
                        textBoxOut.Text = Utils.ByteArrayToString(result);
                    }
                    //CollectionAssert.AreEqual(_ref_cipher, result);//сравнение 2 массивов
                }
                else
                {
                    MessageBox.Show("Введено не 64-битное выражение!");
                }
            }
            else
            {
                MessageBox.Show("Неверный размер ключа!");
            }
        }
Example #2
0
        public string Encrypt(string key, string text)
        {
            if (key.Length != 64)
            {
                throw new Exception("Ключ не верного размера!");
            }
            if (text.Length % 16 != 0)
            {
                while (text.Length % 16 != 0)
                {
                    text += "0";
                }
            }

            int kol = text.Length / 16;

            string[] str = new string[kol];
            string   k   = text;

            for (int i = 0; i < kol; i++)
            {
                k = text.Substring(16 * i, 16);
                //if(text.Length >= 16 * (i + 1) + 1)
                //    k = text.Remove(16*(i+1)+1);
                //k = k.Remove(0, );
                str[i] = k;
            }

            Magma  m   = new Magma();
            string ret = key;

            for (int i = 0; i < kol; i++)
            {
                m.SetKey(Utils.StringToByteArray(ret + ret + ret + ret));
                ret = Utils.ByteArrayToString(m.Encrypt(Utils.StringToByteArray(str[i])));
            }
            return(ret);
        }