有密钥保护的物流跟踪、图书馆管理ISO15693标签ICODE SLIX2读写C#源码

发布于:2025-08-08 ⋅ 阅读:(21) ⋅ 点赞:(0)

本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.3edc645eNmdutm&ft=t&id=615391857885

一、卡密钥认证

private void button22_Click(object sender, EventArgs e)
{
	byte passwordid;            //密钥类型
	byte[] uidbuf = new byte[8];     //卡UID
	byte[] authkeybuf = new byte[4]; //认证密钥

	if (checkhexstr(textBox4.Text, 8, uidbuf, 0) == false)
	{
		MessageBox.Show("需要操作的卡片UID错误,可先执行寻卡操作获取UID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	if (checkhexstr(textBox6.Text, 4, authkeybuf, 0) == false)
	{
		MessageBox.Show("卡片认证密钥输入错误,请输入8位16进制卡认证密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		textBox6.Select();
		return;
	}

	switch (comboBox2.SelectedIndex) {
		case 4:
			passwordid = 0x10;
			break;
		case 3:
			passwordid = 0x08;
			break;
		case 2:
			passwordid = 0x04;
			break;
		case 1:
			passwordid = 0x02;
			break;
		default:
			passwordid = 0x01;
			break;
	}

	byte ctrlword = 0x04;
	byte status = iso15693authkey(ctrlword, uidbuf, passwordid, authkeybuf);
	if (status == 0)
	{
		pcdbeep(50);
		MessageBox.Show("UID:" + textBox4.Text + " 密钥认证成功!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
	}
	else { MessageErrInf(status); }
}

二、更改卡密钥

private void button23_Click(object sender, EventArgs e)
{
	byte passwordid;  //密钥类型
	byte[] uidbuf = new byte[8];     //卡UID
	byte[] newkeybuf = new byte[4];  //认证密钥

	if (checkhexstr(textBox4.Text, 8, uidbuf, 0) == false)
	{
		MessageBox.Show("需要操作的卡片UID错误,可先执行寻卡操作获取UID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	if (checkhexstr(textBox10.Text, 4, newkeybuf, 0) == false)
	{
		MessageBox.Show("卡片新密钥输入错误,请输入8位16进制卡新密钥!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		textBox10.Select();
		return;
	}

	switch (comboBox2.SelectedIndex)
	{
		case 4:
			passwordid = 0x10;
			break;
		case 3:
			passwordid = 0x08;
			break;
		case 2:
			passwordid = 0x04;
			break;
		case 1:
			passwordid = 0x02;
			break;
		default:
			passwordid = 0x01;
			break;
	}

	byte ctrlword = 0x04;
	byte status = iso15693writekey(ctrlword, uidbuf, passwordid, newkeybuf);
	switch (status){
		case 0:
			pcdbeep(50);
			MessageBox.Show("UID:" + textBox4.Text + " 更改卡密钥成功!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
			break;
		case 47:
			MessageBox.Show("异常代码:47,上一条指令必须是认证密码指令......!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
			break;
		default:
			MessageErrInf(status);
			break;
	}            
}

三、设置块密钥保护认证方式

private void button27_Click(object sender, EventArgs e)
{
	byte[] uidbuf = new byte[8];     //卡UID
	if (checkhexstr(textBox4.Text, 8, uidbuf, 0) == false)
	{
		MessageBox.Show("需要操作的卡片UID错误,可先执行寻卡操作获取UID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}
	byte blockpoint = (byte)HLblockpoint.Value;     //高、低区分割块地址
	byte extprotectstatus = (byte)(comboBox3.SelectedIndex + comboBox4.SelectedIndex * 16); //高、低区密钥保护模式
	byte ctrlword = 0x04;
	byte status = iso15693protectblock(ctrlword, uidbuf, blockpoint, extprotectstatus);
	switch (status)
	{
		case 0:
			pcdbeep(50);
			MessageBox.Show("UID:" + textBox4.Text + " 块密码保护设定成功!" , "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			break;
		case 47:
			MessageBox.Show("异常代码:47,必须都认证读取密码和写入密码成功后再进行块密码保护设定!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
			break;
		default:
			MessageErrInf(status);
			break;
	} 
}

四、读取卡内数据

private void button7_Click(object sender, EventArgs e)
{
	//读卡块内数据            		
	textBox5.Text = "22";  //操作多张卡标志用
	byte ctrlword = System.Convert.ToByte(textBox5.Text, 16);//以十六进制方式转成数值

	byte startblock = (byte)ctstartblock.Value;

	byte blocknum = (byte)ctblocknum.Value;

	byte[] uidbuf = new byte[8];
	byte[] revbuf = new byte[255];//卡数据缓冲
	byte[] revlen = new byte[1];            
	if (checkhexstr(textBox4.Text, 8, uidbuf, 0) == false)
	{
		MessageBox.Show("需要操作的卡片UID错误,可先执行寻卡操作获取UID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	int j = 0;
	string carddatahex = "";
	byte status = 8;
	while (j<blocknum)
	{
		status = iso15693readblock(ctrlword, System.Convert.ToByte(startblock + j), 1, uidbuf, revlen, revbuf);
		if (status == 0)
		{
			for (int i = 0; i < revlen[0]; i++)
			{
				carddatahex += revbuf[i].ToString("X02")+ " ";
			}
			j++;
		}
		else { j = blocknum; }
	}

	if (status==0) {
		richTextBox1.Text = carddatahex;
		pcdbeep(50);
	}else if (status==46 ){
		MessageBox.Show("请先认证相应的密码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);            
	}else{  MessageErrInf(status);}
}

五、写数据

private void button8_Click(object sender, EventArgs e)
{
	//块写内数据
	byte status;//存放返回值
	byte ctrlword;
	byte startblock;
	byte blocknum;
	byte[] uidbuf = new byte[8];
	byte[] writebuf = new byte[1024];//卡数据缓冲
	byte[] mypiccdata = new byte[32];
	byte eachblocksize;
	int revlen ;
	int i,j,p;

	//控制字指定,控制字的含义请查看本公司网站提供的动态库说明
	switch (comboBox1.SelectedIndex)
	{
		case 2:
			eachblocksize = 32;
			break;
		case 1:
			eachblocksize = 8;
			break;
		default:
			eachblocksize = 4;
			break;
	}

	textBox5.Text = "22";  //操作多张卡标志用
	ctrlword = System.Convert.ToByte(textBox5.Text, 16);//以十六进制方式转成数值

	startblock =(byte)ctstartblock.Value ;

	blocknum  =(byte)ctblocknum .Value ;
	revlen = Convert.ToInt16(blocknum * eachblocksize);

	if (checkhexstr(textBox4.Text, 8, uidbuf, 0) == false)
	{
		MessageBox.Show("需要操作的卡片UID错误,可先执行寻卡操作获取UID!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	if (checkhexstr(richTextBox1.Text, revlen, writebuf, 0) == false)
	{
		MessageBox.Show("需要写入的数据输入错误,可先执行读卡操作!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	j = 0;
	status = 8;
	while (j < blocknum)
	{
		for (p = 0; p < eachblocksize - 1; p++) {mypiccdata[p]=writebuf[j*eachblocksize+p]; }
		status = iso15693writeblock(ctrlword,  System.Convert.ToByte(startblock + j), 1, uidbuf, eachblocksize, mypiccdata);
		if (status == 0)
		{
			j++;
		}
		else { j = blocknum; }
	}

	switch (status)
	{
		case 0:
			pcdbeep(50);
			MessageBox.Show("UID:" + textBox4.Text + "写卡成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			break;
		case 46:
			MessageBox.Show("请先认证相应的密码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Stop);
			break;
		default:
			MessageErrInf(status);
			break;
	}
}

六、修改SLIX1830标签UID

private void button21_Click(object sender, EventArgs e)
{
	byte[] olduidbuf = new byte[8];  //旧uid缓冲
	byte[] newuidbuf = new byte[8];  //新UID缓冲           

	//控制字指定,控制字的含义请查看本公司网站提供的动态库说明
	byte ctrlword = 0;
	byte afi = System.Convert.ToByte(textBox1.Text, 16);//以十六进制方式转成数值
	for (int i = 0; i < 8; i++)
	{
		olduidbuf[i] = 0;
	}
	if (checkhexstr(textBox4.Text, 8, newuidbuf, 0) == false)
	{
		MessageBox.Show("需要写入的卡片UID错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
		return;
	}

	byte status = iso15693writeuid(ctrlword, afi, olduidbuf, newuidbuf);
	if (status == 0)
	{
		pcdbeep(50);
		MessageBox.Show("UID:" + textBox4.Text + " 写入卡内成功!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
	}
	else { MessageErrInf(status); }
}


网站公告

今日签到

点亮在社区的每一天
去签到