从C#.net 2.0迁移到php,因此密码的哈希值-如何解析?

从C#.net 2.0迁移到php,因此密码的哈希值-如何解析?,c#,php,hash,migrate,hmac,C#,Php,Hash,Migrate,Hmac,我们以前使用C#.net 2.0创建web应用程序 使用以下代码对用户密码进行哈希运算并存储在数据库中 private const string encryptionKey = "AE09F72B007CAAB5"; HMACSHA1 hash = new HMACSHA1(); hash.Key = HexToByte(encryptionKey); encodedPassword = Convert.ToBase64String( hash.ComputeHash(Encoding

我们以前使用C#.net 2.0创建web应用程序

使用以下代码对用户密码进行哈希运算并存储在数据库中

private const string encryptionKey = "AE09F72B007CAAB5";

HMACSHA1 hash = new HMACSHA1();
hash.Key = HexToByte(encryptionKey);
encodedPassword = Convert.ToBase64String(
    hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
现在我们打算迁移到php

因此,当用户想要返回时,我们面临一个问题

为了使数据库中的散列值能够工作,应该使用与该方法相当的php吗

例如,编码密码为pa55w0rd 获取的哈希值为oK9NOVhpTkxLoLfvh1430SFb5gw=


谢谢。

使用.Net版本的PHP()或使用.Net Web服务进行编码/解码不是一个选项?

尝试以下方法:

<?php
 $key = "AE09F72B007CAAB5";
 echo base64_encode(hash_hmac("sha1", "test", $key, true));
?>

在C#中,哈希的默认格式为十六进制(AE09F72B007CAAB5是十六进制数)。PHP默认为Base64格式。解决问题的方法是在php中将base64字符串转换为十六进制。在C#app中,您可以用两种不同的方式生成byte[]数组,但结果略有不同。您的PHP脚本需要精确地模拟它们。 hash.Key=HexToByte(encryptionKey)您传入一个16个字符长的字符串并得到一个8字节的数组,就像
hash.Key=newbyte[]{0xAE,0x09,0xF7,0x2B,0x00,0x7C,0xAA,0xB5}但是打印错误:|数据|=16 |密码|=8
string password = "pa55w0rd";
byte[] b = Encoding.Unicode.GetBytes(password)

hmac-sha1:oK9NOVhpTkxLoLfvh1430SFb5gw=

hi tt,nope不起作用。我得到了v1tgPrd6893541ZqrDVr4F1qHw8=而不是oK9NOVhpTkxLoLfvh1430SFb5gw=我想知道从C#迁移到php的原因是什么。这个评论有点离题,但我想了解一些背后的原因。我们从C#开始,因为主要的开发人员更熟悉。然而,linux主机更便宜,因此转向php。仅此而已。我可以理解,我们一开始并没有做出正确的决定,但我们致力于做出正确的决定。因此,这一举措奏效了!谢谢我本可以就这样离开的。但我想学习并在这方面做得更好。请帮我提几个问题http请求,其中数据是utf-8编码的。相应地调整//mb_convert_encoding()的$from参数,目的是构建一个php登录页面,所以我想它将是http请求?如何判断它是否是utf-8编码?如果可能的话,如何调整编码?再次感谢你!!顺便说一下,我知道对于utf8,我需要更改为$data=mb_convert_编码($password,'UTF-16LE','UTF-8');我的意思是http请求。简言之:如果您发送包含utf-8编码的登录表单的html文档,那么客户端也会发送utf-8编码的登录数据。好奇的是,我找不到关于这个话题的问题,所以…?谢谢米沙,但我们正在寻求削减成本。因此,如果我们有一个与c#中使用的散列函数相当的php函数,那么它会更优雅。无论如何,谢谢你留下你的答案。现在我知道有一种东西叫做法兰格。我以前没听说过。嗨,亚历克斯,谢谢你的回答。我想问你更多的细节,但我得到了我需要的答案。非常感谢。
<?php
$password = "pa55w0rd";
$key = HexToBytes("AE09F72B007CAAB5"); // 8 bytes, hex

// $to must be 'UTF-16LE' // $from depends on the "source" of $password $data = mb_convert_encoding($password, 'UTF-16LE', 'ASCII');

// I've saved this script as an ascii file -> the string literal is ASCII encoded // therefore php's strlen() returns 8 for $password and 16 for $data // this may differ in your case, e.g. if the contents of $password comes from a // http-request where the data is utf-8 encoded. Adjust the $from parameter for // mb_convert_encoding() accordingly echo 'Debug: |data|=', strlen($data), ' |password|=', strlen($password), "\n";

$h = HexToBytes(hash_hmac('sha1', $data, $key)); echo 'hmac-sha1: ', base64_encode($h);

function HexToBytes($s) { // there has to be a more elegant way... return join('', array_map('chr', array_map('hexdec', str_split($s, 2)))); } Debug: |data|=16 |password|=8 hmac-sha1: oK9NOVhpTkxLoLfvh1430SFb5gw=