如何将C#签名例程转换为vb.net [英] How Convert C# signature routine to vb.net
问题描述
我正在开发一个需要创建签名的项目。我有一个C#模块可以工作,我正在尝试创建一个vb.net示例,但它没有生成相同的值,我不知道我做错了什么。
欢迎所有帮助。
价值观:
key =dab01234dab01234
message =GET\\\
api-clientid:06f68777d07e4d2994a657834662bc345\\\
api-version:1 \\\memestamp:4/06/2015 11:04 am\\\
tops-clientid:dab \\ \\ ncommunity
C#Routine
使用System;
使用System。 Collections.Generic;
使用System.Linq;
使用System.Net.Http;
使用System.Net.Http.Headers;
使用System.Security.Cryptography;
使用System.Text;
使用System.Threading.Tasks;
using System.Web.Http;
private static string GetSignature(string key,string message){
var hmac = new HMACSHA256(Encoding) .UTF8.GetBytes(key));
返回Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)));
}
}
返回值by C#
签名G2LWmwWsDkTbOLahA8 / EykgkGIakk5kOl9ZNXrOuKAY =string
VB.net例程
进口系统
进口System.Collections.Generic
进口System.Linq
进口System.Net.Http
Imports System.Net.Http.Headers
Imports System.Security.Cryptography
Imports System.Text
进口System.Threading.Tasks
导入System.Web.Http
私有函数GetSignature(ByVal键为字符串,ByVal消息为字符串)As字符串
Dim hmac As HMACSHA256 = New HMACSHA256(Encoding.UTF8.GetBytes(key))
返回Convert.ToBase64String(hmac.ComputeHash (Encoding.UTF8.GetBytes(message)))
结束功能
返回值通过VB
sSignatureqWrq4fx1U / JYKBI + z6yIw10 + oH3x3nHJ4 / JTHGIlIHA =String
I am working on a project that needs to create a signature. I have a C# module that works and I am trying to create a vb.net sample but it is not generating the same value and I am not sure what I am doing wrong.
All help is welcome.
Values:
key= "dab01234dab01234"
message ="GET\napi-clientid:06f68777d07e4d2994a657834662bc345\napi-version:1\ntimestamp:4/06/2015 11:04am\ntops-clientid:dab\ncommunity"
C# Routine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
private static string GetSignature(string key, string message) {
var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)));
}
}
Value Returned by C#
signature "G2LWmwWsDkTbOLahA8/EykgkGIakk5kOl9ZNXrOuKAY=" string
VB.net Routine
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks
Imports System.Web.Http
Private Function GetSignature(ByVal key As String, ByVal message As String) As String
Dim hmac As HMACSHA256 = New HMACSHA256(Encoding.UTF8.GetBytes(key))
Return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)))
End Function
Value returned by VB
sSignature "qWrq4fx1U/JYKBI+z6yIw10+oH3x3nHJ4/JTHGIlIHA=" String
推荐答案
您将不同的字符串传递给方法。
- 在C#字符串文字中,\ n表示换行符(ASCII代码10)。
- 在VB字符串文字中,\ n 代表反斜杠(ASCII代码92),后跟小写n(ASCII代码110)。
You're passing different strings to the method.
- In a C# string literal, "\n" represents a new-line character (ASCII code 10).
- In a VB string literal, "\n" represents a back-slash (ASCII code 92) followed by a lower-case "n" (ASCII code 110).
没有转换。这被称为翻译,从一种语言到另一种语言。没有必要提出这样的问题,因为您可以自动翻译所有内容。请看我过去的答案:
代码解释,C#到VB.NET [ ^ ]。
-SA
There is no "convert". This is called "translation", from one language to another. There is no need to ask such questions, as you can translate everything automatically. Please see my past answer:
Code Interpretation, C# to VB.NET[^].
—SA
这篇关于如何将C#签名例程转换为vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!