Click here to Skip to main content
15,885,885 members
Articles / Web Development / HTML

A Smart Card Framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (102 votes)
15 May 2015CPOL8 min read 2.1M   145.2K   342  
Describes a framework to use the PCSC Smart Card API with .NET.
using System;
using System.Text;

namespace GemCard
{
	/// <summary>
	/// This class represents the APDU response sent by the card
	/// </summary>
	public class APDUResponse
	{
		/// <summary>
		///	Status bytes length
		/// </summary>
		public const int SW_LENGTH = 2;		

		private byte[]	m_baData = null;	
		private byte	m_bSw1;
		private byte	m_bSw2;

		/// <summary>
		/// Constructor from the byte data sent back by the card
		/// </summary>
		/// <param name="baData">Buffer of data from the card</param>
		public APDUResponse(byte[] baData)
		{
			if (baData.Length > SW_LENGTH)
			{
				m_baData = new byte[baData.Length - SW_LENGTH];

				for (int nI = 0; nI < baData.Length - SW_LENGTH; nI++)
					m_baData[nI] = baData[nI];
			}

			m_bSw1 = baData[baData.Length - 2];
			m_bSw2 = baData[baData.Length - 1];
		}

		/// <summary>
		/// Response data get property. Contains the data sent by the card minus the 2 status bytes (SW1, SW2)
		/// null if no data were sent by the card
		/// </summary>
		public	byte[]	Data
		{
			get
			{
				return m_baData;
			}
		}

		/// <summary>
		/// SW1 byte get property
		/// </summary>
		public byte	SW1
		{
			get
			{
				return m_bSw1;
			}
		}

		/// <summary>
		/// SW2 byte get property
		/// </summary>
		public byte	SW2
		{
			get
			{
				return m_bSw2;
			}
		}

		/// <summary>
		/// Status get property
		/// </summary>
		public	ushort	Status
		{
			get
			{
				return (ushort) (((short) m_bSw1 << 8) + (short) m_bSw2);
			}
		}

        /// <summary>
        /// Overrides the ToString method to format to a string the APDUResponse object
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string sRet;
    
            // Display SW1 SW2
            sRet = string.Format("SW={0:X04}", Status);

            if (m_baData != null)
            {
                StringBuilder sData = new StringBuilder(m_baData.Length * 2);
                for (int nI = 0; nI < m_baData.Length; nI++)
                    sData.AppendFormat("{0:X02}", m_baData[nI]);

                sRet += " Data=" + sData.ToString();
            }

            return sRet;
        }
	}
}

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)


Written By
Architect Connect In Private
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've been back into business to design the licensing system for the enterprise solution for Consistel using a .NET smart card (yes they can run .NET CLR!)

I'm currently designing a micro-payment solution using the NXP DESFire EV1 with the ACSO6 SAM of ACS. I can then add a full proficient expertise on those systems and NFC payments.
This technology being under strict NDA by NXP I cannot publish any related article about it, however I can provide professional consulting for it.

You can contact me for professional matter by using the forum or via my LinkedIn profile.

Comments and Discussions