Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Smart Card Framework for .NET

, 20 Feb 2013 CPOL
Describes a framework to use the PCSC Smart Card API with .NET.
smartcardapi_demo.zip
Interop.GemCardExLib.dll
Interop.SCARDSSPLib.dll
DemoSCFmwk.exe
GemCard.dll
GemCardEx.dll
smartcardapi_doc.zip
smartcardapi_doc
banner.jpg
darkcorner.jpg
GemCard
gradleft.jpg
gradtop.jpg
graycorner.jpg
minus.jpg
plus.jpg
titletile.jpg
smartcardapi_source.zip
Smartcard_API
DemoSCFmwk
Properties
GemCardEx
GemCardEx.aps
GemCardEx.def
GemCardEx.rgs
GemCardEx.suo
GemCardEx.vcproj.CORUSCANT.han.user
GemCardEx.vcproj.vspscc
GemCardExps.def
Release
GemCardEx.dll
SCardDatabaseEx.rgs
Smartcard Framework 2005.suo
GemCard
GemCard.csproj.user
SmartcardFrameworkWithWCFSCardService.zip
SmartcardFramework
DemoSCardService
Properties
Service References
SCardNPService
configuration.svcinfo
configuration91.svcinfo
DemoSCardService.SCardNPService.APDUResponse.datasource
Reference.svcmap
service.wsdl
SCardService
configuration.svcinfo
configuration91.svcinfo
DemoSCardService.SCardService.APDUResponse.datasource
Reference.svcmap
service.wsdl
SCardServiceHost
Properties
Settings.settings
Smartcard Framework 2010.suo
Smartcard Framework 2010.v11.suo
Smartcard_API
DemoSCFmwk
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
Thumbs.db
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
SmartcardService
Properties
SmartcardService.csproj.user
Smartcard_Framework_2010.zip
Smartcard_Framework_2010
Smartcard Framework 2010.suo
Smartcard_API
DemoSCFmwk
DemoSCFmwk.csproj.user
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
Smartcard_Framework_64.zip
Smartcard_Framework
Smartcard_API
DemoSCFmwk
Properties
GemCard
GemCard.csproj.user
SmartCardPlayer
Properties
Smartcard_Test
ApduExchange
ApduExchange.csproj.user
App.ico
GSMHelper
Properties
ReadPhonebook
Properties
Settings.settings
ReadPhonebook.csproj.user
using System;
using System.Collections.Generic;
using System.Text;

namespace GSMHelper
{
    public class PhoneNumber
    {
        public const int   
            TON_INTERNATIONAL = 1,
            TON_UNKNOWN = 0,
            TON_NATIONAL_NUMBER =2;
        
        public const int	NPI_ISDN = 1;

        public const int 
            GSM_MAX_DIAL_LENGTH = 20,
            GSM_MIN_DIAL_REC_LENGTH = 14,
            GSM_DIAL_BYTES = 10;

        const byte
            GSM_TON_MASK = 0x70,
            GSM_NPI_MASK = 0x0F,
            HIGH_MASK = 0xF0,
            LOW_MASK = 0x0F,
            END_OF_NUMBER = 0xFF; 

        const int 
            POS_LENGTHNUMBER = 0,
            POS_TONNPI = 1,
            POS_DIALLING = 2;

        // 7 bits default alphabet to ANSI coding
        // [Low Byte][High Byte]
        char[,]    DEFAULT_TO_ANSI = new char[,]
        {       
         /*  0    1    2    3    4    5    6    7  */
/* 0 */    {'@', ' ', ' ', '0', '�', 'P', '�', 'p'},
/* 1 */    {'�', ' ', '!', '1', 'A', 'Q', 'a', 'q'},
/* 2 */    {'$', '�', '"', '2', 'B', 'R', 'b', 'r'},
/* 3 */    {'�', ' ', '#', '3', 'C', 'S', 'c', 's'},
/* 4 */    {'�', ' ', '�', '4', 'D', 'T', 'd', 't'},
/* 5 */    {'�', ' ', '%', '5', 'E', 'U', 'e', 'u'},
/* 6 */    {'�', ' ', '&', '6', 'F', 'V', 'f', 'v'},
/* 7 */    {'�', ' ', '\'','7', 'G', 'W', 'g', 'w'},
/* 8 */    {'�', ' ', '(', '8', 'H', 'X', 'h', 'x'},
/* 9 */    {'�', ' ', ')', '9', 'I', 'Y', 'i', 'y'},
/* A */    {(char)0x0A,' ', '*', ':', 'J', 'Z', 'j', 'z'},
/* B */    {'�', ' ', '+', ';', 'K', '�', 'k', '�'},
/* C */    {'�', '�', ',', '<', 'L', '�', 'l', '�'},
/* D */    {(char)0x0D,'�', '-', '=', 'M', '�', 'm', '�'},
/* E */    {'�', '�', '.', '>', 'N', '�', 'n', '�'},
/* F */    {'�', '�', '/', '?', 'O', '�', 'o', '�'}
        };  

        byte[] m_data;

        /// <summary>
        /// Build the 
        /// </summary>
        /// <param name="data"></param>
        public PhoneNumber(byte[] data)
        {
            m_data = data;
        }


