代码之家  ›  专栏  ›  技术社区  ›  Siti Marfuah

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

  •  4
  • Siti Marfuah  · 技术社区  · 7 年前

    我在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_函数方法:

    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 回复  |  直到 7 年前
        1
  •  4
  •   zaph    7 年前

    “PD001KY6900430”为14字节,AES(RijndaelManaged default)块大小为16字节,因此需要将输入数据填充到块大小的倍数,即最后两个 0x02 字节数 PKCS#7 padding 0x02 在UTF-16)中是填充。

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

    修复:

    改变
    Crypto.Padding = PaddingMode.None;

    Crypto.Padding = PaddingMode.PKCS7;