Showing posts with label Applications. Show all posts
Showing posts with label Applications. Show all posts

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

Wednesday, May 2, 2012

PDF Layer Merger

 

Recently I’ve created a simple .NET tool, to merge 2 PDF files, one as a layer on top of the other.

The reason behind this, was to be able to digitally remark/comment/mark-up on a PDF file using AutoCAD.  People tend to print out a PDF file, use some highlighters and draw some clouds and comments on this PDF, only to re-scan this afterwards.
This is the way we used to work, but today we live in a digital era…

The tool has been built in VB.NET targeting the NET 2.0 framework, with the use of the excellent PDF library iTextSharp (based on iText).

image


Snippet of the actual transformation function:

  
'''
''' Combines two source pdf's to one single pdf.
'''

''' The file-location incl. the name of the original pdf.
''' The file-location incl. the name of the mark-up pdf.
''' The file-location incl. the name of the generated combined pdf.
''' The name which the added mark-up layer will have in the new pdf.
''' The transformation which is executed on the original pdf: rotation, scaleX, scaleY, offsetX, offsetY
''' Return boolean indication whether the combination of the pdf's was successful.
'''
Public Function PDFCombineLayer(ByVal sourceOriginal As String, ByVal sourceMarkup As String, ByVal destination As String, Optional ByVal NewLayer As String = "SOURCE-DRAWING", Optional ByVal Transform As String = "0;1;1;0;0") As Boolean
Dim oPdfOriginal As iTextSharp.text.pdf.PdfReader = Nothing
Dim oPdfMarkup As iTextSharp.text.pdf.PdfReader = Nothing
Dim oPdfStamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim PDFDirectContent As iTextSharp.text.pdf.PdfContentByte
Dim iNumberOfPages As Integer = Nothing
Dim iPage As Integer = 0
Dim matrix1 As Drawing2D.Matrix = Nothing
Dim matrix2 As Drawing2D.Matrix = Nothing

Try
'Read the source pdf-files and prepare new file.
oPdfOriginal = New iTextSharp.text.pdf.PdfReader(sourceOriginal)
oPdfMarkup = New iTextSharp.text.pdf.PdfReader(sourceMarkup)
oPdfStamper = New iTextSharp.text.pdf.PdfStamper(oPdfMarkup, New IO.FileStream(destination, IO.FileMode.Create, IO.FileAccess.Write))

'Count pages in original pdf and create new pdf wih same amount of pages.
iNumberOfPages = oPdfOriginal.NumberOfPages
oPdfStamper.GetPdfLayers()

'Make transformation matrix
If Transform = "" Then Transform = "0;1;1;0;0" 'Note transform: rotation(degrees), scaleX, scaleY, offsetX, offsetY
Dim ar() As String = Split(Transform, ";")
If Not ar.Length = 5 Then ar = New String() {0, 1, 1, 0, 0}
If ar(1) <= 0 Or ar(2) <= 0 Then Throw New ArgumentOutOfRangeException("transform", "Make sure that scaling factors of the transformation are larger than zero.")
matrix1 = New Drawing2D.Matrix
matrix1.Rotate(-CSng(ar(0)))
matrix1.Scale(CSng(ar(1)), CSng(ar(2)))
matrix1.Translate(CSng(ar(3)), CSng(ar(4)))
matrix2 = New Drawing2D.Matrix

'Add the new layer to each of the pdf-pages.
If NewLayer = "" Then NewLayer = "SOURCE-DRAWING"
Dim PDFNewLayer As New iTextSharp.text.pdf.PdfLayer(NewLayer, oPdfStamper.Writer)
Dim PDFLayerMem As New iTextSharp.text.pdf.PdfLayerMembership(oPdfStamper.Writer)
PDFLayerMem.AddMember(PDFNewLayer)
Do While (iPage < iNumberOfPages)
'Get new page
iPage += 1
Dim iPageOriginal As iTextSharp.text.pdf.PdfImportedPage = oPdfStamper.Writer.GetImportedPage(oPdfOriginal, iPage)
PDFDirectContent = oPdfStamper.GetUnderContent(iPage) 'add page under existing pdf in new layer
matrix2 = matrix1.Clone 'Cloning is necessary, otherwise all adaptations to matrix2 are also done in matrix1.

