How to generate signature with RSA-SHA1 and private key through VBA?

血红的双手。 提交于 2021-02-19 17:58:39

问题


I want to connect an API with Excel through Excel VBA. The API requires a public/private keypair with RSA-SHA1 signature. I have used openssl to generate the pair, uploaded the public key to the API's services and have the private part stored on my local computer.

Now I want to connect with my API so I will sign my request. But VBA does not have native RSA-SHA1 signing. I have found the following code at https://en.wikibooks.org/wiki/Visual_Basic_for_Applications/String_Hashing_in_VBA which signs a message with the help of .net but I cannot supply a private key to it..

Public Function SHA1(sIn As String, Optional bB64 As Boolean = 0) As String
  'Set a reference to mscorlib 4.0 64-bit

  'Test with empty string input:
  '40 Hex:   da39a3ee5e6...etc
  '28 Base-64:   2jmj7l5rSw0yVb...etc

  Dim oT As Object, oSHA1 As Object
  Dim TextToHash() As Byte
  Dim bytes() As Byte

  Set oT = CreateObject("System.Text.UTF8Encoding")
  Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed")

  TextToHash = oT.GetBytes_4(sIn)
  bytes = oSHA1.ComputeHash_2((TextToHash))

  If bB64 = True Then
     SHA1 = ConvToBase64String(bytes)
  Else
     SHA1 = ConvToHexString(bytes)
  End If

  Set oT = Nothing
  Set oSHA1 = Nothing    
End Function

What are some ways that I can sign with RSA-SHA1 from VBA with a key (following good practice) provided that I have the key ready and just needs to be loaded?

来源:https://stackoverflow.com/questions/49696129/how-to-generate-signature-with-rsa-sha1-and-private-key-through-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!