我有VB.Net方法可以做到这一点:
' Step 1: UTF8 Encodes
Dim uEncode As New UnicodeEncoding()
' Step 2: Creates Byte Array from UTF8 encoded string
Dim bytClearString() As Byte = uEncode.GetBytes(ClearString)
' Step 3: Creates the hash from the byte array
Dim sha As New System.Security.Cryptography.SHA256Managed()
Dim hash() As Byte = sha.ComputeHash(bytClearString)
' Base64 Encodes the hash
Return Convert.ToBase64String(hash)
在PHP中,我这样做:
// Step 1 utf8 encode
$tohash = utf8_encode('testinfostring');
// Step 2 cast string to byte array
Not sure about this step...
// Step 3
$hash = hash('sha256', $tohash, true);
// Step 4 convert hashed array to base64 encoded string
echo base64_encode($hash);
我的VB.Net方法的输出与PHP不同,我认为VB加密发生在字节数组上,而PHP在字符串本身上。
非常感谢任何帮助!
答案 0 :(得分:1)
解决方案是改变PHP:
$tohash = utf8_encode('testinfostring');
为:
$tohash = mb_convert_encoding('testinfostring', 'UTF-16LE', 'UTF-8');