简体   繁体   中英

Vb.Net 256 Hash to PHP 256 Hash

I have VB.Net method that does this:

' 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)

In PHP I'm doing this:

// 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);

The output from my VB.Net method differs from the PHP and I think it's that VB encryption is happening on the byte array while PHP is on the string itself.

Any help is greatly appreciated!

The solution was to change the PHP from:

$tohash = utf8_encode('testinfostring');

to:

$tohash = mb_convert_encoding('testinfostring', 'UTF-16LE', 'UTF-8');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM