Extracts a public key from an X.509 certificate.
VB6/VBA
Debug.Print "Testing RSA_GetPublicKeyFromCert ..." Dim strCertFile As String Dim strKeyFile As String Dim strPublicKey As String Dim nChars As Long Dim nRet As Long strCertFile = "AliceRSASignByCarl.cer" ' First find out the length of string we need nChars = RSA_GetPublicKeyFromCert(vbNullString, 0, strCertFile, 0) Debug.Print "RSA_GetPublicKeyFromCert returns " & nChars & " (expecting +ve)" If nChars <= 0 Then Debug.Print "ERROR: " & pkiErrorLookup(nChars) Exit Sub End If ' Pre-dimension the string to receive data - IMPORTANT strPublicKey = String(nChars, " ") ' Read in the Public Key in our "internal" format nRet = RSA_GetPublicKeyFromCert(strPublicKey, nChars, strCertFile, 0) Debug.Print "Public key is " & RSA_KeyBits(strPublicKey) & " bits long" ' Now save as a PKCS#1 public key file strKeyFile = "AlicePubRSA.pub" nRet = RSA_SavePublicKey(strKeyFile, strPublicKey, 0) Debug.Print "RSA_SavePublicKey returns " & nRet If nRet = 0 Then Debug.Print "Saved as public key file '" & strKeyFile & "'" Else Debug.Print "ERROR: " & pkiErrorLookup(nRet) End If
Output
Testing RSA_GetPublicKeyFromCert ... RSA_GetPublicKeyFromCert returns 220 (expecting +ve) Public key is 1024 bits long RSA_SavePublicKey returns 0 Saved as public key file 'AlicePubRSA.pub'
VB.NET
Console.WriteLine("Testing RSA_GetPublicKeyFromCert ...")
Dim strCertFile As String
Dim strKeyFile As String
Dim sbPublicKey As StringBuilder
Dim nRet As Integer
strCertFile = "AliceRSASignByCarl.cer"
sbPublicKey = Rsa.GetPublicKeyFromCert(strCertFile)
Console.WriteLine("RSA_GetPublicKeyFromCert returns " & sbPublicKey.Length & " (expecting +ve)")
If sbPublicKey.Length = 0 Then
Console.WriteLine("ERROR: " & General.LastError())
Exit Sub
End If
Console.WriteLine("Public key is " & Rsa.KeyBits(sbPublicKey.ToString()) & " bits long")
' Now save as a PKCS#1 public key file
strKeyFile = "AlicePubRSA.pub"
nRet = Rsa.SavePublicKey(strKeyFile, sbPublicKey.ToString(), 0)
Console.WriteLine("RSA_SavePublicKey returns " & nRet)
If nRet = 0 Then
Console.WriteLine("Saved as public key file '" & strKeyFile & "'")
Else
Console.WriteLine("ERROR: " & General.LastError())
End If
[Contents]