我正在使用 HMACSHA256 將代碼從 C# 轉換為 vb.net,但使用 C# 和 vb.net 時結果不同
vb.net 中的代碼
Imports System
Imports System.Security.Cryptography
Imports System.Text
Public Module Module1
Public Sub Main()
Console.WriteLine(hmacSHA256("Client-Id:MCH-1634273860130\nRequest-Id:425944\nRequest-Timestamp:2022-09-06T09:23:00Z\nRequest-Target:/checkout/v1/payment\nDigest:9ZhZatgEJvY4c87mSwbknmfWL9 3eS18YV6xnivYIV4=","KHUvvn4fm3zXRIip0UWY"))
End Sub
Function hmacSHA256(ByVal data As String, ByVal key As String) As String
Using hmac As HMACSHA256 = New HMACSHA256(Encoding.ASCII.GetBytes(key))
Dim a2 As Byte() = hmac.ComputeHash(Encoding.ASCII.GetBytes(data))
Dim a3 As String = Convert.ToBase64String(a2).ToString().Replace(" ", "-").Replace("/", "_").Replace("=", "")
Return a3
End Using
End Function
End Module
https://dotnetfiddle.net/e95rZl
和用 C# 撰寫
using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
Console.WriteLine(hmacSHA256("Client-Id:MCH-1634273860130\nRequest-Id:425944\nRequest-Timestamp:2022-09-06T09:23:00Z\nRequest-Target:/checkout/v1/payment\nDigest:9ZhZatgEJvY4c87mSwbknmfWL9 3eS18YV6xnivYIV4=","KHUvvn4fm3zXRIip0UWY"));
}
private static string hmacSHA256(String data, String key)
{
using (HMACSHA256 hmac = new HMACSHA256(Encoding.ASCII.GetBytes(key))) {
byte[] a2 = hmac.ComputeHash(Encoding.ASCII.GetBytes(data));
string a3 = Convert.ToBase64String(a2).ToString().Replace(" ", "-").Replace("/", "_").Replace("=", "");
return a3;
}
}
}
https://dotnetfiddle.net/JblmZD
我已經嘗試使用 PHP HMACSHA256(它與 C# 給出相同的結果)
我的 VB.NET 代碼中是否缺少任何配置?
uj5u.com熱心網友回復:
轉義序列在\nVB.Net 字串中不起作用。改用Microsoft.VisualBasic.ControlChars.Lf:
Console.WriteLine(hmacSHA256("Client-Id:MCH-1634273860130" & ControlChars.Lf & "Request-Id:425944" & ControlChars.Lf & "Request-Timestamp:2022-09-06T09:23:00Z" & ControlChars.Lf & "Request-Target:/checkout/v1/payment" & ControlChars.Lf & "Digest:9ZhZatgEJvY4c87mSwbknmfWL9 3eS18YV6xnivYIV4=","KHUvvn4fm3zXRIip0UWY"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504932.html
上一篇:如何防止在WPF中無故重繪ObservableCollection?
下一篇:json格式的C#目錄路徑
