Mysql中的AES _Encryption,C#.Net中的解密

时间:2022-06-19 18:32:45

Mysql :

Mysql:

SELECT AES_ENCRYPT('Test','pass')

AES_ENCRYPT() and AES_DECRYPT() enable encryption and decryption of data using the official AES (Advanced Encryption Standard) algorithm, previously known as “Rijndael.” Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.

AES_ENCRYPT()和AES_DECRYPT()使用官方AES(高级加密标准)算法(以前称为“Rijndael”)对数据进行加密和解密。使用128位密钥长度的编码,但您可以将其扩展到256通过修改源来进行比特。我们选择了128位,因为它更快,并且对于大多数用途来说它足够安全。

http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_aes-encrypt

http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_aes-encrypt

I was trying to convert that Encrypted string into Decryped Strig in C#.net but i don't get the results as i expect.

我试图在C#.net中将加密的字符串转换为Decryped Strig,但我没有得到我期望的结果。

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx#Y0

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael.aspx#Y0

C#

C#

static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)

In this method I pass ciphertext,Key value which i usedfrom Mysql and Rijndael.Create().IV for byte[] IV

在这个方法中,我传递密文,我使用的密钥值来自Mysql和Rijndael.Create()。IV for byte [] IV

I use the code but i don't get expected result. Review the code and comment Idk where made a mistake

我使用代码,但我没有得到预期的结果。查看代码并评论Idk哪里出错了

3 个解决方案

#1


1  

What you are doing is following a road of pain. Either decrypt/encrypt on MySQL and use an encrypted connection to the database (if that matters) or encrypt/decrypt on your .NET application, storing the encrypted data in a suitable column.

你正在做的是走一条痛苦的道路。在MySQL上解密/加密并使用加密连接到数据库(如果这很重要)或在.NET应用程序上加密/解密,将加密数据存储在合适的列中。

Mixing AES implementations is prone to mistakes and things can break more easily if you change versions of .NET or MySQL.

混合AES实现容易出错,如果更改.NET或MySQL版本,事情就会更容易破解。

Now, to know what exactly is wrong we need to know if the IV is compatible between MySQL and .NET, or else find out what is MySQL's implementation IV and supply that.

现在,要知道究竟什么是错误的,我们需要知道IV和MySQL是否兼容,或者找出MySQL的实现IV并提供它。

And the other potential source of problems is how you have generated the byte arrays (we are not seeing that in your example). You have to consider character encoding issues in generating the arrays if the key is textual.

另一个潜在的问题来源是如何生成字节数组(我们在您的示例中没有看到)。如果密钥是文本的,则必须考虑生成数组时的字符编码问题。

In the comments of this MySQL docs link there is information about the missing parameters.

在此MySQL文档链接的注释中,有关于缺少参数的信息。

#2


1  

Here is some working code for achieving the same encryption via C# as MySQL:

以下是一些通过C#实现与MySQL相同加密的工作代码:

public byte[] AESEncrypt(byte[] plaintext, byte[] key) {
/* 
* Block Length: 128bit
* Block Mode: ECB
* Data Padding: Padded by bytes which Asc() equal for number of padded bytes (done automagically)
* Key Padding: 0x00 padded to multiple of 16 bytes
* IV: None
*/
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.Mode = CipherMode.ECB;
aes.Key = key;

ICryptoTransform encryptor = aes.CreateEncryptor();
MemoryStream mem = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(mem, encryptor,
CryptoStreamMode.Write);

cryptStream.Write(plaintext, 0, plaintext.Length);
cryptStream.FlushFinalBlock();

byte[] cypher = mem.ToArray();

cryptStream.Close();
cryptStream = null;
encryptor.Dispose();
aes = null;

return cypher;
}

For details see MySQL Bug # 16713

有关详细信息,请参阅MySQL Bug#16713

EDIT:

编辑:

Since the above is relying on officially non-documented information (though it is working) I would recommend to avoid it and use one of the options described in the answer from Vinko Vrsalovic .

由于上述内容依赖于官方未记录的信息(尽管它有效),我建议避免使用它,并使用Vinko Vrsalovic答案中描述的其中一个选项。

