本示例使用设备:?Android Linux RFID读写器NFC发卡器WEB可编程NDEF文本/智能海报/-淘宝网 (taobao.com)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MifareClassIC卡读写DEMO</title>
<script language="javascript">
var BLOCK0_EN = 0x01;//读第一块的(16个字节)
var BLOCK1_EN = 0x02;//读第二块的(16个字节)
var BLOCK2_EN = 0x04;//读第三块的(16个字节)
var NEEDSERIAL = 0x08;//仅读指定序列号的卡
var EXTERNKEY = 0x10;//用明码认证密码,产品开发完成后,建议把密码放到设备的只写区,然后用该区的密码后台认证,这样谁都不知道密码是多少,需要这方面支持请联系
var NEEDHALT = 0x20; //读/写完卡后立即休眠该卡,相当于这张卡不在感应区。要相重新操作该卡必要拿开卡再放上去
var iswsrun = false;
var ws;
var received_msg = "";
var mytimer;
var strls = "";
var errorno = "";
var wsUri = "ws://127.0.0.1:39189"; //端口号必须与RFIDWebServer端口一致
function isUIntNum(val) {
var testval = /^\d+$/; // 非负整数
return (testval.test(val));
}
function isHex(val) {
var testval = /^(\d|[A-F]|[a-f])+$/; // 十六进制数判断
return (testval.test(val));
}
function beep() { //驱动发卡器响声令
textarea.value = "";
WebSocketRun("pcdbeep,30");
}
function getdevicenumber() { //读取发卡器唯一出厂序号,可以当加密狗使用
textarea.value = "";
WebSocketRun("pcdgetdevicenumber");
}
function piccrequest() {
var RequestCardComm = "piccrequest,";
WebSocketRun(RequestCardComm);
}
function readcard() {
textarea.value = "";
block0.value= "";
block1.value= "";
block2.value= "";
if (selinoutkey.selectedIndex==1){
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //外部密钥认证
}else {myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;} //已装载到发卡器内部密钥认证
myareano = selareano.selectedIndex; //指定为本次读取第8区,十进制
authmode = selauthmode.selectedIndex; //指定密码模式,十进制,大于0表示用A密码认证,推荐用A密码认证
mypiccserial = "00000000"; //指定本次操作的卡序列号,十六进制,未知时可指定为8个0
mypicckey = authkey0.value.trim(); //指定卡片密码,十六进制,FFFFFFFFFFFF为卡片厂家出厂密码
if (!isHex(mypicckey) || mypicckey.length!=12) {
textarea.value = "认证密钥输入错误,请输入正确的12位16进制认证密钥!";
authkey0.focus();
authkey0.select();
return;
}
var ReadCardComm = "piccreadex," + myctrlword + "," + mypiccserial + "," + myareano + "," + authmode + "," + mypicckey;
WebSocketRun(ReadCardComm);
}
function writecard() {
textarea.value = "";
if (selinoutkey.selectedIndex==1){
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //外部密钥认证
}else {myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;} //已装载到发卡器内部密钥认证
myareano = selareano.selectedIndex; //指定为本次读取第8区,十进制
authmode = selauthmode.selectedIndex; //指定密码模式,十进制,大于0表示用A密码认证,推荐用A密码认证
mypiccserial = "00000000"; //指定本次操作的卡序列号,十六进制,未知时可指定为8个0
mypicckey = authkey0.value.trim(); //指定卡片密码,十六进制,FFFFFFFFFFFF为卡片厂家出厂密码
if (!isHex(mypicckey) || mypicckey.length!=12) {
textarea.value = "认证密钥输入错误,请输入正确的12位16进制认证密钥!";
authkey0.focus();
authkey0.select();
return;
}
block0data = block0.value.trim(); //第0块写卡数据
if (!isHex(block0data) || block0data.length!=32) {
textarea.value = "第0块数据输入错误,请输入正确的32位16进制写卡数据!";
block0.focus();
block0.select();
return;
}
block1data = block1.value.trim(); //第1块写卡数据
if (!isHex(block1data) || block1data.length!=32) {
textarea.value = "第1块数据输入错误,请输入正确的32位16进制写卡数据!";
block1.focus();
block1.select();
return;
}
block2data = block2.value.trim(); //第2块写卡数据
if (!isHex(block2data) || block2data.length!=32) {
textarea.value = "第2块数据输入错误,请输入正确的32位16进制写卡数据!";
block2.focus();
block2.select();
return;
}
piccdata=block0data+block1data+block2data
var WriteCardComm = "piccwriteex," + myctrlword + "," + mypiccserial + "," + myareano + "," + authmode + "," + mypicckey + "," + piccdata;
WebSocketRun(WriteCardComm);
}
function changecardkeyex() {
textarea.value = "";
if (selinoutkey.selectedIndex==1){
myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; //外部密钥认证
}else {myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;} //已装载到发卡器内部密钥认证
myareano = selareano.selectedIndex; //指定为本次读取第8区,十进制
authmode = selauthmode.selectedIndex; //指定密码模式,十进制,大于0表示用A密码认证,推荐用A密码认证
mypiccserial = "00000000"; //指定本次操作的卡序列号,十六进制,未知时可指定为8个0
mypicckey_old = authkey0.value.trim(); //指定卡片认证密码,十六进制,FFFFFFFFFFFF为卡片厂家出厂密码
if (!isHex(mypicckey_old) || mypicckey_old.length!=12) {
textarea.value = "认证密钥输入错误,请输入正确的12位16进制认证密钥!";
authkey0.focus();
authkey0.select();
return;
}
mypicckeyA = newkeya.value.trim(); //新A密钥
if (!isHex(mypicckeyA) || mypicckeyA.length!=12) {
textarea.value = "新A密钥输入错误,请输入正确的12位16进制新A密钥!";
newkeya.focus();
newkeya.select();
return;
}
mypiccctr = cardctr.value.trim(); //新控制位,出厂为FF078069,
if (!isHex(mypiccctr) || mypiccctr.length!=8) {
textarea.value = "新控制位输入错误,请输入正确的8位16进制控制位!";
cardctr.focus();
cardctr.select();
return;
}
mypicckeyB = newkeyb.value.trim(); //新B密钥
if (!isHex(mypicckeyB) || mypicckeyB.length!=12) {
textarea.value = "新B密钥输入错误,请输入正确的12位16进制新B密钥!";
newkeyb.focus();
newkeyb.select();
return;
}
mypicckey_new=mypicckeyA+mypiccctr+mypicckeyB;
switch (selchangekey.selectedIndex){
case 0:
mypicckey_new=mypicckey_new+"00";
break;
case 1:
mypicckey_new=mypicckey_new+"02";
break;
default:
mypicckey_new=mypicckey_new+"03";
break;
}
var ChangeKeyComm = "piccchangesinglekeyex," + myctrlword + "," + mypiccserial + "," + myareano + "," + authmode + "," + mypicckey_old + "," + mypicckey_new;
WebSocketRun(ChangeKeyComm);
}
function writecarduid(){
textarea.value = "";
decnumber=newuid.value.trim();
if (decnumber.length<1){
textarea.value = "请先输入要写入的UID!";
newuid.focus();
newuid.select();
return;
}
block0data=block0.value.trim();
if (block0data.length=32){ //如果有读取旧卡的厂商数据,新写入的卡片厂商信息也按旧卡写入
oldcode=block0data.substring(10,32);
}else{oldcode="08040003941846B4DC211D"}
decnumber=parseInt(decnumber); //指定写卡内容,长度为16个字节,其中前面四个字节是卡号,第五字节必须为卡号的异或和校验字,后面为厂家标识
hexnumber= decnumber.toString(16).toUpperCase().padStart(8, '0');
xorcode=parseInt("0x"+hexnumber.substring(0,2))^parseInt("0x"+hexnumber.substring(2,4))^parseInt("0x"+hexnumber.substring(4,6))^parseInt("0x"+hexnumber.substring(6,8));
piccdata0 =hexnumber+xorcode.toString(16).toUpperCase().padStart(2, '0')+oldcode;
if (selinoutkey.selectedIndex==1){
myctrlword = BLOCK0_EN + EXTERNKEY; //外部密钥认证
}else {myctrlword = BLOCK0_EN ;} //已装载到发卡器内部密钥认证
authmode = selauthmode.selectedIndex; //指定密码模式,十进制,大于0表示用A密码认证,推荐用A密码认证
mypiccserial = "00000000"; //指定本次操作的卡序列号,十六进制,未知时可指定为8个0
mypicckey_old = authkey0.value.trim(); //指定卡片认证密码,十六进制,FFFFFFFFFFFF为卡片厂家出厂密码
if (!isHex(mypicckey_old) || mypicckey_old.length!=12) {
textarea.value = "认证密钥输入错误,请输入正确的12位16进制认证密钥!";
authkey0.focus();
authkey0.select();
return;
}
var WriteCardComm = "piccwriteserial," + myctrlword + "," + mypiccserial + "," + authmode + "," + mypicckey_old + "," + piccdata0;
WebSocketRun(WriteCardComm);
}
function DispErrInfo(errcode){
var errstr = "";
switch (errcode) {
case "ReturnCode:001":
errstr = ",读写扇区数据失败!";
break;
case "ReturnCode:002":
errstr = ",读写1、2块数据失败!";
break;
case "ReturnCode:003":
errstr = ",读写第2块数据失败!";
break;
case "ReturnCode:008":
errstr = ",未寻到卡,请将卡放到发卡器的感应区!";
break;
case "ReturnCode:009":
errstr = ",有多张卡在感应区,寻卡过程中防冲突失败,读卡失败!";
break;
case "ReturnCode:010":
errstr = ",该卡可能已被休眠,无法选中卡片!";
break;
case "ReturnCode:011":
errstr = ",密码装载失败!";
break;
case "ReturnCode:012":
errstr = ",卡密码认证失败!";
break;
case "ReturnCode:013":
errstr = ",读块操作失败,原因是刷卡太快或本块所对应的区还没通过密码认证!";
break;
case "ReturnCode:014":
errstr = ",写块操作失败,原因是刷卡太快或本块所对应的区还没通过密码认证!";
break;
case "ReturnCode:018":
errstr = ",写块操作失败!";
break;
case "ReturnCode:021":
errstr = ",没有动态库!";
break;
case "ReturnCode:022":
errstr = ",动态库或驱动程序异常!";
break;
case "ReturnCode:023":
errstr = ",驱动程序错误或发卡器未连接!";
break;
case "ReturnCode:024":
errstr = ",操作超时,一般是动态库没有反映!";
break;
case "ReturnCode:025":
errstr = ",发送字数不够!";
break;
case "ReturnCode:026":
errstr = ",发送的CRC错!";
break;
case "ReturnCode:027":
errstr = ",接收的字数不够!";
break;
case "ReturnCode:028":
errstr = ",接收的CRC错!";
break;
case "ReturnCode:029":
errstr = ",函数输入参数格式错误!";
break;
case "ReturnCode:444":
errstr = ",RFIDWebServer系统文件错误!";
break;
default:
errstr = ",未知的错误!";
}
return errstr;
}
window.onerror = function (e) {
alert("不好意思,出错了!");
return true;//屏蔽系统事件
}
function timeoutevent() {
ws.close();
CardIDShowerdev.value = "websockket返回超时";
}
//websockket数据处理
function WebSocketRev(evt) {
clearTimeout(mytimer);
received_msg = evt.data;
ws.close();
//在这里解析返回数据
var strlist = received_msg.split(",");
if (strlist.length > 1) {
var dispstr = "";
switch (strlist[0]) {
case "pcdbeep":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",已执行蜂鸣响声操作!\r\n"
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
textarea.value = dispstr;
break;
case "pcdgetdevicenumber":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",已读取设备编号!\r\n"
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
if (strlist.length > 2) {
dispstr = dispstr + "设备编号:" + strlist[2] + "\r\n";
}
textarea.value = dispstr;
break;
case "piccrequest":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",已读取Mifare卡卡号!\r\n";
carduid.value=strlist[2];
card8h10dz.value=parseInt("0x"+strlist[2]).toString().padStart(10, '0');
card8h10df.value=strlist[3];
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
textarea.value = dispstr;
break;
case "piccreadex":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",已读取卡内数据!\r\n"
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
if (strlist.length > 2) {
carduid.value= strlist[2] ;
HLCode=strlist[2].substring(0,2)+strlist[2].substring(2,4)+strlist[2].substring(4,6)+strlist[2].substring(6,8);
card8h10dz.value=parseInt("0x"+HLCode).toString().padStart(10, '0');
LHCode=strlist[2].substring(6,8)+strlist[2].substring(4,6)+strlist[2].substring(2,4)+strlist[2].substring(0,2);
card8h10df.value=parseInt("0x"+LHCode).toString().padStart(10, '0');
}
if (strlist.length > 3) {
block0.value= strlist[3].substring(0,32);
block1.value= strlist[3].substring(32,64);
block2.value= strlist[3].substring(64,96);
}
textarea.value = dispstr;
break;
case "piccwriteex":
case "piccwriteserial":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",写卡成功!\r\n"
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
if (strlist.length > 2) {
carduid.value= strlist[2] ;
HLCode=strlist[2].substring(0,2)+strlist[2].substring(2,4)+strlist[2].substring(4,6)+strlist[2].substring(6,8);
card8h10dz.value=parseInt("0x"+HLCode).toString().padStart(10, '0');
LHCode=strlist[2].substring(6,8)+strlist[2].substring(4,6)+strlist[2].substring(2,4)+strlist[2].substring(0,2);
card8h10df.value=parseInt("0x"+LHCode).toString().padStart(10, '0');
}
textarea.value = dispstr;
break;
case "piccchangesinglekeyex":
dispstr = "函数名称:" + strlist[0] + "\r\n";
if (strlist[1] == "ReturnCode:000") {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + ",更改卡片密钥成功!\r\n"
} else {
dispstr = dispstr + "操作结果:" + strlist[1];
dispstr = dispstr + DispErrInfo(strlist[1]);
}
if (strlist.length > 2) {
carduid.value= strlist[2] ;
HLCode=strlist[2].substring(0,2)+strlist[2].substring(2,4)+strlist[2].substring(4,6)+strlist[2].substring(6,8);
card8h10dz.value=parseInt("0x"+HLCode).toString().padStart(10, '0');
LHCode=strlist[2].substring(6,8)+strlist[2].substring(4,6)+strlist[2].substring(2,4)+strlist[2].substring(0,2);
card8h10df.value=parseInt("0x"+LHCode).toString().padStart(10, '0');
}
textarea.value = dispstr;
break;
}
}
}
function WebSocketRun(sendinfo) {
iswsrun = false;
try {
if ("WebSocket" in window) {
ws = new WebSocket(wsUri);
}
else if ("MozWebSocket" in window) {
ws = new MozWebSocket(wsUri);
}
else {
received_msg = "您的浏览器不支持WebSocket,请选用支持WebSocket的浏览器!";
return;
}
clearTimeout(mytimer);
ws.onopen = function (evt) {
ws.send(sendinfo);
iswsrun = true;
mytimer = setTimeout("timeoutevent()", 2000);
};
ws.onmessage = WebSocketRev;
ws.onerror = function (e) {
if (iswsrun != true) {
received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
window.open("http://www.icmcu.com/ICID15693-02V2test/RFIDWebServerSetup.exe", "top");//打开新窗口
alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
}
};
}
catch (ex) {
if (iswsrun != true) {
received_msg = "请先在当前电脑下载>安装>运行我们的服务程序......";
window.open("http://www.icmcu.com/ICID15693-02V2test/RFIDWebServerSetup.exe", "top");//打开新窗口
alert("请先下载>安装>运行我们的服务程序,再刷新本页面......");
}
}
}
</script>
</head>
<body>
<table width="866" height="425" border="1" align="center">
<tr>
<th width="124" height="45" scope="row"><input name="butt_beep" type="submit" id="butt_beep" onclick="beep()" value="驱动发卡器响声" /></th>
<td width="716"><input name="butt_getdevnum" type="submit" id="butt_getdevnum" onclick="getdevicenumber()" value="读取发卡器唯一出厂序列号" /></td>
</tr>
<tr>
<th height="45" scope="row"><input name="butt_piccrequest" type="submit" id="butt_piccrequest" onclick="piccrequest()" value="寻M1卡获取卡号" /></th>
<td>原始16进制卡号:
<input name="carduid" type="text" id="carduid" size="9" maxlength="8" />
,转8H10D正码:
<input name="card8h10dz" type="text" id="card8h10dz" size="10" maxlength="10" />
,8H10D反码:
<input name="card8h10df" type="text" id="card8h10df" size="10" maxlength="10" /></td>
</tr>
<tr>
<th height="45" scope="row"> </th>
<td><p>扇区号:
<select name="selareano" id="selareano">
<option selected="selected">第0扇区</option>
<option>第1扇区</option>
<option>第2扇区</option>
<option>第3扇区</option>
<option>第4扇区</option>
<option>第5扇区</option>
<option>第6扇区</option>
<option>第7扇区</option>
<option>第8扇区</option>
<option>第9扇区</option>
<option>第10扇区</option>
<option>第11扇区</option>
<option>第12扇区</option>
<option>第13扇区</option>
<option>第14扇区</option>
<option>第15扇区</option>
</select>
<label for="changeauthkey"></label>
<select name="selinoutkey" id="selinoutkey">
<option>内部密钥认证</option>
<option selected="selected">外部密钥认证</option>
</select>
<label for="newkey"></label>
<select name="selauthmode" id="selauthmode">
<option>B密钥认证</option>
<option selected="selected">A密钥认证</option>
</select>
,认证密钥:
<label for="authkey0"></label>
<input name="authkey0" type="text" id="authkey0" value="FFFFFFFFFFFF" size="13" maxlength="12" />
</p></td>
</tr>
<tr>
<th height="45" scope="row"><p>
<input name="butt_readcard" type="submit" id="butt_readcard" onclick="readcard()" value="轻松读取扇区数据" />
</p>
<input name="butt_writecard" type="submit" id="butt_writecard" onclick="writecard()" value="轻松写入扇区数据" /></th>
<td><p>
<label for="rwtext"></label>
第0块:
<input name="block0" type="text" id="block0" size="33" maxlength="32" />
</p>
<p>第1块:
<input name="block1" type="text" id="block1" size="33" maxlength="32" />
</p>
<p>第2块:
<input name="block2" type="text" id="block2" size="33" maxlength="32" />
</p></td>
</tr>
<tr>
<th height="45" scope="row"><input name="butt_changecardkeyex" type="submit" id="butt_changecardkeyex" onclick="changecardkeyex()" value="修改扇区密钥" /></th>
<td><select name="selchangekey" id="selchangekey">
<option>只修改A密钥</option>
<option>只修改AB密钥</option>
<option selected="selected">修改AB密钥及控制位</option>
</select>
,新A密钥
<input name="newkeya" type="text" id="newkeya" value="FFFFFFFFFFFF" size="12" maxlength="12" />
,控制位
<input name="cardctr" type="text" id="cardctr" value="FF078069" size="8" maxlength="8" />
,B密钥
<input name="newkeyb" type="text" id="newkeyb" value="FFFFFFFFFFFF" size="12" maxlength="12" /></td>
</tr>
<tr>
<th height="45" scope="row"><input name="butt_writecarduid" type="submit" id="butt_writecarduid" onclick="writecarduid()" value="写入UID卡号" /></th>
<td>8H10D正码UID:
<input name="newuid" type="text" id="newuid" size="10" maxlength="10" onkeyup="this.value=this.value.replace(/\D/g,'')"/></td>
</tr>
<tr>
<th height="100" scope="row"><p>操作提示</p>
<p> </p>
<p> </p></th>
<td><textarea name="textarea" id="textarea" cols="100" rows="8" ></textarea></td>
</tr>
</table>
</body>
</html>