Mysql中的AES _Encryption,C#.Net中的解密 [英] AES _Encryption in Mysql , Decryption in C#.Net

查看:73
本文介绍了Mysql中的AES _Encryption,C#.Net中的解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mysql:

SELECT AES_ENCRYPT('Test','pass')

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

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.

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

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

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

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

C#

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

在这种方法中,我将密文,密钥值(我从Mysql和
Rijndael.Create()。IV中使用)传递给byte [] IV

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

我没有得到预期的结果。
查看代码并在错误的地方注释IDk

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

推荐答案

以下是一些有效的代码,可通过C#作为MySQL:

Here is some working code for achieving the same encryption via C# as 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;
}

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

For details see MySQL Bug # 16713

编辑:

由于以上内容依赖于官方未记录的信息(尽管它正在运行),所以我建议避免使用此信息,并使用 Vinko Vrsalovic的回答

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 .

这篇关于Mysql中的AES _Encryption,C#.Net中的解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