Click here to Skip to main content
15,878,959 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.1K   342  
Describes a framework to use the PCSC Smart Card API with .NET.
using System;
using System.Collections.Generic;
using System.Text;

namespace GemCard
{
    /// <summary>
    /// This class is used to update a set of parameters of an APDUCommand object
    /// </summary>
    public class APDUParam
    {
        byte
            m_bClass = 0,
            m_bChannel = 0,
            m_bP2 = 0,
            m_bP1 = 0;
        byte[] m_baData = null;
        short m_nLe = -1;
        bool 
            m_fUseP1 = false,
            m_fUseP2 = false,
            m_fChannel = false,
            m_fData = false,
            m_fClass = false,
            m_fLe = false;

        #region Constructors
        public APDUParam()
        {
        }

        /// <summary>
        /// Copy constructor (used for cloning)
        /// </summary>
        /// <param name="param"></param>
        public APDUParam(APDUParam param)
        {
            // Copy field
            if (param.m_baData != null)
                param.m_baData.CopyTo(m_baData, 0);
            m_bClass = param.m_bClass;
            m_bChannel = param.m_bChannel;
            m_bP1 = param.m_bP1;
            m_bP2 = param.m_bP2;
            m_nLe = param.m_nLe;

            // Copy flags field
            m_fChannel = param.m_fChannel;
            m_fClass = param.m_fClass;
            m_fData = param.m_fData;
            m_fLe = param.m_fLe;
            m_fUseP1 = param.m_fUseP1;
            m_fUseP2 = param.m_fUseP2;
        }

        public APDUParam(byte bClass, byte bP1, byte bP2, byte[] baData, short nLe)
        {
            this.Class = bClass;
            this.P1 = bP1;
            this.P2 = bP2;
            this.Data = baData;
            this.Le = (byte)nLe;
        }
        #endregion

        /// <summary>
        /// Clones the current APDUParam instance
        /// </summary>
        /// <returns></returns>
        public APDUParam Clone()
        {
            return new APDUParam(this);
        }

        /// <summary>
        /// Resets the current instance, all flags are set to false
        /// </summary>
        public void Reset()
        {
            m_bClass = 0;
            m_bChannel = 0;
            m_bP2 = 0;
            m_bP1 = 0;

            m_baData = null;
            m_nLe = -1;

            m_fUseP1 = false;
            m_fUseP2 = false;
            m_fChannel = false;
            m_fData = false;
            m_fClass = false;
            m_fLe = false;
        }

        #region Flags properties
        public bool UseClass
        {
            get { return m_fClass; }
        }

        public bool UseChannel
        {
            get { return m_fChannel; }
        }

        public bool UseLe
        {
            get { return m_fLe; }
        }

        public bool UseData
        {
            get { return m_fData; }
        }

        public bool UseP1
        {
            get { return m_fUseP1; }
        }

        public bool UseP2
        {
            get { return m_fUseP2; }
        }
        #endregion

        #region Parameter properties
        public byte P1
        {
            get { return m_bP1; }

            set
            {
                m_bP1 = value;
                m_fUseP1 = true;
            }
        }

        public byte P2
        {
            get { return m_bP2; }
            set
            {
                m_bP2 = value;
                m_fUseP2 = true;
            }

        }

        public byte[] Data
        {
            get { return m_baData; }
            set
            {
                m_baData = value;
                m_fData = true;
            }
        }

        public byte Le
        {
            get { return (byte)m_nLe; }
            set
            {
                m_nLe = value;
                m_fLe = true;
            }
        }

        public byte Channel
        {
            get { return m_bChannel; }
            set
            {
                m_bChannel = value;
                m_fChannel = true;
            }
        }

        public byte Class
        {
            get { return m_bClass; }
            set
            {
                m_bClass = value;
                m_fClass = true;
            }
        }

        #endregion
    }
}

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