Beispiel #1
0
        //DES加密
        private void btnEncrypt2_Click(object sender, EventArgs e)
        {
            if (txtDesKey.Text.Trim() == "")
            {
                MessageBox.Show("请先生成DESKey密钥!");
                return;
            }
            if (txtDesKey.Text.Length != 8)
            {
                MessageBox.Show("DESKey密钥长度为8位!");
                return;
            }
            if (chkBoxIsIV.Enabled && chkBoxIsIV.Checked && !checkIV(txtIV.Text))
            {
                MessageBox.Show("DES加密向量为8个字节(格式:8个可显ASCII或0x开头的16个HEX符)!");
                return;
            }
            string      ivStr        = chkBoxIsIV.Enabled && chkBoxIsIV.Checked ? txtIV.Text : "";
            PaddingMode paddingMode1 = cmbPaddingMode.SelectedIndex <= 0 ? PaddingMode.PKCS7 : (PaddingMode)(cmbPaddingMode.SelectedIndex + 1);

            txtCiphertext2.Text = DESCrypt.Encrypt(txtDesKey.Text, txtPlaintext2.Text, ivStr, cipherMode1, paddingMode1);
        }
Beispiel #2
0
        private void crackDES()
        {
            do
            {
                if (counter > total)
                {
                    timerCrackState.Stop();
                    InvokeCtrl.UpdateCtrlText(txtDesKey, desKey);
                    InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen);

                    MessageBox.Show("任务结束,未找到密钥!");
                    InvokeCtrl.UpdateCtrlEnabled(btnCrack, true);
                    InvokeCtrl.UpdateCtrlEnabled(btnPause, false);
                    InvokeCtrl.UpdateCtrlEnabled(btnStop, false);
                    InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true);
                    LogHelper.Log(string.Format("任务结束,用时[{0}],未找到密钥!", InvokeCtrl.GetCtrlText(lbSeconds)));
                    break;
                }

                /*
                 * 1、假设密钥由md5生成(即字符范围[0-9A-F]),那么8位的密钥范围可以看作0x00000000-0xFFFFFFFF
                 * 2、假设md5生成字符统一大写或小写,那么需要考虑切换大小写再加密
                 * 2、假设加密向量与密钥相同(否则计算量超出本算法范围。。)
                 */
                desKey = counter.ToString("x8");

                counter++;
                prgValue = Convert.ToInt32(10000 * counter / total);
                //InvokeCtrl.UpdateCtrlText(lbCounter, counter.ToString());

                //InvokeCtrl.UpdateCtrlText(txtDesKey, desKey);
                cipherGen = DESCrypt.Encrypt(desKey, plaintext, desKey);
                //InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen);

                if (cipherGen.Equals(ciphertext))
                {
                    timerCrackState.Stop();
                    InvokeCtrl.UpdateCtrlText(txtDesKey, desKey);
                    InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen);

                    MessageBox.Show("恭喜你,找到密钥:" + cipherGen);
                    InvokeCtrl.UpdateCtrlEnabled(btnCrack, true);
                    InvokeCtrl.UpdateCtrlEnabled(btnPause, false);
                    InvokeCtrl.UpdateCtrlEnabled(btnStop, false);
                    InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true);
                    LogHelper.Log(string.Format("在进度[{0}]处,用时[{1}],找到密钥:{2}", InvokeCtrl.GetCtrlText(prgBar1), InvokeCtrl.GetCtrlText(lbSeconds), cipherGen));
                    break;
                }

                string desKeyUpcase = desKey.ToUpper();
                if (desKeyUpcase.Equals(desKey))
                {
                    continue;
                }
                desKey = desKeyUpcase;

                //InvokeCtrl.UpdateCtrlText(txtDesKey, desKey);
                cipherGen = DESCrypt.Encrypt(desKey, plaintext, desKey);
                //InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen);

                if (cipherGen.Equals(ciphertext))
                {
                    timerCrackState.Stop();
                    InvokeCtrl.UpdateCtrlText(txtDesKey, desKey);
                    InvokeCtrl.UpdateCtrlText(txtCipherGen, cipherGen);

                    MessageBox.Show("恭喜你,找到密钥:" + cipherGen);
                    InvokeCtrl.UpdateCtrlEnabled(btnCrack, true);
                    InvokeCtrl.UpdateCtrlEnabled(btnPause, false);
                    InvokeCtrl.UpdateCtrlEnabled(btnStop, false);
                    InvokeCtrl.UpdateCtrlEnabled(txtPlaintext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtCiphertext, true);
                    InvokeCtrl.UpdateCtrlEnabled(txtThreadNum, true);
                    LogHelper.Log(string.Format("在进度[{0}]处,用时[{1}],找到密钥:{2}", InvokeCtrl.GetCtrlText(prgBar1), InvokeCtrl.GetCtrlText(lbSeconds), cipherGen));
                    break;
                }
            } while (true);
        }