#3


1  

If you run SELECT AES_ENCRYPT('Test','pass') your are sending the pass over the network unencrypted so any one can unencrypted the data.

如果您运行SELECT AES_ENCRYPT('Test','pass'),您将通过网络发送未加密的传递,以便任何人都可以解密数据。

The AES_ENCRYPT is used to store data so if the database gets hacked your data is safe, not to transmit data.

AES_ENCRYPT用于存储数据,因此如果数据库被黑客攻击,您的数据是安全的,而不是传输数据。

if you want data encryption over the net work connect to your mysql server using the ssl socket

如果你想通过网络连接到你的mysql服务器使用ssl套接字进行数据加密

#1


1  

What you are doing is following a road of pain. Either decrypt/encrypt on MySQL and use an encrypted connection to the database (if that matters) or encrypt/decrypt on your .NET application, storing the encrypted data in a suitable column.

你正在做的是走一条痛苦的道路。在MySQL上解密/加密并使用加密连接到数据库(如果这很重要)或在.NET应用程序上加密/解密,将加密数据存储在合适的列中。

Mixing AES implementations is prone to mistakes and things can break more easily if you change versions of .NET or MySQL.

混合AES实现容易出错,如果更改.NET或MySQL版本,事情就会更容易破解。

Now, to know what exactly is wrong we need to know if the IV is compatible between MySQL and .NET, or else find out what is MySQL's implementation IV and supply that.

现在,要知道究竟什么是错误的,我们需要知道IV和MySQL是否兼容,或者找出MySQL的实现IV并提供它。

And the other potential source of problems is how you have generated the byte arrays (we are not seeing that in your example). You have to consider character encoding issues in generating the arrays if the key is textual.

另一个潜在的问题来源是如何生成字节数组(我们在您的示例中没有看到)。如果密钥是文本的,则必须考虑生成数组时的字符编码问题。

In the comments of this MySQL docs link there is information about the missing parameters.

在此MySQL文档链接的注释中,有关于缺少参数的信息。

#2


1  

Here is some working code for achieving the same encryption via C# as MySQL:

以下是一些通过C#实现与MySQL相同加密的工作代码:

public byte[] AESEncrypt(byte[] plaintext, byte[] key) {
/* 
* Block Length: 128bit
* Block Mode: ECB
* Data Padding: Padded by bytes which Asc() equal for number of padded bytes (done automagically)
* Key Padding: 0x00 padded to multiple of 16 bytes
* IV: None
*/
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.Mode = CipherMode.ECB;
aes.Key = key;

ICryptoTransform encryptor = aes.CreateEncryptor();
MemoryStream mem = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(mem, encryptor,
CryptoStreamMode.Write);

cryptStream.Write(plaintext, 0, plaintext.Length);
cryptStream.FlushFinalBlock();

byte[] cypher = mem.ToArray();

cryptStream.Close();
cryptStream = null;
encryptor.Dispose();
aes = null;

return cypher;
}

For details see MySQL Bug # 16713

有关详细信息,请参阅MySQL Bug#16713

EDIT:

编辑:

Since the above is relying on officially non-documented information (though it is working) I would recommend to avoid it and use one of the options described in the answer from Vinko Vrsalovic .

由于上述内容依赖于官方未记录的信息(尽管它有效),我建议避免使用它,并使用Vinko Vrsalovic答案中描述的其中一个选项。

#3


1  

If you run SELECT AES_ENCRYPT('Test','pass') your are sending the pass over the network unencrypted so any one can unencrypted the data.

如果您运行SELECT AES_ENCRYPT('Test','pass'),您将通过网络发送未加密的传递,以便任何人都可以解密数据。

The AES_ENCRYPT is used to store data so if the database gets hacked your data is safe, not to transmit data.

AES_ENCRYPT用于存储数据,因此如果数据库被黑客攻击,您的数据是安全的,而不是传输数据。

if you want data encryption over the net work connect to your mysql server using the ssl socket

如果你想通过网络连接到你的mysql服务器使用ssl套接字进行数据加密