C# AES解密&;加密

C# AES解密&;加密,c#,mysql,encryption,utf-8,ascii,C#,Mysql,Encryption,Utf 8,Ascii,我合并了这个代码来加密我的数据。 不幸的是,始终存在以下错误: 输入不是有效的Base 64字符串,因为它包含非Base 64字符、两个以上的空格或空格中的无效字符 这是我的代码--> 这是我的登录表单--> private void按钮登录单击(对象发送者,事件参数) { if(textboxusername.Text.Length private void按钮注册\单击(对象发送者,事件参数e) { if(textboxusername.Text.Length

我合并了这个代码来加密我的数据。 不幸的是,始终存在以下错误:

输入不是有效的Base 64字符串,因为它包含非Base 64字符、两个以上的空格或空格中的无效字符

这是我的代码-->

这是我的登录表单-->

private void按钮登录单击(对象发送者,事件参数)
{
if(textboxusername.Text.Length<2 | | textboxpassword.Text.Length<4)
{
Show(“用户名或密码太短!”,“确定”);
}
其他的
{
mysqlcon;
con=新的MySqlConnection(myConnectionString);
尝试
{
con.Open();
string exists=$“如果不存在,则创建表`userlogin`.`userlogin`”+
$“(`id`INT非空自动增量,`username`VARCHAR(64)”+
$“不为空,`password`VARCHAR(64)不为空,`prename`VARCHAR(64)”+
$“不为空,`LANSAME`VARCHAR(64)不为空,`EmailAddress`VARCHAR(64)”+
$“不为空,主键(`id`)引擎=InnoDB;”;
MySqlCommand cmd=新的MySqlCommand(存在,con);
cmd.ExecuteNonQuery();
字符串user=AesCrypt.Encrypt(textboxusername.Text);
string pass=AesCrypt.Encrypt(textboxpassword.Text);
字符串encusr=$“从userlogin中选择*,其中username='{user}';”;
字符串encpass=$“从userlogin中选择*,其中密码='{pass}';”;
字符串decusr=AesCrypt.Decrypt(encusr);
字符串depass=AesCrypt.Decrypt(encpass);
if(decusr==textboxusername.Text&&decpass==textboxpassword.Text)
{
FormMsbOk.Show(“您以用户身份成功登录:+textboxusername.Text,“确定”);
con.Close();
this.Hide();
var main=new FormMain();
main.Close+=(s,args)=>this.Close();
main.Show();
}
其他的
{
textboxusername.Clear();
textboxpassword.Clear();
Show(“错误用户名或密码错误!”,“确定”);
}
}
捕获(异常nocon)
{
textboxusername.Clear();
textboxpassword.Clear();
FormMsbOk.Show(“无法打开连接!”+nocon.Message,“确定”);
}
这是我的登记表-->

private void按钮注册\单击(对象发送者,事件参数e)
{
if(textboxusername.Text.Length<2 | | textboxpassword.Text.Length<4)
{
FormMsbOk.Show(“用户名或密码太短!”+
“用户名的最小值为2个字符,对于”+
“密码为4个字符。”,“确定”);
}
其他的
{
mysqlcon;
con=新的MySqlConnection(myConnectionString);
尝试
{
con.Open();
string exists=$“如果不存在,则创建表`userlogin`.`userlogin`”+
$“(`id`INT非空自动增量,`username`VARCHAR(64)”+
$“不为空,`password`VARCHAR(64)不为空,`prename`VARCHAR(64)”+
$“不为空,`LANSAME`VARCHAR(64)不为空,`EmailAddress`VARCHAR(64)”+
$“不为空,主键(`id`)引擎=InnoDB;”;
MySqlCommand emdexsts=新的MySqlCommand(存在,con);
emdexists.ExecuteNonQuery();
字符串encusr=AesCrypt.Encrypt(textboxusername.Text);
字符串encpass=AesCrypt.Encrypt(textboxpassword.Text);
字符串encprename=AesCrypt.Encrypt(textboxprename.Text);
字符串enclastname=AesCrypt.Encrypt(textboxlastname.Text);
字符串encemail=AesCrypt.Encrypt(textboxemail.Text);
字符串insert=$“插入到`userlogin`.`userlogin`”+
$“(`username`、`password`、`prename`、`name`、`emailAddress`)+
$“值(“+encusr+”、“+encpass+”、“+encprename+”、”+
“““+enclastname+”,“+EncelastMail+”;”;
MySqlCommand cmdinsert=新的MySqlCommand(insert,con);
cmdinsert.ExecuteNonQuery();
con.Close();
Show(“Registriert”,“Ok”);
textboxusername.Clear();
textboxpassword.Clear();
textboxprename.Clear();
textboxlastname.Clear();
textboxrepeat.Clear();
textboxemail.Clear();
}
捕获(异常nocon)
{
FormMsbOk.Show(“无法打开连接!”+nocon.Message,“确定”);
}
}

查看
字符串decusr=AesCrypt.Decrypt(encusr);
并在该行上使用断点查看
encusr
在该点的值

您正在将一个包含SQL查询的字符串传递给
AesCrypt.Decrypt
方法,该方法期望获得一个要解密的加密值。您可能希望它处理运行该查询的结果,而不是查询本身

其他提示:

  • MySqlConnection
    MySqlCommand
    都是
    IDisposable
    的,所以每一个都应该在
    using
    块中。一旦这样做,您就不必担心关闭连接,因为退出using块将处理连接,这将调用Close。请注意,即使您的code在块内抛出异常
  • 如果使用字符串连接来构造查询,那么它容易受到SQL注入的攻击
        public static string IV = "abababababababab";  // 16 chars = 128 bytes
        public static string Key = "abababababababababababababababab";   // 32 chars = 256 bytes
        public static string Encrypt(string decrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
            AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
            encdec.BlockSize = 128;
            encdec.KeySize = 256;
            encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            encdec.Padding = PaddingMode.PKCS7;
            encdec.Mode = CipherMode.CBC;
    
            ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
    
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
            icrypt.Dispose();
    
            return Convert.ToBase64String(enc);
        }
    
        public static string Decrypt(string encrypted)
        {
            byte[] encbytes = Convert.FromBase64String(encrypted);
            AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
            encdec.BlockSize = 128;
            encdec.KeySize = 256;
            encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            encdec.Padding = PaddingMode.PKCS7;
            encdec.Mode = CipherMode.CBC;
    
            ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);
    
            byte[] dec = icrypt.TransformFinalBlock(encbytes, 0, encbytes.Length);
            icrypt.Dispose();
    
            return ASCIIEncoding.ASCII.GetString(dec);
        }
    
            private void buttonlogin_Click(object sender, EventArgs ex)
        {
            if (textboxusername.Text.Length < 2 || textboxpassword.Text.Length < 4)
            {
                FormMsbOk.Show("Username or Password is too short!","Ok");
            }
            else
            {
                MySqlConnection con;
                con = new MySqlConnection(myConnectionString);
                try
                {
                    con.Open();
                    string exists = $"CREATE TABLE IF NOT EXISTS `userlogin`.`userlogin` " +
                    $"( `id` INT NOT NULL AUTO_INCREMENT , `username` VARCHAR(64)" +
                    $" NOT NULL , `password` VARCHAR(64) NOT NULL , `prename` VARCHAR(64)" +
                    $" NOT NULL , `surname` VARCHAR(64) NOT NULL , `emailadress` VARCHAR(64)" +
                    $" NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;";
                    MySqlCommand cmd = new MySqlCommand(exists, con);
                    cmd.ExecuteNonQuery();
    
                    string user = AesCrypt.Encrypt(textboxusername.Text);
                    string pass = AesCrypt.Encrypt(textboxpassword.Text);
                    string encusr = $"SELECT * FROM userlogin WHERE username='{user}';";
                    string encpass = $"SELECT * FROM userlogin WHERE password='{pass}';";
    
                    string decusr = AesCrypt.Decrypt(encusr);
                    string decpass = AesCrypt.Decrypt(encpass);
    
                    if (decusr == textboxusername.Text && decpass == textboxpassword.Text)
                    {
                        FormMsbOk.Show("You logged in successfully as user: " + textboxusername.Text, "Ok");
                        con.Close();
                        this.Hide();
                        var main = new FormMain();
                        main.Closed += (s, args) => this.Close();
                        main.Show();
                    }
                    else
                    {
                        textboxusername.Clear();
                        textboxpassword.Clear();
                        FormMsbOk.Show("Error Username or password is wrong!", "Ok");
                    }
                }
                catch (Exception nocon)
                {
                    textboxusername.Clear();
                    textboxpassword.Clear();
                    FormMsbOk.Show("Can not open connection! " + nocon.Message,"Ok");
                }
    
    private void buttonregister_Click(object sender, EventArgs e)
        {
            if (textboxusername.Text.Length < 2 || textboxpassword.Text.Length < 4)
            {
                FormMsbOk.Show("Username or Password is too short! " +
                    "The minimum for the user name is 2 characters and for " +
                    "the password is 4 characters. ", "Ok");
            }
            else
            {
                MySqlConnection con;
                con = new MySqlConnection(myConnectionString);
                try
                {
                    con.Open();
                    string exists = $"CREATE TABLE IF NOT EXISTS `userlogin`.`userlogin` " +
                    $"( `id` INT NOT NULL AUTO_INCREMENT , `username` VARCHAR(64)" +
                    $" NOT NULL , `password` VARCHAR(64) NOT NULL , `prename` VARCHAR(64)" +
                    $" NOT NULL , `surname` VARCHAR(64) NOT NULL , `emailadress` VARCHAR(64)" +
                    $" NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;";
                    MySqlCommand emdexists = new MySqlCommand(exists, con);
                    emdexists.ExecuteNonQuery();
                    string encusr = AesCrypt.Encrypt(textboxusername.Text);
                    string encpass = AesCrypt.Encrypt(textboxpassword.Text);
                    string encprename = AesCrypt.Encrypt(textboxprename.Text);
                    string enclastname = AesCrypt.Encrypt(textboxlastname.Text);
                    string encemail = AesCrypt.Encrypt(textboxemail.Text);
                    string insert = $"INSERT INTO `userlogin`.`userlogin` " +
                        $"(`username`, `password`, `prename`, `surname`, `emailadress`) " +
                        $"VALUES ('" + encusr + "', '" + encpass + "', '" + encprename + "'," +
                        " '" + enclastname + "', '" + encemail + "');";
                    MySqlCommand cmdinsert = new MySqlCommand(insert, con);
                    cmdinsert.ExecuteNonQuery();
                    con.Close();
                    FormMsbOk.Show("Registriert", "Ok");
                    textboxusername.Clear();
                    textboxpassword.Clear();
                    textboxprename.Clear();
                    textboxlastname.Clear();
                    textboxrepeat.Clear();
                    textboxemail.Clear();
                }
                catch (Exception nocon)
                {
                    FormMsbOk.Show("Can not open connection! " + nocon.Message, "Ok");
                }
            }