Friday, May 18, 2012

Belgium eID Card Reader Application

A friend of mine, Tim, asked me to write up a simple application
that's able to read out the Belgium ID cards.

This is what I ended up with...
A few things you need to know:
  • Reference the Interop.EIDLIBCTRLLib.dll
  • Target the x86 platform (the dll only exists in a 32bit flavour)




























eID Reader Binaries
eID Reader Source Code

The actual eID Object : were all the “Magic” Happens…

Friend Class eIDObject

#Region "Events"

    Protected Friend Event Init_Failed()
    Protected Friend Event Exit_Failed()
    Protected Friend Event Finished_InfoRetrieval()

#End Region

#Region "Variables"

    Protected Friend ErrorMessage As String

    Private lHandle As Integer = Nothing
    Private Eidlib1 As New EIDLIBCTRLLib.EIDlib
    Private RetStatus As EIDLIBCTRLLib.RetStatus = Nothing
    Private MapColPicture As New EIDLIBCTRLLib.MapCollection
    Private MapColID As New EIDLIBCTRLLib.MapCollection
    Private MapColAddress As New EIDLIBCTRLLib.MapCollection
    Private CertifCheck As New EIDLIBCTRLLib.CertifCheck

#End Region

#Region "BEID Properties"

#Region "BEID Card Info"

    Private strCardNumber As String
    Protected Friend ReadOnly Property CardNumber() As String
        Get
            Return strCardNumber
        End Get
    End Property

    Private strChipNumber As String
    Protected Friend ReadOnly Property ChipNumber() As String
        Get
            Return strChipNumber
        End Get
    End Property

    Private strIssuingMunicipality As String
    Protected Friend ReadOnly Property IssuingMunicipality() As String
        Get
            Return strIssuingMunicipality
        End Get
    End Property

    'START DATE - INITIAL DATE
    Private dateBeginValidityDate As Date
    Protected Friend ReadOnly Property BeginValidityDate() As Date
        Get
            Return dateBeginValidityDate
        End Get
    End Property

    Private strBegVal As Object

    Private strBegValDag As String
    Protected Friend ReadOnly Property BeginValidityDay() As String
        Get
            Return strBegValDag
        End Get
    End Property

    Private strBegValMaand As String
    Protected Friend ReadOnly Property BeginValidityMonth() As String
        Get
            Return strBegValMaand
        End Get
    End Property

    Private strBegValJaar As String
    Protected Friend ReadOnly Property BeginValidityYear() As String
        Get
            Return strBegValJaar
        End Get
    End Property

    'END DATE - EXPIRE DATE
    Private dateEndValidityDate As Date
    Protected Friend ReadOnly Property EndValidityDate() As Date
        Get
            Return dateEndValidityDate
        End Get
    End Property

    Private strEndVal As Object
    Private strEndValDag As String
    Protected Friend ReadOnly Property EndValidityDay() As String
        Get
            Return strEndValDag
        End Get
    End Property

    Private strEndValMaand As String
    Protected Friend ReadOnly Property EndValidityMonth() As String
        Get
            Return strEndValMaand
        End Get
    End Property

    Private strEndValJaar As String
    Protected Friend ReadOnly Property EndValidityYear() As String
        Get
            Return strEndValJaar
        End Get
    End Property

#End Region

#Region "BEID Person Info"
    Private strName As String
    Protected Friend ReadOnly Property Name() As String
        Get
            Return strName
        End Get
    End Property

    Private strFirstName1 As String
    Protected Friend ReadOnly Property FirstName() As String
        Get
            Return strFirstName1
        End Get
    End Property

    Private strFirstName2 As String
    Protected Friend ReadOnly Property FirstName2() As String
        Get
            Return strFirstName2
        End Get
    End Property

    Private strBirthPlace As String
    Protected Friend ReadOnly Property BirthPlace() As String
        Get
            Return strBirthPlace
        End Get
    End Property

    Private strBirthDate As String
    Protected Friend ReadOnly Property BirthDate() As String
        Get
            Return strBirthDate
        End Get
    End Property

    Private strGender As String
    Protected Friend ReadOnly Property Gender() As String
        Get
            Return strGender
        End Get
    End Property

    Private strNationality As String
    Protected Friend ReadOnly Property Nationality() As String
        Get
            Return strNationality
        End Get
    End Property

    Private strNationalNumber As String
    Protected Friend ReadOnly Property NationalNumber() As String
        Get
            Return strNationalNumber
        End Get
    End Property
#End Region

