Sample code for 30+ languages & platforms
Visual Basic 6.0

Get the Public Key from a Private Key

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.ToPublicKey method, which extracts the public portion of the loaded private key into an independent PublicKey object. The only argument is the PublicKey that receives the copy.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: A key pair's public half is derived from the private half, and this is how you obtain it — the public key you distribute for others to verify signatures or encrypt to you, while the private key stays secret. The resulting PublicKey is an independent copy, so exporting or sharing it never risks exposing the private material. From there, export it as PEM, JWK, or XML as needed.

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

'  Load a private key to export.
Dim privKey As New PrivateKey
success = privKey.LoadPemFile("qa_data/private.pem")
If (success = 0) Then
    Debug.Print privKey.LastErrorText
    Exit Sub
End If

'  Extract the public portion of the private key into an independent PublicKey object.  The
'  PublicKey is a separate copy, unaffected by later changes to the PrivateKey.
Dim pubKey As New PublicKey
success = privKey.ToPublicKey(pubKey)
If (success = 0) Then
    Debug.Print privKey.LastErrorText
    Exit Sub
End If

'  The PublicKey can now be exported or shared -- for example as PEM.  The argument prefers the
'  traditional PKCS #1 PEM when a traditional representation exists.
Dim preferPkcs1 As Long
preferPkcs1 = 1
Dim pubPem As String
pubPem = pubKey.GetPem(preferPkcs1)
If (pubKey.LastMethodSuccess = 0) Then
    Debug.Print pubKey.LastErrorText
    Exit Sub
End If

Debug.Print pubPem