        /// <summary>
        /// Return true if the number is empty
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                bool ret = true;

                for (int nI = 0; nI < m_data.Length; nI++)
                {
                    if (m_data[nI] != END_OF_NUMBER)
                    {
                        ret = false;
                        break;
                    }
                }

                return ret;
            }
        }


            /// <summary>
            /// Gets the Identifier value for this Phone number
            /// </summary>
            public string Identifier
        {
            get
            {
                short nId;
                StringBuilder identifier = new StringBuilder();

                if (IdentifierLength != 0)
                {
                    nId = 0;
                    while ((m_data[nId] != 0xFF) &&
                          (nId < IdentifierLength))
                    {
                        identifier.Append(ToANSI(m_data[nId++]));
                    }
                }

                return identifier.ToString();

            }
        }


        /// <summary>
        /// Type of Numbering, this is used to know if the number is International or local;
        /// </summary>
        public int TypeOfNumbering
        {
            get
            {
                return (m_data[IdentifierLength + POS_TONNPI] & GSM_TON_MASK) >> 4;
            }
        }


        /// <summary>
        /// Gets the number for this Phone number
        /// </summary>
        public string Number
        {
            get
            {
                StringBuilder number = new StringBuilder();
                int nI = 0;
                byte
                    bHigh,
                    bLow;

                byte[] m_address = new byte[GSM_MIN_DIAL_REC_LENGTH];
                Buffer.BlockCopy(m_data, IdentifierLength, m_address, 0, GSM_MIN_DIAL_REC_LENGTH);

                while ((m_address[POS_DIALLING + nI] != END_OF_NUMBER) &&
                      (nI < NumberLength))
                {
                    bHigh = (byte)(m_address[POS_DIALLING + nI] & HIGH_MASK);
                    bLow = (byte)(m_address[POS_DIALLING + nI] & LOW_MASK);

                    number.Append(GetASCII(bLow));
                    if (bHigh == HIGH_MASK)
                    {
                        break;
                    }
                    else
                    {
                        bHigh >>= 4;
                        number.Append(GetASCII(bHigh));

                        nI += 1;
                    }
                }

                // Manage international number
                if (TypeOfNumbering == TON_INTERNATIONAL)
                {
                    number.Insert(0, '+');
                }

                return number.ToString();
            }
        }


        /// <summary>
        /// Gets the ANSI character for a GSM character 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private char ToANSI(short data)
        {
            byte bHigh, bLow;

            bHigh = (byte)((data & 0x70) >> 4);
            bLow = (byte)(data & 0x0F);

            return DEFAULT_TO_ANSI[bLow, bHigh];
        }


        /// <summary>
        /// Gets the ASCII character for Phone number digit
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private char GetASCII(byte data)
        {
            char cDig = ' ';

            if (data <= 9)
            {
                cDig = (char)(((char)data) + '0');
            }
            else
            {
                switch (data)
                {
                    case 0x0A:
                        {
                            cDig = '*';
                            break;
                        }

                    case 0x0B:
                        {
                            cDig = '#';
                            break;
                        }

                    case 0x0C:
                        {
                            cDig = 'P';
                            break;
                        }

                    case 0x0D:
                        {
                            cDig = 'B';
                            break;
                        }

                    case 0x0E:
                        {
                            cDig = 'C';
                            break;
                        }
                }
            }

            return cDig;
        }


        /// <summary>
        /// Gets the length of the phone number in bytes
        /// </summary>
        private int NumberLength
        {
            get
            {
                return GSM_DIAL_BYTES;
            }
        }


        /// <summary>
        /// Gets the length of the Identifier
        /// </summary>
        private int IdentifierLength
        {
            get
            {
                return m_data.Length - GSM_MIN_DIAL_REC_LENGTH;
            }
        }


        /// <summary>
        /// Gets the number of bytes used for the Phone number, this includes the TON & NPI byte
        /// </summary>
        private int NumberOfBytesForPhoneNumber
        {
            get
            {
                return m_data[IdentifierLength + POS_LENGTHNUMBER] - 1;
            }   
        }


        /// <summary>
        /// Gets a string representation of the phone number
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string ret = "Not Used";

            if (!IsEmpty)
            {
                ret = string.Format(Identifier + ": " + Number);
            }

            return ret;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

orouit
Architect Consistel - Singapore
Singapore Singapore
Software Architect, COM, .NET and Smartcard based security specialist.
 
I've been working in the software industry since I graduated in Electrical and Electronics Engineering. I chose software because I preferred digital to analog.
 
I started to program with 6802 machine code and evolved to the current .NET technologies... that was a long way.
 
For more than 20 years I have always worked in technical positions as I simply like to get my hands dirty and crack my brain when things don't go right!
 
After 12 years in the smart card industry I can claim a strong knowledge in security solutions based on those really small computers! I'm currently back in the business to design the licensing system for the enterprise solution I'm currenly working on, using a .NET smart card (yes they can run .NET CLR!)
 
View my profile on LinkedIn
 
You can contact me for professional consulting by using the forum.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 20 Feb 2013
Article Copyright 2006 by orouit
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid