Exemplo n.º 1
0
        private void btnEncode_Click(object sender, EventArgs e)
        {
            string res = string.Empty;

            if (algorithm.GetType() != typeof(SHA2))
            {
                res = Encoding.Default.GetString(
                    algorithm.Crypt(Encoding.Default.GetBytes(tbData.Text))
                    );
            }
            else
            {
                byte[] hash = algorithm.Crypt(Encoding.Default.GetBytes(tbData.Text));


                for (int i = 0; i < hash.Length; i++)
                {
                    res += string.Format("{0:X2}", hash[i]);
                }
            }

            tbDecode.Text = res;
        }
Exemplo n.º 2
0
        private void OnClickedItem(object sender, ToolStripItemClickedEventArgs e)
        {
            if (algoritham.GetType() != ((ICryptoLibrary)e.ClickedItem.Tag).GetType())
            {
                this.algoritham = (ICryptoLibrary)e.ClickedItem.Tag;
                byte[] key;
                if (this.algoritham.GetType() != typeof(SHA2))
                {
                    key = this.algoritham.GenerateRandomKey();
                    btnRandomGenerateKey.Enabled = true;
                    btnSetKey.Enabled            = true;
                    btnDecode.Enabled            = true;
                    btnDecryptAll.Enabled        = true;
                    btnDecryptSeleced.Enabled    = true;
                    btnDecodeFromFile.Enabled    = true;
                }
                else
                {
                    key = ((SHA2)this.algoritham).GetKey();
                    btnRandomGenerateKey.Enabled = false;
                    btnSetKey.Enabled            = false;
                    btnDecode.Enabled            = false;
                    btnDecryptAll.Enabled        = false;
                    btnDecryptSeleced.Enabled    = false;
                    btnDecodeFromFile.Enabled    = false;
                }

                if (this.algoritham.GetType() != typeof(Knapsack))
                {
                    gbKnapSackProp.Enabled = false;
                    //tbKey.Enabled = true;
                    tbKey.KeyPress -= this.tbKey_KeyPress;
                    lbHint.Visible  = false;
                }
                else
                {
                    // set up for knapsack
                    gbKnapSackProp.Enabled = true;
                    //tbKey.Enabled = false;
                    tbKey.KeyPress += this.tbKey_KeyPress;

                    tbN.Text       = ((Knapsack)this.algoritham).n.ToString();
                    tbM.Text       = ((Knapsack)this.algoritham).m.ToString();
                    tbIM.Text      = ((Knapsack)this.algoritham).mInverse.ToString();
                    lbHint.Visible = true;
                }

                // encoding problem fixed like this :)
                if (this.algoritham.GetType() != typeof(Knapsack))
                {
                    this.tbKey.Text = new string(Encoding.Default.GetChars(key));
                }
                else
                {
                    uint[] temp       = new uint[key.Length / 4];
                    string tempKeyStr = string.Empty;

                    for (
                        int i = 0; i < key.Length; i += 4)
                    {
                        temp[i / 4] = BitConverter.ToUInt32(key, i);
                        tempKeyStr += " " + BitConverter.ToUInt32(key, i);
                    }

                    this.tbKey.Text = tempKeyStr;
                    //this.tbKey.Text = string.Join(", ", key.Select(x => x.ToString()).ToArray());
                }
                this.gbAlgorithm.Text = this.algoritham.ToString();
            }
        }