IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

lire une carte a puce en c#


Sujet :

C#

  1. #1
    Membre à l'essai
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2011
    Messages : 34
    Points : 17
    Points
    17
    Par défaut lire une carte a puce en c#
    bonjour ts le monde je cherche a charger mes cartes a Puce tt en passant par une interface graphique crée depuis c-sharp
    en cherchant un peu sur la toile et en consultant quelques projets effectués l annee dernier, j ai pu tomber sur ce bout de code , mais j'ai rien Pigée , est ce que quelqu'un pourrai m'aider a résoudre ce probleme Merci ts le monde

    PS : la carte utilisé est une carte Fox LX 832

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Configuration;
     
    namespace Gestion
    {
        public partial class LireCarte : Form
        {
            public LireCarte()
            {
                InitializeComponent();
                Actualiser();
                Init();
            }
     
            void Actualiser()
            {
                this.utilisateurTableAdapter.Fill(this.gestionDataSet.utilisateur);
            }
     
    //----------------Initialisation des Variables générales--------------------------------------
     
            public ModWinsCard.SCARD_IO_REQUEST ioRequest = new ModWinsCard.SCARD_IO_REQUEST();
            int hContext;
            int retCode;
            int ActiveProtocol;
            int hCard;
            byte[] SendBuff = new byte[262];
            byte[] RecvBuff = new byte[262];
            int SendBuffLen, RecvBuffLen;
            string tmpStr;
            int indx;
     
    //---------------------------------------------------------------------------------------------------
    //-------------------------------RàZ des Buffers------------------------------------------------------------------------------------
            private void ClearBuffers()
            {
                long indx;
     
                for (indx = 0; indx < 262; indx++)
                {
                    RecvBuff[indx] = 0x00;
                    SendBuff[indx] = 0x00;
                }
            }
    //----------------------------------------------------------------------------------------------------------------------------------
    //-------------------------Initialisation ACR------------------------------------------------------------------
     
     
            void Init()
            {
     
                //Initialize List of Available Readers
                int pcchReaders = 0;
                int indx = 0;
                string rName = "";
     
                //Establish Context
                retCode = ModWinsCard.SCardEstablishContext(ModWinsCard.SCARD_SCOPE_USER, 0, 0, ref hContext);
     
                if (retCode != ModWinsCard.SCARD_S_SUCCESS)
                {
                    textBoxLire.Text = "SCardEstablishContext Error: ";
                    return;
                }
                else
                {
                    textBoxLire.Text = "SCardEstablishContext Success";
                }
     
                //Get readers buffer len
                retCode = ModWinsCard.SCardListReaders(hContext, null, null, ref pcchReaders);
     
                if (retCode != ModWinsCard.SCARD_S_SUCCESS)
                {
                    textBoxLire.Text = "SCardListReaders Error: ";
                    return;
                }
     
                //Set reader buffer size
                byte[] ReadersList = new byte[pcchReaders];
     
                // Fill reader list
                retCode = ModWinsCard.SCardListReaders(hContext, null, ReadersList, ref pcchReaders);
     
                if (retCode != ModWinsCard.SCARD_S_SUCCESS)
                {
                    textBoxLire.Text = "SCardListReaders Error: ";
                    return;
                }
                else
                {
                    textBoxLire.Text = "SCardListReaders Success";
                }
     
                //Convert reader buffer to string
                while (ReadersList[indx] != 0)
                {
     
                    while (ReadersList[indx] != 0)
                    {
                        rName = rName + (char)ReadersList[indx];
                        indx = indx + 1;
                    }
    /*
                    //Add reader name to list
                    cbReader.Text = rName;
                    cbReader.Items.Add(rName);
                    rName = "";
                    indx = indx + 1;*/
     
                }
     
     
            } 
     
    //---------------------------------------------------------------------------------------------------------
    //--------------------------------Connexion à la carte à puce---------------------------------------------       
            void connexion()
            {
                string reader = "ACS ACR38U 0";
                ioRequest.dwProtocol = this.ActiveProtocol;
                ioRequest.cbPciLength = 8;
     
                retCode = ModWinsCard.SCardConnect(hContext,
                                                     reader,
                                                     ModWinsCard.SCARD_SHARE_SHARED,
                                                     ModWinsCard.SCARD_PROTOCOL_T0,
                                                     ref hCard,
                                                     ref ActiveProtocol);
     
                if (retCode != ModWinsCard.SCARD_S_SUCCESS)
                {
                    textBoxLire.Text = "problème co !";
                }
                else
                {
     
                    ClearBuffers();
                    //Select CardType
                    SendBuff[0] = 0xFF;
                    SendBuff[1] = 0xA4;
                    SendBuff[2] = 0x00;
                    SendBuff[3] = 0x00;
                    SendBuff[4] = 0x01;
                    SendBuff[5] = 0x06;
     
                    SendBuffLen = 6;
                    RecvBuffLen = 255;
     
                    tmpStr = "";
                    for (indx = 0; indx <= SendBuffLen - 1; indx++)
                    {
                        tmpStr = tmpStr + string.Format("{0:x2}", (SendBuff[indx])).ToUpper() + " ";
                    }
                    tmpStr = "";
                    retCode = ModWinsCard.SCardTransmit(hCard,
                                                        ref ioRequest,
                                                        ref SendBuff[0],
                                                        SendBuffLen,
                                                        ref ioRequest,
                                                        ref RecvBuff[0],
                                                        ref RecvBuffLen);
     
     
     
                    for (indx = RecvBuffLen - 2; indx <= RecvBuffLen - 1; indx++)
                    {
                        tmpStr = tmpStr + string.Format("{0:x2}", RecvBuff[indx]).ToUpper() + " ";
                    }
     
                    if (tmpStr != "90 00 ")
                    {
                        textBoxLire.Text = "Return bytes are not acceptable.";
                    }
                    else
                    {
                        tmpStr = "";
                        for (indx = 0; indx <= RecvBuffLen - 1; indx++)
                        {
                            tmpStr = tmpStr + string.Format("{0:x2}", RecvBuff[indx]).ToUpper() + " ";
                        }
                    }
     
                    butLirelire.Enabled = true;
                    butLirecon.Enabled = false;
                    butLiredec.Enabled = true;
                }
            }
    //--------------------------------------Lit les donnees sur la carte à puce ------------------------
            void Lirecarte(int t, int ad)
        {
     
                tmpStr = "";
                byte adByte = System.Convert.ToByte(ad);
                ClearBuffers();
     
    			SendBuff[0] = 0xFF;
    			SendBuff[1] = 0xB0;
    			SendBuff[2] = 0x00;
                SendBuff[3] = adByte;
                SendBuff[4] = 0x14;
     
    			SendBuffLen = 5;
    			RecvBuffLen = SendBuff[4] + 6;
     
    			tmpStr = "";
     
    			for(indx = 0; indx <= SendBuffLen -1; indx++)
    			{
    				tmpStr = tmpStr + string.Format("{0:x2}", SendBuff[indx]).ToUpper() + " ";
    			}
     
                retCode = ModWinsCard.SCardTransmit(hCard,
                                                    ref ioRequest,
                                                    ref SendBuff[0],
                                                    SendBuffLen,
                                                    ref ioRequest,
                                                    ref RecvBuff[0],
                                                    ref RecvBuffLen);
     
    			if(retCode != ModWinsCard.SCARD_S_SUCCESS)
    			{
                    textBoxLire.Text = "problème Transmit !";
                }
                else
                {
                    textBoxLire.Text = "Transmit ... ";
     
     
     
    			// 3. Display data read from card into Data textbox
    			tmpStr = "";
    			for(indx = 0; indx <= SendBuff[4] -1; indx++)
    			{
    				tmpStr = tmpStr +  Convert.ToChar(RecvBuff[indx]);
    			}
     
                if (t == 1)
                {
                    textLireid.Text = tmpStr;
                }
                if (t == 2)
                {
                    textLirenom.Text = tmpStr;
                }
                if (t == 3)
                {
                    textLirecre.Text = tmpStr;
                }
     
                }  
        }
    //---------------------------------------------------------------------------------------------------------
            private void butLirecon_Click(object sender, EventArgs e)
            {
     
                connexion();
            }
     
            private void butLirelire_Click(object sender, EventArgs e)
            {
     
                Lirecarte(1, 40);
                Lirecarte(2, 70);
                Lirecarte(3, 110);
     
            }
     
            private void butLiredec_Click(object sender, EventArgs e)
            {
                //--------------------Ferme la connection---------------------
     
                retCode = ModWinsCard.SCardDisconnect(hCard, ModWinsCard.SCARD_UNPOWER_CARD);
     
                if (retCode != ModWinsCard.SCARD_S_SUCCESS)
                {
                    textBoxLire.Text = "problème déconnection !";
                }
                else
                {
                    textBoxLire.Text = "Terminé ! déconnection ... ";
                    butLirelire.Enabled = false;
                    butLirecon.Enabled = true;
                    butLiredec.Enabled = false;
     
                }
            }
     
    //----------------------------------------Ecriture Base de donnée---------------------------------
            void requete(int w)
            {
                if (textLireid.Text == "" || textLirenom.Text == "")
                {
     
                    MessageBox.Show("Vous devez remplir les champs Nom et Prénom pour valider le formulaire", "Saisie incomplète", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     
                }
                else
                {
                    string connectionString = null;
                    try
                    {
     
                        connectionString = Properties.Settings.Default.gestionConnectionString1; //--Variable de connection
     
                    }
                    catch (System.Exception exe) { System.Windows.Forms.MessageBox.Show(exe.Message); }
     
     
                    try
                    {
                        using (SqlConnection connexion = new SqlConnection(connectionString))
                        {
                            connexion.Open();//--Ouverture de connexion
                            SqlCommand sqlCommand;
                            //-------------------------------------Requête SQL-----------------------------
     
                                SqlCommand sqlCommand1 = new SqlCommand("INSERT INTO utilisateur (Nom ,Id ,Unites) values (@Nom , @Id , @Unites) ", connexion);
                                SqlCommand sqlCommand2 = new SqlCommand("update utilisateur Set Nom = @Nom, Unites = @Unites Where Id = @Id ; ", connexion);
     
                            //-----------------------------Création de variable de Textbox------------------
                            if (w == 1)
                            {
                                sqlCommand = sqlCommand1;
     
                                //textbox -> variable @Nom
     
                                sqlCommand.Parameters.Add(
                                    "@Nom", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Nom"].Value = textLirenom.Text;
     
                                //textbox -> variable @Nom
     
                                sqlCommand.Parameters.Add(
                                    "@Id", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Id"].Value = textLireid.Text;
     
                                //textbox -> variable @Unites 
     
                                sqlCommand.Parameters.Add(
                                    "@Unites", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Unites"].Value = textLirecre.Text;
     
                                sqlCommand.ExecuteNonQuery();
                            }
                            if (w == 2)
                            {
                                sqlCommand = sqlCommand2;
     
                                //textbox -> variable @Nom
     
                                sqlCommand.Parameters.Add(
                                    "@Nom", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Nom"].Value = textLirenom.Text;
     
                                //textbox -> variable @Nom
     
                                sqlCommand.Parameters.Add(
                                    "@Id", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Id"].Value = textLireid.Text;
     
                                //textbox -> variable @Unites 
     
                                sqlCommand.Parameters.Add(
                                    "@Unites", SqlDbType.VarChar);
     
                                sqlCommand.Parameters[
                                    "@Unites"].Value = textLirecre.Text;
     
                                sqlCommand.ExecuteNonQuery();
                            }
     
     
                            //-- Execution de la commande ----
     
     
                            Actualiser();
                        }
     
     
                    }
                    catch (Exception exep) { System.Windows.Forms.MessageBox.Show(exep.Message); }
                }
            }
            private void butLiremaj_Click(object sender, EventArgs e)
            {
                requete(2);
     
            }
     
            private void butLireact_Click(object sender, EventArgs e)
            {
                Actualiser();
            }
     
            private void butLireajout_Click(object sender, EventArgs e)
            {
                requete(1);
            }
     
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
     
            }
     
        }
    }

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 43
    Points : 25
    Points
    25
    Par défaut
    Bonjour,

    carte Fox LX 832 n'est pas une carte à puces... mais plus un lecteur :
    http://yoannsculo.developpez.com/tut...rte-fox-lx832/

    Ensuite le code est juste une "interface" vers la DLL winscard...
    ScardEstablishContext,
    ScardGetReaderList,
    ScardTransmit, ...

    Pour aide : http://code.google.com/p/chenguo/sou...c=svn262&r=262

  3. #3
    Membre à l'essai
    Femme Profil pro
    Inscrit en
    Avril 2011
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations forums :
    Inscription : Avril 2011
    Messages : 34
    Points : 17
    Points
    17
    Par défaut
    Citation Envoyé par Polux95 Voir le message
    Ensuite le code est juste une interface vers la DLL winscard...

    si j ai bien compris tout le code est dans la doc de la fox , donc pas besoin de coder pour effectuer des taches sur la carte , faux juste appeler les fonctions qui existent deja
    c est ca ?

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 43
    Points : 25
    Points
    25
    Par défaut
    Bonjour,

    Alors tu as la méthode Init() dans ton code qui est la pour initialiser la communication avec ta carte.

    Ensuite la méthode 'Connexion' effectue deux choses:
    - Le connect à la carte (avec le choix du lecteur)
    - Envoie D'une commande APDU 'Select' (ModWinsCard.SCardTransmit)

    La méthode 'Lirecarte' envoie une commande APDU 'ReadRecord', de nouveau avec ModWinsCard.SCardTransmit

    Ce qu'il reste à faire est de connaitre tes commandes APDU, et de les envoyer avec ModWinsCard.SCardTransmit après la méthode Init() et la partie ModWinsCard.SCardConnect


    Les commandes APDU seront définie dans la spécification de ta carte à puce (normalement en respectant la norme ISO7816)

    Définition (réduit au minimum d'une commande APDU) : http://en.wikipedia.org/wiki/Smart_c...ocol_data_unit

Discussions similaires

  1. Lire une carte à puce via une navigateur WEB
    Par aymerictj dans le forum Débuter
    Réponses: 2
    Dernier message: 12/08/2013, 17h22
  2. Lire une carte à puce
    Par Ggamer dans le forum Général Python
    Réponses: 1
    Dernier message: 22/10/2012, 01h02
  3. Applet Java pour lire une carte à puce : par où commencer ?
    Par Le Barde dans le forum API standards et tierces
    Réponses: 1
    Dernier message: 01/09/2010, 14h28
  4. [JAXWS][PKCS11]plusieurs certifs sur une carte a puce
    Par Le Marlou dans le forum Sécurité
    Réponses: 1
    Dernier message: 11/03/2010, 10h29
  5. module pkcs11 pour lire une carte cyberflex 32k
    Par ikuzar dans le forum Administration système
    Réponses: 0
    Dernier message: 05/06/2009, 16h21

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo