Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MySQL中加密,在C中解密#_C#_Php_Mysql_Encryption_Blob - Fatal编程技术网

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

C# 在MySQL中加密,在C中解密#,c#,php,mysql,encryption,blob,C#,Php,Mysql,Encryption,Blob,我在MySQL中对数据进行了加密,我将其存储为BLOB,然后我需要在C#中对其进行解密,但没有得到预期的结果 MYSQL中的BLOB: 这是我的结果: 应该是PD001KY6900430 这是我的C代码# 请告诉我我犯的错误。“PD001KY6900430”是14字节,AES(RijndaelManaged default)块大小是16字节,因此需要将输入数据填充到块大小的倍数,即。因此,“PD001KY6900430\u0002\u0002”的最后两个字节(其中\u0002表示UTF-16

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

MYSQL中的BLOB:

这是我的结果:

应该是PD001KY6900430

这是我的C代码#

请告诉我我犯的错误。

“PD001KY6900430”是14字节,AES(RijndaelManaged default)块大小是16字节,因此需要将输入数据填充到块大小的倍数,即。因此,“PD001KY6900430\u0002\u0002”的最后两个字节(其中\u0002表示UTF-16中
0x02
的单个字节)是填充

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

修复方法:

更改
Crypto.Padding=PaddingMode.None

Crypto.Padding=PaddingMode.PKCS7


最好始终完全指定所有选项。

我确实尝试删除了填充,对于列accNo,它可以工作,结果与预期一样,但另一列address出现错误,错误:“填充无效,无法删除”更正:如果我删除,“Crypto.Mode=CipherMode.ECB;加密填充=填充模式。无;,即使对于accNo列,也会立即出现错误“Padding is invalid and cannot remove”为什么要删除
CipherMode.ECB
?请提供
accNo
上的数据。首先,我尝试删除paddingmode。无,accNo的结果与预期一致,但错误“Padding is invalid…”。。。。当我使用ciphermode.ECB和paddingmode.none时,此明文的address,BLOB数据“JL.MESJID RAYA NO.3 RT.004/002”将被删除。问题是,我的结果中添加了一些字符,aaa,并且长明文的结果被截断。。。。。。。。。。我对此一无所知……令人困惑
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);

            }
        }
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;
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;