Siti Marfuah
Siti Marfuah

Reputation: 43

Encrypting in MySQL, Decrypting in C#

I got my data encrypted in MySQL, I store it as BLOB, then I need to decrypt it in C#, but I don't get the expected result.

The BLOB in MYSQL:

BLOB in MySQL

This is my result:

Result

It should be just PD001KY6900430

Here's My Code in 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);

            }
        }

Here is the mkey method :

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;

Here is the decrypt_function method :

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;

Please show me the mistake I've made.

Upvotes: 4

Views: 875

Answers (1)

zaph
zaph

Reputation: 112855

"PD001KY6900430" is 14 bytes, AES(RijndaelManaged default) block size is 16-bytes so the input data needs to be padded to a block size multiple, that is the last two 0x02 bytes of PKCS#7 padding. Thus the two last bytes of: "PD001KY6900430\u0002\u0002" (where \u0002 represents a single byte of 0x02 in UTF-16) is the padding.

This is usually handled (removed) by specifying PKCS#7 padding to the decryption method.

The Fix:

Change
Crypto.Padding = PaddingMode.None;
to
Crypto.Padding = PaddingMode.PKCS7;

It is always best to fully specify all options.

Upvotes: 5

Related Questions