Encryption in MySQL, decryption in C #

I encrypted my data in MySQL, I stored it as a BLOB, then I need to decrypt it in C #, but I do not get the expected result.

BLOB in MYSQL:

BLOB in MySQL

This is my result:

Result

It should be just PD001KY6900430

Here is 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 made.

+4
source share
1 answer

"PD001KY6900430" - 14 , AES (RijndaelManaged default) - 16 , , 0x02 PKCS # 7 padding. , : "PD001KY6900430\u0002\u0002" (\u0002 0x02 UTF-16) - .

(), PKCS # 7 .

:


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

.

+4

Source: https://habr.com/ru/post/1684176/


All Articles