php aes‐256‐cbc,与PHP一样,以C#解密用AES-256-CBC加密的数据

博客作者在尝试使用C# 2013解密之前用PHP AES-256-CBC加密的数据时遇到了问题。C#代码在设置Key时抛出错误,指出指定的密钥大小不适用于该算法。讨论中提到AES-256-CBC的块大小应该是128位,而非256位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I did some encryption using PHP in my Database and would normally decrypt using:

$encrypt_method = "AES-256-CBC";

$secret_key = "testing";

$secret_iv = "testingyes!!!";

$key = hash('sha256', $secret_key); // hash the key

$iv = substr(hash('sha256', $secret_iv), 0, 16); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning

echo(openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv)); // the decrypted data

I'm trying to do the same task but with C# 2013 to decrypt the same data, any ideas?

I would encrypt in php using:

$encrypt_method = "AES-256-CBC";

$secret_key = "testing";

$secret_iv = "testingyes!!!";

$key = hash('sha256', $secret_key); // hash the key

$iv = substr(hash('sha256', $secret_iv), 0, 16); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning

echo(base64_encode(openssl_encrypt($data, $encrypt_method, $key, 0, $iv))); // the encrypted data

encrypting: this is a test

gives: d0EzQ2MvMHkxRks2cXg5NkFkK2twZz09=

I tried this in C#:

public static String sha256_hash(String value)

{

StringBuilder Sb = new StringBuilder();

using (SHA256 hash = SHA256Managed.Create())

{

Encoding enc = Encoding.UTF8;

Byte[] result = hash.ComputeHash(enc.GetBytes(value));

foreach (Byte b in result)

Sb.Append(b.ToString("x2"));

}

return Sb.ToString();

}

private static String AES_decrypt(String Input)

{

RijndaelManaged aes = new RijndaelManaged();

aes.KeySize = 256;

aes.BlockSize = 256;

aes.Mode = CipherMode.CBC;

aes.Padding = PaddingMode.None;

aes.Key = Convert.FromBase64String(sha256_hash("testing"));

aes.IV = Convert.FromBase64String(sha256_hash("testingyes!!!").Substring(0, 16));

var decrypt = aes.CreateDecryptor();

byte[] xBuff = null;

using (var ms = new MemoryStream())

{

using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))

{

byte[] xXml = Convert.FromBase64String(Input);

cs.Write(xXml, 0, xXml.Length);

}

xBuff = ms.ToArray();

}

String Output = Encoding.UTF8.GetString(xBuff);

return Output;

}

string cipherData = "d0EzQ2MvMHkxRks2cXg5NkFkK2twZz09=";

string f = AES_decrypt(cipherData);

Console.Write(f);

But I'm getting error: specified key is not a valid size for this algorithm

However the key I'm using is working when I use PHP

Talk1:

Maybe a stupid question but before I'm going to help you. Why do you want to decrypt the data?

Talk2:

I have some encrypted configuration data in the Database I need to read into a window service coded with C#

Talk3:

.Rattansingh Do you want to get the hashed value back or the key?

Talk4:

I'm trying to decrypt d0EzQ2MvMHkxRks2cXg5NkFkK2twZz09= with AES-256-CBC and with using the key and iv hashed as above. The encryption algorithm is not hashed, it just accepts a hashed key & iv

Talk5:

in php, its openssl_decrypt that's performing the encryption using the hashed keys

Solutions1RijndaelManaged aes = new RijndaelManaged();

aes.KeySize = 256;

aes.BlockSize = 256;

Block size should be 128 to be compatible with AES-256-CBC.

Rijndael supports variable block sizes - AES does not.

Talk1:

changed it but still getting the error on the line: aes.Key = Convert.FromBase64String(sha256_hash("testing")); CryptographicException, specified key is not a valid size for this algorithm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值