gpt4 book ai didi

c# - PHP mcrypt_encrypt 到 .NET

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:50 24 4
gpt4 key购买 nike

我几乎失去了头发、思想和其他一切!我一直在尝试将此 PHP 函数转换为 C#:

function  encrypt_decrypt($action, $string) {
$output = false;
$key = 'My strong secret key';
// initialization vector
$iv = md5(md5($key));
$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
$output = bin2hex($output);
return $output;
}

我一直在使用 Rijandel 类:

function  encrypt_decrypt(string password) {
UTF8Encoding encoding = new UTF8Encoding();
// For consistency with PHP function, MD5Encrypt applies MD5 encryption and does a bin2hex
byte[] Key = Encoding.ASCII.GetBytes(MD5Encrypt(password).ToLower());
byte[] IV = Encoding.ASCII.GetBytes(MD5Encrypt(MD5Encrypt(password).ToLower()).ToLower());

RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
rj.KeySize = 256;
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
MemoryStream ms = new MemoryStream();

using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
string output = "";
foreach (var ele in encoded)
{
output += ele.ToString("X2");
}

return output;
}

我一直在验证 PHP 代码的输出与 C# 代码的输出,但它们不匹配。 (http://writecodeonline.com/php/)。如有任何反馈,我们将不胜感激。

最佳答案

在执行此操作时需要牢记多个问题,例如转换二进制文件、检查编码和填充问题。由于我们看不到您的完整代码,在这种情况下我们束手无策。查看本教程以获取更多信息:http://blog.djekldevelopments.co.uk/?p=334

关于c# - PHP mcrypt_encrypt 到 .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com