加密-解密datagridview mysql:输入数据不是完整的块

时间:2018-10-16 14:56:06

标签: c# encryption datagridview

我仍然是C#初学者,正在帮助我的那个人离开了。 我有2个数据库。我想将密码从一个数据库移到另一个数据库。但是每个数据库都使用不同的加密。我想解密数据库1的密码,然后使用不同的加密方式为数据库2加密它们。 我什至不知道代码是否可以正常工作,因为我无法对其进行测试...我在这里不断出错:

  

公共字符串Decrypt >>>> cs.Close();

     

System.Security.Cryptography.CryptographicException:“输入数据不是完整的块。”

private void PopulateData()
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "UserName";
dataGridView1.Columns[0].HeaderText = "UserName";
dataGridView1.Columns[0].DataPropertyName = "UserName";

dataGridView1.Columns[1].HeaderText = "Encrpted Password";
dataGridView1.Columns[1].Name = "Password";
dataGridView1.Columns[1].DataPropertyName = "Password";

dataGridView1.Columns[2].HeaderText = "Decrypted Password";
dataGridView1.Columns[2].Name = "DecryptedPassword";
dataGridView1.Columns[2].DataPropertyName = "DecryptedPassword";

        string constring = 
"datasource=server;port=port;username=user;password=pass;SSL Mode=None";
        MySqlConnection conDatabase = new MySqlConnection(constring);
        MySqlCommand cmdDatabase = new MySqlCommand(" SELECT 
username,password FROM database.users WHERE 1 ;", conDatabase);
        try
        {
            MySqlDataAdapter sda = new MySqlDataAdapter();
            sda.SelectCommand = cmdDatabase;
            DataTable dbdataset = new DataTable();
            sda.Fill(dbdataset);
            BindingSource bsource = new BindingSource();
            bsource.DataSource = dbdataset;
            dataGridView1.DataSource = bsource;
            sda.Update(dbdataset);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        { 
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[2].Value = Decrypt(row.Cells[1].Value.ToString());
            }
        }
}

private void btnSave_Click(object sender, EventArgs e)
{
string constr = 
ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("insert into 
database.user_account (username, passwd) values (@username,@passwd) ON 
DUPLICATE KEY UPDATE username = username,passwd=passwd"))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@username", dataGridView1.Text.Trim());
        cmd.Parameters.AddWithValue("@passwd", 
Encrypt(dataGridView1.Text.Trim()));
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}
this.PopulateData();
}

private string Encrypt(string clearText)
{
string EncryptionKey = "xxxxx";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new 
byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 
0x65, 0x76 });
    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);
    using (MemoryStream ms = new MemoryStream())
    {
        using (CryptoStream cs = new CryptoStream(ms, 
encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
        }
        clearText = Convert.ToBase64String(ms.ToArray());
    }
}
return clearText;
}

private string Decrypt(string cipherText)
{
string EncryptionKey = "xxxx";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new 
byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 
0x65, 0x76 });
    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);
    using (MemoryStream ms = new MemoryStream())
    {
        using (CryptoStream cs = new CryptoStream(ms, 
encryptor.CreateDecryptor(), CryptoStreamMode.Write))
        {
            cs.Write(cipherBytes, 0, cipherBytes.Length);
            cs.Close();
        }
        cipherText = Encoding.Unicode.GetString(ms.ToArray());
    }
}
return cipherText;
}

我尝试了所有经验,但是没有用。 还有更简单的方法吗? 对于我的经验不足和冗长的帖子,我深表歉意!

0 个答案:

没有答案