Пример #1
0
        /// <summary>
        /// EBC模式解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_EBCDecrypt_Click(object sender, RoutedEventArgs e)
        {
            if (textBox.Text == string.Empty)
            {
                System.Windows.MessageBox.Show("请选择待加密或解密的文件!!", "提示");
                return;
            }
            else
            {
                Secretkey secretket_form = new Secretkey();
                if (secretket_form.ShowDialog().Value == true)
                {
                    SM4Utils sm4 = new SM4Utils();
                    //sm4.secretKey = "JeF8U9wHFOMfs2Y8";//密钥

                    sm4.secretKey = Keyto16bytes(secretket_form.key);

                    sm4.hexString = false;
                    byte[] bytedata;
                    bytedata = FiletoByte(textBox.Text);
                    string plain_data = sm4.Decrypt_ECB(Encoding.Default.GetString(bytedata));
                    if (plain_data == string.Empty)
                    {
                        System.Windows.MessageBox.Show("输入的密钥不正确!!", "提示");
                        return;
                    }
                    else
                    {
                        byte[]         plain_bytedata = Encoding.Default.GetBytes(plain_data);
                        string         extension      = System.IO.Path.GetExtension(textBox.Text);
                        SaveFileDialog sfd            = new SaveFileDialog();
                        sfd.Title  = "保存解密文件";
                        sfd.Filter = "All files|*" + extension;
                        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                            fs.Write(plain_bytedata, 0, plain_bytedata.Length);
                            textBox_DePath.Clear();
                            textBox_DePath.AppendText(" 解密文件路径:" + sfd.FileName);
                            fs.Close();
                            System.Windows.MessageBox.Show("已成功解密文件!!", "提示");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("请保存明文文件!!", "提示");
                            return;
                        }
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("请输入并确认密钥!!", "提示");
                    return;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// CBC模式加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_CBCEncrypt_Click(object sender, RoutedEventArgs e)
        {
            if (textBox.Text == string.Empty)
            {
                System.Windows.MessageBox.Show("请选择待加密或解密的文件!!", "提示");
                return;
            }
            else
            {
                Secretkey secretket_form = new Secretkey();
                if (secretket_form.ShowDialog().Value == true)
                {
                    SM4Utils sm4 = new SM4Utils();
                    //sm4.secretKey = "JeF8U9wHFOMfs2Y8";

                    sm4.secretKey = Keyto16bytes(secretket_form.key);

                    sm4.hexString = false;
                    tempiv        = GetRandomString(16, true, true, true, false, null).ToLower();
                    sm4.iv        = tempiv;
                    //sm4.iv = "UISwD9fW6cFh9SNS";
                    byte[] bytedata;
                    bytedata = FiletoByte(textBox.Text);
                    string cipher_data        = sm4.Encrypt_CBC(Encoding.Default.GetString(bytedata));
                    string cipher_data_iv     = sm4.iv + cipher_data;
                    byte[] cipher_bytedata    = Encoding.Default.GetBytes(cipher_data);
                    byte[] cipher_bytedata_iv = Encoding.Default.GetBytes(cipher_data_iv);

                    string         extension = System.IO.Path.GetExtension(textBox.Text);
                    SaveFileDialog sfd       = new SaveFileDialog();
                    sfd.Title  = "保存加密文件";
                    sfd.Filter = "All files|*" + extension;
                    if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                        fs.Write(cipher_bytedata_iv, 0, cipher_bytedata_iv.Length);
                        textBox_EnPath.Clear();
                        textBox_EnPath.AppendText(" 加密文件路径:" + sfd.FileName);
                        fs.Close();
                        System.Windows.MessageBox.Show("已成功加密文件!!", "提示");
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("请保存密文文件!!", "提示");
                        return;
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("请输入一个密钥以用于加密!!", "提示");
                    return;
                }
            }
        }