在MySQL中加密,在C#中解密

时间:2017-08-22 03:11:14

标签: c# php mysql encryption blob

我在MySQL中加密了数据,我将其存储为BLOB,然后我需要在C#中解密,但我没有得到预期的结果。

MYSQL中的BLOB:

BLOB in MySQL

这是我的结果:

Result

应该只是PD001KY6900430

这是我在C#中的代码

string ConnectionString = "Data Source=win-3doecchgfbt;Initial Catalog=DWH;User id=sa;Password=Password123;";
        using (SqlConnection connection = new SqlConnection(ConnectionString))
        {
            string query = "SELECT * FROM tb_investor";
            SqlDataAdapter adapter = new SqlDataAdapter();
            var command = new SqlCommand(query, connection);
            adapter.SelectCommand = command;

            DataTable dTable = new DataTable();

            adapter.Fill(dTable);
            for(var x =0; x < dTable.Rows.Count; x++)
            {
                var dr = dTable.Rows;

                byte[] accNoByte = (byte[])dr[x].ItemArray[1];

                byte[] key = mkey("satu");

                var rkey = BitConverter.ToString(key).Replace("-", "");

                var decAccNo = decrypt_function(accNoByte, key);

            }
        }

这是mkey方法:

Encoding winLatinCodePage = Encoding.GetEncoding(1252);
        byte[] key = Encoding.UTF8.GetBytes(skey);
        byte[] k = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        for (int i = 0; i < key.Length; i++)
        {
            k[i % 16] = (byte)(k[i % 16] ^ key[i]);
        }

        return k;

这是decrypt_function方法:

RijndaelManaged Crypto = null;
        MemoryStream MemStream = null;
        ICryptoTransform Decryptor = null;
        CryptoStream Crypto_Stream = null;
        StreamReader Stream_Read = null;
        string Plain_Text;

        try
        {
            Crypto = new RijndaelManaged();
            Crypto.Key = Key;
            Crypto.Mode = CipherMode.ECB;
            Crypto.Padding = PaddingMode.None;

            MemStream = new MemoryStream(Cipher_Text);
            Crypto.GenerateIV();
            //Create Decryptor make sure if you are decrypting that this is here and you did not copy paste encryptor.
            Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);

            //This is different from the encryption look at the mode make sure you are reading from the stream.
            Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);

            //I used the stream reader here because the ReadToEnd method is easy and because it return a string, also easy.
            Stream_Read = new StreamReader(Crypto_Stream);
            Plain_Text = Stream_Read.ReadToEnd();
        }
        finally
        {
            if (Crypto != null)
                Crypto.Clear();

            MemStream.Flush();
            MemStream.Close();

        }
        return Plain_Text;

请告诉我我犯过的错误。

1 个答案:

答案 0 :(得分:4)

&#34; PD001KY6900430&#34;是14字节,AES(RijndaelManaged默认)块大小是16字节,因此输入数据需要填充到块大小倍数,即PKCS#7 padding的最后两个0x02字节。因此,最后两个字节是:&#34; PD001KY6900430 \ u0002 \ u0002&#34; (其中\ u0002表示UTF-16中0x02的单个字节)是填充。

通常通过为解密方法指定PKCS#7填充来处理(删除)。

  

修复:

更改
Crypto.Padding = PaddingMode.None;

Crypto.Padding = PaddingMode.PKCS7;

最好完全指定所有选项。