Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带VB Net的Kraken API“;无效密钥";_C#_Vb.net_Cryptography - Fatal编程技术网

C# 带VB Net的Kraken API“;无效密钥";

C# 带VB Net的Kraken API“;无效密钥";,c#,vb.net,cryptography,C#,Vb.net,Cryptography,我试图在这里转换C代码(特别是“QueryPrivate”函数): 到VB网络工作代码。我一直收到一个“无效密钥”响应。下面是我想出的代码: Private Function QueryPrivate(a_sMethod As String, Optional props As String = Nothing) As String ' generate a 64 bit nonce using a timestamp at tick resolution _u

我试图在这里转换C代码(特别是“QueryPrivate”函数):

到VB网络工作代码。我一直收到一个“无效密钥”响应。下面是我想出的代码:

Private Function QueryPrivate(a_sMethod As String, Optional props As String = Nothing) As String
        ' generate a 64 bit nonce using a timestamp at tick resolution
        _url = "https://api.kraken.com"
        _version = 0
        _key = "Key Here"
        _secret = "Secret Here"

        _rateGate = New RateGate(1, TimeSpan.FromSeconds(5))
        Dim nonce As Int64 = DateTime.Now.Ticks
        props = Convert.ToString("nonce=" + nonce.ToString()) & props


        Dim path As String = String.Format("/{0}/private/{1}", _version, a_sMethod)
        Dim address As String = _url & path
        Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest)
        webRequest__1.ContentType = "application/x-www-form-urlencoded"
        webRequest__1.Method = "POST"
        webRequest__1.Headers.Add("API-Key", _key)


        Dim base64DecodedSecred As Byte() = Convert.FromBase64String(_secret)

        Dim np = nonce.ToString() + Convert.ToChar(0) + props

        Dim pathBytes = Encoding.UTF8.GetBytes(path)
        Dim hash256Bytes = sha256_hash(np)
        Dim z = New Byte(pathBytes.Count() + (hash256Bytes.Count() - 1)) {}
        pathBytes.CopyTo(z, 0)
        hash256Bytes.CopyTo(z, pathBytes.Count())

        Dim signature = getHash(base64DecodedSecred, z)

        webRequest__1.Headers.Add("API-Sign", Convert.ToBase64String(signature))

        If props IsNot Nothing Then

            Using writer = New StreamWriter(webRequest__1.GetRequestStream())
                writer.Write(props)
            End Using
        End If

        'Make the request
        Try
            'Wait for RateGate
            _rateGate.WaitToProceed()

            Using webResponse As WebResponse = webRequest__1.GetResponse()
                Using str As Stream = webResponse.GetResponseStream()
                    Using sr As New StreamReader(str)
                        Dim responseContent3 As String = sr.ReadToEnd
                        Return responseContent3
                    End Using
                End Using
            End Using
        Catch wex As WebException
            Using response As HttpWebResponse = DirectCast(wex.Response, HttpWebResponse)
                Using str As Stream = response.GetResponseStream()
                    Using sr As New StreamReader(str)
                        Dim responseContent3 As String = sr.ReadToEnd
                        Return responseContent3
                    End Using
                End Using

            End Using
        End Try
    End Function
    Private Function sha256_hash(value As [String]) As Byte()
        Using hash As SHA256 = SHA256Managed.Create()
            Dim enc As Encoding = Encoding.UTF8

            Dim result As [Byte]() = hash.ComputeHash(enc.GetBytes(value))

            Return result
        End Using
    End Function

    Private Function getHash(keyByte As Byte(), messageBytes As Byte()) As Byte()
        Using hmacsha512 = New HMACSHA512(keyByte)

            Dim result As [Byte]() = hmacsha512.ComputeHash(messageBytes)


            Return result
        End Using
    End Function

我不明白为什么这会一直返回一个无效的密钥,除了我对密钥进行编码的方式与在C#

中的方式不同之外。您的代码中存在一些问题,将字符串和字符组合在一起

要在VB中连接字符串,请使用
&
而不是“+”

您还使用了几个帮助函数进行哈希运算,这也可能会有问题


您最好同时在C#和VB中启动程序,并逐行检查每一行代码,直到找出两者之间的差异。

这是错误的:
props=Convert.ToString(“nonce=“+nonce.ToString())&props
。使用&在VB中组合字符串not+Good catch。但是,我尝试将所有的“+”符号更改为“&”,但仍然返回“无效密钥”。我不认为我可以一步一步地通过它找到问题所在,因为这与我发送密钥的方式有关。你仍然可以一行一行地通过它,确保每个变量在每一步都是相同的。你会在一行或多行中发现与C版本不匹配的东西。啊,好吧,我试试看。这一定是我做的蠢事。我发现了两个问题。我解决的一个问题是更改C代码,它是
code
var np=nonce+Convert.ToChar(0)+postData;,对于VBnet等效的
code
Dim np=nonce.ToString()&postData,现在我无法理解这一部分。C#
code
var z=new byte[pathBytes.Count()+hash256Bytes.Count()];给我一个50的长度,vbnet
code
Dim z=New Byte(pathBytes.Count()&(hash256Bytes.Count()-1)){}给我一个1832的长度。这里有什么想法吗?不幸的是,除了逐行比较程序中的变量和工作程序外,没有什么神奇的方法可以从一种语言转换到另一种语言。这种转换看起来像是由开发人员fusion完成的。你试过Telerik的转换器吗?