亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將創建加密密鑰的 PHP 代碼轉換為 C# MD5

將創建加密密鑰的 PHP 代碼轉換為 C# MD5

PHP
阿波羅的戰車 2023-08-11 10:45:46
我正在嘗試將創建加密密鑰的以下代碼轉換為 c# 結果我需要$key ** 和 ** $iv$passphrase = 'asdfghjkl';$salt =  '123456789' // for test purposes I fixed the value but it should be openssl_random_pseudo_bytes(8);        $salted = '';        $dx = '';        while (strlen($salted) < 48) {            $dx = md5($dx . $passphrase . $salt, true);            $salted .= $dx;        }        $key = substr($salted, 0, 32);        $iv = substr($salted, 32, 16);到目前為止我能做什么string passphrase = "asdfghjkl";string salt = "123456789";string key, iv;byte[] salted = new byte[0];byte[] dx = new byte[0];while (salted.Length < 48)      {        string a = passphrase + salt;        byte[] b = Encoding.UTF8.GetBytes(a);        byte[] rv = new byte[dx.Length + b.Length];        System.Buffer.BlockCopy(dx, 0, rv, 0, dx.Length);        System.Buffer.BlockCopy(b, 0, rv, dx.Length, b.Length);        //dx = MD5CryptoServiceProvider.Create().ComputeHash(rv);        using (MD5 md5 = MD5.Create())              {                dx = md5.ComputeHash(rv);              }        byte[] rx = new byte[salted.Length + dx.Length];        System.Buffer.BlockCopy(salted, 0, rx, 0, salted.Length);        System.Buffer.BlockCopy(dx, 0, rx, salted.Length, dx.Length);        salted = rx;      }string utfString1 = Encoding.UTF8.GetString(salted);key = utfString1.Substring( 0, 32);iv = utfString1.Substring(32, 16);但我沒有得到相同的結果
查看完整描述

1 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

被調用的 PHPmd5函數將以原始二進制格式(16 字節)返回 md5 哈希值。

這是 .NET 中的等效項。

...

var passphrase = Encoding.UTF8.GetBytes("asdfghjkl");

var salt = Encoding.UTF8.GetBytes("123456789");

var salted = new List<byte>();

var dx = new byte[0];

using (var md5 = MD5.Create())

{

? ? do

? ? {

? ? ? ? var bytesToHash = dx.Concat(passphrase).Concat(salt);

? ? ? ? dx = md5.ComputeHash(bytesToHash.ToArray());

? ? ? ? salted.AddRange(dx);

? ? } while (salted.Count < 48);

}


var key = salted.Take(32).ToArray();

var iv = salted.Skip(32).Take(16).ToArray();

...

如果要檢查輸出,請使用 PHP?base64_encode函數和 .NET?Convert.ToBase64String,然后比較 base64 字符串。



查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號