簡體   English   中英

從緩沖區到字符串的轉換在 c# 和 Nodejs 中給出了不同的結果

[英]Conversion from buffer to string gives different results in c# and Nodejs

我正在嘗試將此 function 從 C# 轉換為節點,但是當我嘗試將緩沖區轉換為字符串時,我得到了不同的結果。

string str = "34924979";
System.Security.Cryptography.SHA512 sha = new System.Security.Cryptography.SHA512Managed();
byte[] ba = System.Text.Encoding.ASCII.GetBytes(str);
byte[] data= sha.ComputeHash(ba);
Console.WriteLine(System.Text.Encoding.ASCII.GetString(data));


>> `?????gV)????∟?Z?s??v$We-??N?"?w????0??\i♠G???

這就是我想要做的。

const crypto = require('crypto')

const str = '34924979';
const sha = crypto.createHash('sha512').update(str).digest('utf8');
const hash = sha.toString('utf-8').replace(/\uFFFD/g, '?');
console.log(hash);

>> `????gV)????∟?Z?v$We-??N?"?w????0?\i♠Gߕ?

您在 C# 和 JS 中使用了不同的編碼。

嘗試將 JS 代碼更改為以下內容:

const sha = crypto.createHash('sha512').update(str).digest('ascii');
const hash = sha.toString('ascii');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM