资讯详情 C#读写Mifare Ultralight EV1、Mifare Ultralight C、Mifare Ultralight AES卡修改3DES、AES密钥源码
📅 2026/10/23 12:11:56
本示例适配的发卡器https://item.taobao.com/item.htm?fttid1036723554669Ultralight卡存储结构Ultralight EV1卡存储结构Ultralight C卡存储结构Ultralight AES卡存储结构函数声明//------------------------------------------------------------------------------------------------------------------------------------------------------ //External function declaration: make sound equipment [DllImport(OUR_MIFARE.dll, EntryPoint pcdbeep, CallingConvention CallingConvention.StdCall)] static extern byte pcdbeep(UInt32 xms);//xms milliseconds //------------------------------------------------------------------------------------------------------------------------------------------------------ //Read the device number, can be used as a software encryption dog, can also according to the number on the company website query warranty period [DllImport(OUR_MIFARE.dll, EntryPoint pcdgetdevicenumber, CallingConvention CallingConvention.StdCall)] static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber //获取IC卡芯片型号---------------------------------------------------------------------------------------------------------------------------------------- [DllImport(OUR_MIFARE.dll, EntryPoint getmifareversion)] static extern byte getmifareversion(byte[] cardtypestr, byte[] AtqaSak, byte[] versionbuf, byte[] versionlen, byte[] retsw); //------------------------------------------------------------------------------------------------------------------------------------------------------ //Read card Serial Number only [DllImport(OUR_MIFARE.dll, EntryPoint piccrequest_ul, CallingConvention CallingConvention.StdCall)] public static extern byte piccrequest_ul(byte[] serial);// Serial Number //------------------------------------------------------------------------------------------------------------------------------------------------------ //Read card 4 pages data [DllImport(OUR_MIFARE.dll, EntryPoint piccread_ul, CallingConvention CallingConvention.StdCall)] public static extern byte piccread_ul(byte blockadd, byte[] piccdata);// Serial Number //------------------------------------------------------------------------------------------------------------------------------------------------------ //Write 1 Page [DllImport(OUR_MIFARE.dll, EntryPoint piccwrite_ul, CallingConvention CallingConvention.StdCall)] public static extern byte piccwrite_ul(byte blockadd, byte[] piccdata); //------------------------------------------------------------------------------------------------------------------------------------------------------- //认证密码 [DllImport(OUR_MIFARE.dll, EntryPoint piccauthkey_ntag, CallingConvention CallingConvention.StdCall)] public static extern byte piccauthkey_ntag(byte[] picckey, byte[] piccntagpack); //------------------------------------------------------------------------------------------------------------------------------------------------------ //Easy to read [DllImport(OUR_MIFARE.dll, EntryPoint piccreadex_ul, CallingConvention CallingConvention.StdCall)] public static extern byte piccreadex_ul(byte ctrlword, byte[] serial, byte blockadd, byte blocksize, byte[] piccdata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //Easy to write [DllImport(OUR_MIFARE.dll, EntryPoint piccwriteex_ul, CallingConvention CallingConvention.StdCall)] static extern byte piccwriteex_ul(byte ctrlword, byte[] serial, byte blockadd, byte blocksize, byte[] piccdata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //Send Display Information to Drive [DllImport(OUR_MIFARE.dll, EntryPoint lcddispfull, CallingConvention CallingConvention.StdCall)] static extern byte lcddispfull(string lcdstr); //------------------------------------------------------------------------------------------------------------------------------------------------------ //3des密钥认证 [DllImport(OUR_MIFARE.dll, EntryPoint piccauthkeyul3des, CallingConvention CallingConvention.StdCall)] static extern byte piccauthkeyul3des(byte[] keybuf, byte keyno); //------------------------------------------------------------------------------------------------------------------------------------------------------ //3des密钥认证 [DllImport(OUR_MIFARE.dll, EntryPoint piccauthkeyulaes, CallingConvention CallingConvention.StdCall)] static extern byte piccauthkeyulaes(byte[] keybuf, byte keyno); //------------------------------------------------------------------------------------------------------------------------------------------------------ //写一页 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeswrite, CallingConvention CallingConvention.StdCall)] static extern byte picculaeswrite(byte ctrlword, byte block, byte[] databuf); //------------------------------------------------------------------------------------------------------------------------------------------------------ //更改aes卡配置 [DllImport(OUR_MIFARE.dll, EntryPoint picculaessetconfiguration, CallingConvention CallingConvention.StdCall)] static extern byte picculaessetconfiguration(byte ctrlword, byte[] settingsbuf); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES专用指令读 [DllImport(OUR_MIFARE.dll, EntryPoint picculaesread, CallingConvention CallingConvention.StdCall)] public static extern byte picculaesread(byte ctrlword, byte blockadd, byte[] piccdata); ////------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡锁3-15块 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeslockpage3to15, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeslockpage3to15(byte ctrlword, byte[] piccdata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡锁16-39块 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeslockpage16to39, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeslockpage16to39(byte ctrlword, byte[] piccdata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡锁配置块 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeslockconfiguration, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeslockconfiguration(byte ctrlword); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡锁定密钥 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeslockkey, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeslockkey(byte ctrlword, byte keytype); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡读取计数器值 [DllImport(OUR_MIFARE.dll, EntryPoint picculaesreadcnt, CallingConvention CallingConvention.StdCall)] public static extern byte picculaesreadcnt(byte ctrlword, byte countid, byte[] countdatabuf); //------------------------------------------------------------------------------------------------------------------------------------------------------ //AES卡读取计数器递增 [DllImport(OUR_MIFARE.dll, EntryPoint picculaesincrcnt, CallingConvention CallingConvention.StdCall)] public static extern byte picculaesincrcnt(byte ctrlword, byte countid, int incrvalue); //------------------------------------------------------------------------------------------------------------------------------------------------------ //读取卡版本型号 [DllImport(OUR_MIFARE.dll, EntryPoint picculaesgetversion, CallingConvention CallingConvention.StdCall)] public static extern byte picculaesgetversion(byte ctrlword, byte[] mypiccversiondata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //读取AES卡签名信息 [DllImport(OUR_MIFARE.dll, EntryPoint picculaesreadsig, CallingConvention CallingConvention.StdCall)] public static extern byte picculaesreadsig(byte ctrlword, byte[] piccsigdata, byte[] piccsigdatalen); //------------------------------------------------------------------------------------------------------------------------------------------------------ //更改AES卡签名信息 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeswritesig4bytes, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeswritesig4bytes(byte ctrlword, byte block, byte[] piccsigdata); //------------------------------------------------------------------------------------------------------------------------------------------------------ //锁定AES卡签名信息 [DllImport(OUR_MIFARE.dll, EntryPoint picculaeslocksig, CallingConvention CallingConvention.StdCall)] public static extern byte picculaeslocksig(byte ctrlword, byte locktype); //------------------------------------------------------------------------------------------------------------------------------------------------------ //启用随机ID功能后的寻卡 [DllImport(OUR_MIFARE.dll, EntryPoint piccrequest, CallingConvention CallingConvention.StdCall)] public static extern byte piccrequest(byte[] piccserial); //------------------------------------------------------------------------------------------------------------------------------------------------------ //锁定页数据 [DllImport(OUR_MIFARE.dll, EntryPoint picclock_ntag, CallingConvention CallingConvention.StdCall)] public static extern byte picclock_ntag(byte locktype, byte[] mypicclockdata);获取卡内芯片型号byte[] AtqaSak new byte[3]; byte[] retsw new byte[2]; byte[] versionbuf new byte[100]; byte[] versionlen new byte[2]; byte[] cardtypebuf new byte[1024]; byte status getmifareversion(cardtypebuf, AtqaSak, versionbuf, versionlen, retsw); string cardtypestr Encoding.ASCII.GetString(cardtypebuf).Trim(); string retstr retsw[0].ToString(X2) retsw[1].ToString(X2); if (status 0) { MessageDispInfo(status); } else { textBox_cardtype.Text cardtypestr; if (cardtypestr.Contains(AES)) { tabControl1.SelectedTab tabPage4; } else { if (cardtypestr.Contains( C )) { tabControl1.SelectedTab tabPage3; } else { if (cardtypestr.Contains(EV1)) { tabControl1.SelectedTab tabPage2; } else { tabControl1.SelectedTab tabPage1; } } } pcdbeep(30); MessageBox.Show(获取IC卡芯片型号操作卡片返回代码 retstr \r\n型号 cardtypestr, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); }寻找到发卡器上的Ultralight卡获取到卡片UIDbyte[] mypiccserial new byte[7]; string CardSN; byte status piccrequest_ul(mypiccserial); switch (status) { case 0: CardSN ; for (int i 0; i 7; i) { CardSN CardSN mypiccserial[i].ToString(X2); } textBox3.Text CardSN; pcdbeep(38); MessageBox.Show(读取卡号成功, 注意:, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); break; case 9: textBox3.Text ; MessageBox.Show(卡有可能启用了随机ID功能请点右边的【启用随机ID功能后的寻卡】进行寻卡 - 认证密码(两种密码都可以) - 读取0、1、2页就可获取原始UID号, 注意:, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); break; default: textBox3.Text ; MessageDispInfo(status); break; }认证Ultralight AES卡密钥if (comboBox1.SelectedIndex 2) { MessageBox.Show(Originality Key是NXP内部密码以加密的方式存放在卡的不对外公开的区域用于查验卡片是否为NXP原装卡。这个密码是不对外公开的。文档中说到如果客户有需要可以和NXP定制自己的Originality Key用于查验卡片是不是客户自己发行的。!, 提示:, MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return; } byte[] keybuf new byte[24]; if (checkhexstr(textBox_AESkey.Text.Trim(), 16, keybuf) false) { MessageBox.Show(AES认证密钥输入错误请输入16字节16进制的AES密钥, 警告, MessageBoxButtons.OK, MessageBoxIcon.Error); textBox_AESkey.Select(); return; } byte keyid (byte)comboBox1.SelectedIndex; byte status piccauthkeyulaes(keybuf, 0); switch (status) { case 0: pcdbeep(30); MessageBox.Show(卡密钥认证成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 12: MessageBox.Show(错误代码 status.ToString(D) 卡密钥认证失败, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageDispInfo(status); break; }认证Ultralight C卡3DES密钥byte[] keybuf new byte[24]; if (checkhexstr(textBox_3deskey.Text.Trim(), 16, keybuf) false) { MessageBox.Show(3DES认证密钥输入错误请输入16字节16进制的3DES密钥, 警告, MessageBoxButtons.OK, MessageBoxIcon.Error); textBox_3deskey.Select(); return; } byte status piccauthkeyul3des(keybuf, 0); switch (status) { case 0: pcdbeep(30); MessageBox.Show(卡密钥认证成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 12: MessageBox.Show(错误代码 status.ToString(D) 卡密钥认证失败, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageDispInfo(status); break; }认证Ultralight EV1卡密钥byte[] keybuf new byte[4]; byte[] mypiccntagpack new byte[2];//认证后返回的确认码 if (checkhexstr(textBox_EV1key.Text.Trim(), 4, keybuf) false) { MessageBox.Show(认证密钥输入错误请输入4字节16进制的密钥, 警告, MessageBoxButtons.OK, MessageBoxIcon.Error); textBox_EV1key.Select(); return; } byte status piccauthkey_ntag(keybuf, mypiccntagpack); switch (status) { case 0: pcdbeep(30); MessageBox.Show(卡密钥认证成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 12: MessageBox.Show(错误代码 status.ToString(D) 卡密钥认证失败, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageDispInfo(status); break; }读取Ultralight卡内数据byte blockaddr (byte)numericUpDown_CBlockadd.Value; int blocksize (int)numericUpDown_CRWsize.Value; if (blocksize 1) { return; } byte[] databuf new byte[250]; int block; int p 0; byte status 8; while (blocksize 1) { if (blocksize 4) { block 4; } else { block blocksize; } byte[] readbuf new byte[16]; status piccread_ul(blockaddr, readbuf); if (status 0) { for (int i 0; i block * 4; i) { databuf[p] readbuf[i]; p; } blocksize blocksize - block; blockaddr (byte)(blockaddr block); } else { break; } } if (status 0 p 0) { string datastr ; for (int i 0; i p; i) { datastr datastr databuf[i].ToString(X2) ; } richTextBox_Cdata.Text datastr; pcdbeep(30); MessageBox.Show(读取卡内数据成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { switch (status) { case 13: MessageBox.Show(错误代码 status.ToString(D) 未验证卡密码 或 指定的块不允许读请重新寻卡 或 认证卡密钥后试试, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageDispInfo(status); break; } }写数据byte blockadd (byte)numericUpDown_CBlockadd.Value; int blocksize (int)numericUpDown_CRWsize.Value; if (blockadd 4) { MessageBox.Show(Mifare Ultralight C卡的3块是OTP锁定状态块为防止卡片损坏禁止用此方式写4块以前的数据, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (blockadd blocksize - 1 39) { MessageBox.Show(Mifare Ultralight C卡的40块是OTP锁定状态块、41块为计数器块42以后为配置及密钥块为防止卡片损坏禁止用此方式写40及以后块的数据, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } byte[] databuf new byte[250]; byte[] writebuf new byte[4]; int datalen blocksize * 4; int p 0; byte status 8; if (checkhexstr(richTextBox_Cdata.Text.Trim(), datalen, databuf) false) { MessageBox.Show(写入数据输入错误请输入 datalen.ToString() 字节的16进制数据, 警告, MessageBoxButtons.OK, MessageBoxIcon.Error); richTextBox_Cdata.Select(); return; } while (blocksize 1) { for (int i 0; i 4; i) { writebuf[i] databuf[p]; p; } status piccwrite_ul(blockadd, writebuf); if (status 0) { blocksize blocksize - 1; blockadd (byte)(blockadd 1); } else { blocksize 0; } } if (status 0 p 0) { pcdbeep(30); MessageBox.Show(数据写入成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (status 13 || status 14) { MessageBox.Show(错误代码 status.ToString(D) 未验证卡密码 或 指定的块已锁定不允许改写请重新寻卡 或 认证卡密钥后试试, 提示, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageDispInfo(status); } }本示例下载【免费】C#读写MifareUlitraligthEV1、UlitraligthC、UlitraligthAES卡源码资源-CSDN下载