如何使用VBA Excel编码BASE64和SHA1 HMAC加密

时间:2013-01-03 21:27:59

标签: excel vba google-maps-api-3

我一直在互联网上搜索,此外还与谷歌服务台人员就此问题进行联络。到目前为止没有运气。

我正在尝试通过VBA中的Web服务调用使用Google API for Directions。 我的代码工作得很好,它从Web服务中提供了正确的结果。但是,如果我想发送超过几千个日常请求,他们需要在将数据发送给他们之前加密数据。

他们在下面有一个代码,但与VBA无关,仅适用于VB.net。

这里的任何人都熟悉这个代码在VBA中的可用性吗? VBA是否具有所需的库以及对代码的一些微小调整或?

Option Explicit On
Option Strict On

Imports System
Imports System.Security.Cryptography
Imports System.Text

Namespace SignUrl
    Public Module GoogleUrlSigner
        Public Function Sign(ByVal url As String, ByVal keyString As String) As String
            Dim encoding As ASCIIEncoding = New ASCIIEncoding()
        'URL-safe decoding
        Dim privateKeyBytes As Byte() = Convert.FromBase64String(keyString.Replace("-", "+").Replace("_", "/"))

        Dim objURI As Uri = New Uri(url)
        Dim encodedPathAndQueryBytes As Byte() = encoding.GetBytes(objURI.LocalPath & objURI.Query)

        'compute the hash
        Dim algorithm As HMACSHA1 = New HMACSHA1(privateKeyBytes)
        Dim hash As Byte() = algorithm.ComputeHash(encodedPathAndQueryBytes)

        'convert the bytes to string and make url-safe by replacing '+' and '/' characters
        Dim signature As String = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_")

        Add the signature to the existing URI.
        Return objURI.Scheme & "://" & objURI.Host & objURI.LocalPath & objURI.Query & "&signature=" & signature
    End Function
End Module

Public Module Program
    Sub Main()
        Const keyString As String = "vNIXE0xscrmjlyV-12Nj_BvUPaw="

        'The URL shown here is a static URL which should be already
        'URL-encoded. In practice, you will likely have code
        'which assembles your URL from user or web service input
        'and plugs those values into its parameters.
        Dim urlString As String = "http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"

        Console.WriteLine(GoogleUrlSigner.Sign(urlString, keyString))
    End Sub
End Module
End Namespace

0 个答案:

没有答案