Public key to string

StewartBW 1,145 Reputation points
2024-06-23T02:45:50.3466667+00:00

Hello experts

I need to insert a certificate public key to a vcf contact file as Base64 string.

According to RFC 2426 section 3.7.2

It's like:

KEY;X509;ENCODING=BASE64:
 A1UEBhMCSVQxEDAOBgNVBAgCB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8x
 A1UEBhMCSVQxEDAOBgNVBAgCB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8x
 fNtkzg==

On the beginning of each line there's space, then 72 chars and line wrap, this is the public key indeed, anyone knows about the existing functions in .net 4 that will do the task?

Thanks in advance.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,734 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,056 Reputation points Microsoft Vendor
    2024-06-24T02:30:45.04+00:00

    Hi @StewartBW ,

    You can use the Convert.ToBase64String method to convert the certificate to a Base64 string.

    Then format the Base64 string to adhere to the RFC 2426 specification by spliting the Base64 string into 72-character lines, prefixed by a space.

        Function FormatBase64String(ByVal base64String As String) As String
            Const lineLength As Integer = 72
            Dim formattedString As New StringBuilder()
            Dim currentIndex As Integer = 0
    
            While currentIndex < base64String.Length
                Dim length As Integer = Math.Min(lineLength, base64String.Length - currentIndex)
                formattedString.Append(" ") ' Add a space at the beginning of each line
                formattedString.AppendLine(base64String.Substring(currentIndex, length))
                currentIndex += length
            End While
    
            Return formattedString.ToString()
        End Function
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.