'Compensate for the orientation of the original pdf-file.
Dim iRotation As Single = oPdfOriginal.GetPageRotation(iPage)
Select Case iRotation
Case Is = 90
matrix2.Rotate(-iRotation)
matrix2.Translate(-oPdfOriginal.GetPageSize(iPage).Width, 0)
Case Is = 180 'Not tested yet.
matrix2.Rotate(-iRotation)
matrix2.Translate(-oPdfOriginal.GetPageSize(iPage).Width, -oPdfOriginal.GetPageSize(iPage).Height)
Case Is = 270 'Not tested yet.
matrix2.Rotate(-iRotation)
matrix2.Translate(0, oPdfOriginal.GetPageSize(iPage).Height)
Case Else
End Select

'Add the new layer ot this page.
PDFDirectContent.BeginLayer(PDFNewLayer)
PDFDirectContent.AddTemplate(iPageOriginal, matrix2)
PDFDirectContent.EndLayer()
Loop
oPdfStamper.SetFullCompression()
Return True
Catch ex As Exception
Return False
Finally
oPdfMarkup.Close()
oPdfOriginal.Close()
oPdfStamper.Close()
matrix1.Dispose()
matrix2.Dispose()
End Try
End Function

Code has been posted on Google Code http://code.google.com/p/pdflayermerger/


Many thanks to iText for the excellent library and to Lukasz Swiatkowski for the glass button component (http://www.codeproject.com/Articles/17695/Creating-a-Glass-Button-using-GDI).

Sunday, January 9, 2011

Secunia Personal Software Inspector (PSI)

 

Upon stumbling through the web, I found a nice tool to check the installed software for security issues and out-dated programs.
So I thought I’d shared it with you…

http://secunia.com/vulnerability_scanning/personal/

They even have an online scanning tool, so you don’t need to install the application http://secunia.com/vulnerability_scanning/online/

Screenshot PSI application :image

FREE PC Security for Home Users

The Secunia PSI is a FREE security tool designed to detect vulnerable and out-dated programs and plug-ins which expose your PC to attacks. Attacks exploiting vulnerable programs and plug-ins are rarely blocked by traditional anti-virus and are therefore increasingly "popular" among criminals.
The only solution to block these kind of attacks is to apply security updates, commonly referred to as patches.
Patches are offered free-of-charge by most software vendors, however, finding all these patches is a tedious and time consuming task.
Secunia PSI automates this and alerts you when your programs and plug-ins require updating to stay secure.
Download the Secunia PSI now and secure your PC today - free-of-charge.


Secunia PSI Watch: How to install and use the Secunia PSI 2.0

Current version:
2.0

Latest release:
5th Jan, 2011

File size:
1,737,088 bytes

Alternate download link: ftp://ftp.secunia.com/PSISetup.exe

Older version of the Secunia PSI can be downloaded at:

http://secunia.com/PSI1Setup.exe

System Requirements

The list of requirements that must be met for the Secunia PSI to function correctly are:

Supported Operating Systems (32 & 64 bit):
  • Microsoft Windows 7
  • Microsoft Windows Vista SP 1 or later
  • Microsoft Windows XP - SP 3
Privileges

To install and run the Secunia PSI you require administrative privileges.

Connectivity

Access to Secunia's encrypted servers via SSL (https://psi.secunia.com:443/) and access to Microsoft Windows Update servers,
see also Software Requirements below

Software Requirements

The latest version of Microsoft Update.
You can determine whether or not you are running the latest version by visiting update.microsoft.com.
If you are able to check your system for missing updates through this tool, your system should function properly with the Secunia PSI.

Hardware Requirements:

There are no additional hardware requirements.
If your computer runs any of the above mentioned Operating Systems, then Secunia PSI should work.