#Region "BEID Adres Info"
    Private strStreet As String
    Protected Friend ReadOnly Property Street() As String
        Get
            Return strStreet
        End Get
    End Property

    Private strStreetNumber As String
    Protected Friend ReadOnly Property StreetNumber() As String
        Get
            Return strStreetNumber
        End Get
    End Property

    Private strZIPCode As String
    Protected Friend ReadOnly Property ZIPCode() As String
        Get
            Return strZIPCode
        End Get
    End Property

    Private strMunicipality As String
    Protected Friend ReadOnly Property Municipality() As String
        Get
            Return strMunicipality
        End Get
    End Property

#End Region

#Region "BEID Picture Info"

    Private imgPicture As Global.System.Drawing.Image
    Protected Friend ReadOnly Property Picture() As Global.System.Drawing.Image
        Get
            Return imgPicture
        End Get

    End Property

#End Region

#End Region

#Region "Constructor"

    Protected Friend Sub New()
    End Sub

#End Region

#Region "Methods"

    Private Function eID_Init() As Boolean
        Try
            RetStatus = Eidlib1.Init("", 0, 0, lHandle)
            If (RetStatus.GetGeneral = 0) Then
                Return True
            Else
                ErrorMessage = "Unable to initialize eID Reader!"
                RaiseEvent Init_Failed()
                Return False
            End If
        Catch ex As Exception
            ErrorMessage = "Unable to initialize eID Reader!" + vbCrLf + GetErrorInfo(ex)
            RaiseEvent Init_Failed()
            Return False
        End Try
    End Function

    Private Function eID_Exit() As Boolean
        Try
            RetStatus = Eidlib1.Exit()
            Return True
        Catch ex As Exception
            ErrorMessage = "Unable to exit eID Reader!" + vbCrLf + GetErrorInfo(ex)
            RaiseEvent Exit_Failed()
            Return False
        Finally
            'release all objects
            RetStatus = Nothing
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try
    End Function

    Protected Friend Function RetrieveInformation() As Boolean
        'reset errormessage
        ErrorMessage = ""
        Try
            If eID_Init() Then
                ' get the info stored on the Belgium eID card & populate the properties
                RetStatus = Eidlib1.GetID(MapColID, CertifCheck)

                'card
                strChipNumber = MapColID.GetValue("ChipNumber")
                strCardNumber = MapColID.GetValue("CardNumber")
                strBegVal = MapColID.GetValue("BeginValidityDate")
                strEndVal = MapColID.GetValue("EndValidityDate")
                strIssuingMunicipality = MapColID.GetValue("IssuingMunicipality")
                '        'Opsplitsen begindatum
                strBegValDag = Microsoft.VisualBasic.Right(strBegVal, 2)
                strBegValMaand = Microsoft.VisualBasic.Mid(strBegVal, 5, 2)
                strBegValJaar = Microsoft.VisualBasic.Left(strBegVal, 4)
                '        'Opsplitsen einddatum
                strEndValDag = Microsoft.VisualBasic.Right(strEndVal, 2)
                strEndValMaand = Microsoft.VisualBasic.Mid(strEndVal, 5, 2)
                strEndValJaar = Microsoft.VisualBasic.Left(strEndVal, 4)
                dateBeginValidityDate = CDate(strBegValDag & "/" & strBegValMaand & "/" & strBegValJaar)
                dateEndValidityDate = CDate(strEndValDag & "/" & strEndValMaand & "/" & strEndValJaar)
                'person
                strName = MapColID.GetValue("Name")
                strFirstName1 = MapColID.GetValue("FirstName1")
                strFirstName2 = MapColID.GetValue("FirstName2")
                strBirthDate = MapColID.GetValue("BirthDate")
                'transform birthdate
                Dim d As String = ""
                Dim m As String = ""
                Dim y As String = ""

                d = Microsoft.VisualBasic.Right(strBirthDate, 2)
                m = Microsoft.VisualBasic.Mid(strBirthDate, 5, 2)
                y = Microsoft.VisualBasic.Left(strBirthDate, 4)
                strBirthDate = CDate(d & "/" & m & "/" & y)
                strBirthPlace = MapColID.GetValue("BirthPlace")
                strGender = MapColID.GetValue("Gender")
                strNationality = UCase(MapColID.GetValue("Nationality"))
                strNationalNumber = MapColID.GetValue("NationalNumber")
                'address
                RetStatus = Eidlib1.GetAddress(MapColAddress, CertifCheck)
                strStreet = Trim(MapColAddress.GetValue("Street")) 'Neerhofstraat 26
                'get street number
                strStreetNumber = strStreet.Substring(strStreet.LastIndexOf(" ") + 1)
                'get street
                strStreet = strStreet.Substring(0, strStreet.LastIndexOf(" "))
                strZIPCode = MapColAddress.GetValue("ZIPCode")
                strMunicipality = MapColAddress.GetValue("Municipality")
                'get picture
                Dim s As IO.MemoryStream = Nothing
                Dim o() As Byte = Nothing
                Dim raw As EIDLIBCTRLLib.Raw = Nothing
                Try
                    Eidlib1.GetRawData(raw)
                    raw.GetPictureData(o)
                    s = New IO.MemoryStream(o)
                    imgPicture = Global.System.Drawing.Image.FromStream(s)
                    s.Dispose()
                    s = Nothing
                Catch ex As Exception
                    ErrorMessage = "Unable to retrieve picture from eID card!" + vbCrLf + GetErrorInfo(ex)
                Finally
                    If Not s Is Nothing Then
                        s.Dispose()
                        s = Nothing
                    End If
                End Try
            Else
                Return False
            End If
            Return True
        Catch ex As Exception
            ErrorMessage = "Unable to retrieve information from eID card!" + vbCrLf + GetErrorInfo(ex)
            Return False
        Finally
            eID_Exit()
            RaiseEvent Finished_InfoRetrieval()
        End Try
    End Function

    Protected Friend Function ExportInformationToFile(ByVal filename As String) As Boolean
        Dim txtwriter As IO.StreamWriter = Nothing
        Try
            If Not String.IsNullOrEmpty(filename) Then

                txtwriter = New IO.StreamWriter(filename)
                txtwriter.WriteLine("CARDNUMBER=" + Me.CardNumber)
                txtwriter.WriteLine("BEGINVALIDITY=" + Me.BeginValidityDate)
                txtwriter.WriteLine("ENDVALIDITY=" + Me.EndValidityDate)
                txtwriter.WriteLine("ISSUINGMUNICIPALITY=" + Me.IssuingMunicipality)
                txtwriter.WriteLine("FIRSTNAME=" + Me.FirstName + IIf(Me.FirstName2 = "", "", " " + Me.FirstName2))
                txtwriter.WriteLine("SURNAME=" + Me.Name)
                txtwriter.WriteLine("GENDER=" + Me.Gender)
                txtwriter.WriteLine("NATIONALITY=" + Me.Nationality)
                txtwriter.WriteLine("BIRTHDATE=" + Me.BirthDate)
                txtwriter.WriteLine("BIRTHPLACE=" + Me.BirthPlace)
                txtwriter.WriteLine("NATIONALNUMBER=" + Me.NationalNumber)
                txtwriter.WriteLine("STREET=" + Me.Street)
                txtwriter.WriteLine("STREETNUMBER=" + Me.StreetNumber)
                txtwriter.WriteLine("ZIP=" + Me.ZIPCode)
                txtwriter.WriteLine("CITY=" + Me.Municipality)
                txtwriter.Close()
                Return True
            Else
                ErrorMessage = "Export Filename is empty!"
                Return False
            End If
        Catch ex As Exception
            ErrorMessage = "Unable to write eID card Information to file : " + filename + GetErrorInfo(ex)
            Return False
        Finally
            If Not txtwriter Is Nothing Then
                txtwriter.Dispose()
                txtwriter = Nothing
            End If
        End Try
    End Function

    Protected Friend Function ExportPictureToFile(ByVal filename As String) As Boolean
        Try
            If Not Me.Picture Is Nothing Then
                Me.Picture.Save(filename, Drawing.Imaging.ImageFormat.Jpeg)
                Return True
            Else
                ErrorMessage = "No picture found!"
                Return False
            End If
        Catch ex As Exception
            ErrorMessage = "Unable to export the picture to : " + filename + GetErrorInfo(ex)
            Return False
        End Try
    End Function

#End Region
   
End Class

6 comments:

  1. When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a
    comment is added I get several emails with the same comment.
    Is there any way you can remove people from that service?
    Cheers!

    Here is my weblog: Sac Louis Vuitton

    ReplyDelete
  2. In fact no matter if someone doesn't know afterward its up to other visitors that they will assist, so here it occurs.

    Also visit my website ... Cheap Louis Vuitton Handbags

    ReplyDelete
  3. Are you looking to make money from your websites/blogs by using popunder ads?
    If so, did you know about Propeller Ads?

    ReplyDelete
  4. System.Runtime.InteropServices.COMException
    HResult=0x80040154
    Message=Retrieving the COM class factory for component with CLSID {4E29F09E-EBCC-4A3B-BAA7-8372EC375870} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

    ReplyDelete
  5. Thanks for sharing. If you want to know more about the site Kaotic, here I just read a tutorial and hope it a help too: What Is Kaotic Website?

    ReplyDelete