CryptoSys PKI examples VB6 to VB.NET

PEM_FileFromBinFile

Converts a binary file into a PEM file.

VB6/VBA

Debug.Print "Testing PEM_FileFromBinFile ..."
Dim nRet As Long
Dim strBinFile As String
Dim strPEMFile As String
Dim strDigest As String

' Input file is a DER-encoded X.509 certificate
' (at 227 bytes, the smallest we could devise)
strBinFile = "smallca.cer"
strPEMFile = "smallca.pem.cer"

' Convert to a PEM file
nRet = PEM_FileFromBinFile(strPEMFile, strBinFile, "CERTIFICATE", 72)
Debug.Print "PEM_FileFromBinFile returns " & nRet & " (expecting 0)"

' To prove we did it properly, compute the thumbprint of the two certs
strDigest = String(PKI_SHA1_CHARS, " ")
nRet = X509_CertThumb(strBinFile, strDigest, Len(strDigest), PKI_HASH_SHA1)
If nRet > 0 Then
    Debug.Print "SHA-1(der-file)=" & strDigest
Else
    Debug.Print "ERROR: computing cert thumb"
End If
nRet = X509_CertThumb(strPEMFile, strDigest, Len(strDigest), PKI_HASH_SHA1)
If nRet > 0 Then
    Debug.Print "SHA-1(pem-file)=" & strDigest
Else
    Debug.Print "ERROR: computing cert thumb"
End If

Output

Testing PEM_FileFromBinFile ...
PEM_FileFromBinFile returns 0 (expecting 0)
SHA-1(der-file)=a36b1bfa0af41a2785066b2d5135b67011ac3b7f
SHA-1(pem-file)=a36b1bfa0af41a2785066b2d5135b67011ac3b7f

VB.NET

Console.WriteLine("Testing PEM_FileFromBinFile ...")
Dim nRet As Integer
Dim strBinFile As String
Dim strPEMFile As String
Dim strDigest As String

' Input file is a DER-encoded X.509 certificate
' (at 227 bytes, the smallest we could devise)
strBinFile = "smallca.cer"
strPEMFile = "smallca.pem.cer"

' Convert to a PEM file
nRet = Pem.FileFromBinFile(strPEMFile, strBinFile, "CERTIFICATE", 72)
Console.WriteLine("PEM_FileFromBinFile returns " & nRet & " (expecting 0)")

' To prove we did it properly, compute the thumbprint of the two certs
strDigest = X509.CertThumb(strBinFile, HashAlgorithm.Sha1)
If strDigest.Length > 0 Then
    Console.WriteLine("SHA-1(der-file)=" & strDigest)
Else
    Console.WriteLine("ERROR: computing cert thumb")
End If
strDigest = X509.CertThumb(strPEMFile, HashAlgorithm.Sha1)
If strDigest.Length > 0 Then
    Console.WriteLine("SHA-1(pem-file)=" & strDigest)
Else
    Console.WriteLine("ERROR: computing cert thumb")
End If

[Contents]

[HOME]   [NEXT: PEM_FileToBinFile...]

Copyright © 2010-20 D.I. Management Services Pty Ltd. All rights reserved.