private void btnReadSector3_Click(object sender, EventArgs e) { short icdev = 0x0000; int status; byte mode = 0x60; byte secnr = 0x00; txtDataOne3.Text = ""; txtDataTwo3.Text = ""; txtDataThree3.Text = ""; txtKeyA3.Text = ""; txtKey3.Text = ""; txtKeyB3.Text = ""; if (rbtKeyB3.Checked) { mode = 0x61; } secnr = Convert.ToByte(cbxMass3.Text); IntPtr keyBuffer = Marshal.AllocHGlobal(1024); byte[] bytesKey = Utils.ToDigitsBytes(txtInputKey3.Text); for (int i = 0; i < bytesKey.Length; i++) { Marshal.WriteByte(keyBuffer, i, bytesKey[i]); } status = HFReader.rf_M1_authentication2(icdev, mode, (byte)(secnr * 4), keyBuffer); Marshal.FreeHGlobal(keyBuffer); if (status != 0) { MessageBox.Show("rf_M1_authentication2 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } IntPtr dataBuffer = Marshal.AllocHGlobal(1024); for (int i = 0; i < 4; i++) { int j; byte cLen = 0; status = HFReader.rf_M1_read(icdev, (byte)((secnr * 4) + i), dataBuffer, ref cLen); if (status != 0 || cLen != 16) { MessageBox.Show("rf_M1_read failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); Marshal.FreeHGlobal(dataBuffer); return; } byte[] bytesData = new byte[16]; for (j = 0; j < bytesData.Length; j++) { bytesData[j] = Marshal.ReadByte(dataBuffer, j); } if (i == 0) { txtDataOne3.Text = Utils.ToHexString(bytesData); } else if (i == 1) { txtDataTwo3.Text = Utils.ToHexString(bytesData); } else if (i == 2) { txtDataThree3.Text = Utils.ToHexString(bytesData); } else if (i == 3) { byte[] byteskeyA = new byte[6]; byte[] byteskey = new byte[4]; byte[] byteskeyB = new byte[6]; for (j = 0; j < 16; j++) { if (j < 6) { byteskeyA[j] = bytesData[j]; } else if (j >= 6 && j < 10) { byteskey[j - 6] = bytesData[j]; } else { byteskeyB[j - 10] = bytesData[j]; } } txtKeyA3.Text = ""; // Utils.ToHexString(byteskeyA); txtKey3.Text = Utils.ToHexString(byteskey); txtKeyB3.Text = Utils.ToHexString(byteskeyB); } } Marshal.FreeHGlobal(dataBuffer); }
private void DoWriteBlock(int blockNo) { short icdev = 0x0000; int status; byte mode = 0x60; byte secnr = 0x00; byte adr; int i; if (rbtKeyB3.Checked) { mode = 0x61; } secnr = Convert.ToByte(cbxMass3.Text); adr = (byte)(blockNo + secnr * 4); if (blockNo == 3) { if (DialogResult.No == MessageBox.Show("Are you sure you want to overwrite block3!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)) { return; } } IntPtr keyBuffer = Marshal.AllocHGlobal(1024); byte[] bytesKey = Utils.ToDigitsBytes(txtInputKey3.Text); for (i = 0; i < bytesKey.Length; i++) { Marshal.WriteByte(keyBuffer, i, bytesKey[i]); } status = HFReader.rf_M1_authentication2(icdev, mode, (byte)(secnr * 4), keyBuffer); Marshal.FreeHGlobal(keyBuffer); if (status != 0) { MessageBox.Show("rf_M1_authentication2 failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // byte[] bytesBlock; if (blockNo == 0) { bytesBlock = Utils.ToDigitsBytes(txtDataOne3.Text); } else if (blockNo == 1) { bytesBlock = Utils.ToDigitsBytes(txtDataTwo3.Text); } else if (blockNo == 2) { bytesBlock = Utils.ToDigitsBytes(txtDataThree3.Text); } else { if (string.IsNullOrEmpty(txtKeyA3.Text) || txtKeyA3.Text.Length < 12) { MessageBox.Show("Please enter keyA (12 symbols)"); return; } String strBlock3 = txtKeyA3.Text; strBlock3 += txtKey3.Text; strBlock3 += txtKeyB3.Text; bytesBlock = Utils.ToDigitsBytes(strBlock3); } IntPtr dataBuffer = Marshal.AllocHGlobal(1024); for (i = 0; i < bytesBlock.Length; i++) { Marshal.WriteByte(dataBuffer, i, bytesBlock[i]); } status = HFReader.rf_M1_write(icdev, adr, dataBuffer); Marshal.FreeHGlobal(dataBuffer); if (status != 0) { MessageBox.Show("rf_M1_write failed!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }