Skip to content

Commit

Permalink
编辑器完成
Browse files Browse the repository at this point in the history
  • Loading branch information
XAS-712 committed Jan 12, 2019
1 parent 9f548db commit 62436b8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 48 deletions.
62 changes: 32 additions & 30 deletions MifareOneTool/ClassMifareS50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public static byte[] ReadRAC(byte[] ac)
{
ac[i] = (byte)~ac[i];
}
acbits[0] = (byte)(((ac[0] & 0x01) <<2)
+ ((ac[0] & 0x10) >>3)
acbits[0] = (byte)(((ac[0] & 0x01) << 2)
+ ((ac[0] & 0x10) >> 3)
+ ((ac[1] & 0x01)));
acbits[1] = (byte)(((ac[0] & 0x02) <<1)
+ ((ac[0] & 0x20)>>4)
acbits[1] = (byte)(((ac[0] & 0x02) << 1)
+ ((ac[0] & 0x20) >> 4)
+ ((ac[1] & 0x02) >> 1));
acbits[2] = (byte)(((ac[0] & 0x04))
+ ((ac[0] & 0x40) >> 5)
+ ((ac[1] & 0x04) >> 2));
acbits[3] = (byte)(((ac[0] & 0x08) >>1)
acbits[3] = (byte)(((ac[0] & 0x08) >> 1)
+ ((ac[0] & 0x80) >> 6)
+ ((ac[1] & 0x08) >> 3));
return acbits;
Expand All @@ -51,34 +51,35 @@ public static byte[] GenAC(byte[] ac)
{
byte[] acbits = new byte[4];
acbits[3] = 0x00;
acbits[1] = (byte)((ac[0]<< 2)& 0x10
| (ac[1] << 3) & 0x20
| (ac[2] << 4) & 0x40
| (ac[3]<<5)&0x80);
acbits[2] = (byte)((ac[0]>>1)& 0x01
| (ac[1]) & 0x02
| (ac[2] << 1) & 0x04
| (ac[3]<<2)&0x08);
acbits[2] = (byte)((ac[0] << 4) & 0x10
| (ac[1] << 5) & 0x20
| (ac[2] << 6) & 0x40
| (ac[3] << 7) & 0x80);
acbits[1] = (byte)(((ac[0] << 2) & 0x10)
| ((ac[1] << 3) & 0x20)
| ((ac[2] << 4) & 0x40)
| ((ac[3] << 5) & 0x80));
acbits[2] = (byte)(((ac[0] >> 1) & 0x01)
| ((ac[1]) & 0x02)
| ((ac[2] << 1) & 0x04)
| ((ac[3] << 2) & 0x08)
| ((ac[0] << 4) & 0x10)
| ((ac[1] << 5) & 0x20)
| ((ac[2] << 6) & 0x40)
| ((ac[3] << 7) & 0x80));
for (int i = 0; i < ac.Length; i++)
{
ac[i] = (byte)(ac[i] ^ 0xff);
ac[i] = (byte)~ac[i];
}
acbits[1] = (byte)((ac[0]) & 0x01
| (ac[1] << 1) & 0x02
| (ac[2] << 2) & 0x04
| (ac[3] << 3) & 0x08);
acbits[0] = (byte)((ac[0]>>2) & 0x01
| (ac[1] >>1) & 0x02
| (ac[2]) & 0x04
| (ac[3] << 1) & 0x08);
acbits[0] = (byte)((ac[0] << 3) & 0x10
| (ac[1] << 4) & 0x20
| (ac[2] << 5) & 0x40
| (ac[3] << 6) & 0x80);
acbits[1] = (byte)(acbits[1] |
((ac[0]) & 0x01)
| ((ac[1] << 1) & 0x02)
| ((ac[2] << 2) & 0x04)
| ((ac[3] << 3) & 0x08));
acbits[0] = (byte)(((ac[0] >> 2) & 0x01)
| ((ac[1] >> 1) & 0x02)
| ((ac[2]) & 0x04)
| ((ac[3] << 1) & 0x08)
| ((ac[0] << 3) & 0x10)
| ((ac[1] << 4) & 0x20)
| ((ac[2] << 5) & 0x40)
| ((ac[3] << 6) & 0x80));
return acbits;
}
}
Expand Down Expand Up @@ -308,6 +309,7 @@ public void LoadFromMfd(string file)
public void ExportToMfd(string file)
{
byte[] fileBuffer = this.SectorsRaw;
File.WriteAllBytes(file, fileBuffer);
}
}
}
3 changes: 2 additions & 1 deletion MifareOneTool/FormHTool.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 75 additions & 6 deletions MifareOneTool/FormHTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using Microsoft.VisualBasic;

namespace MifareOneTool
{
Expand All @@ -20,6 +22,7 @@ public FormHTool()

private S50 currentS50 = new S50();
string currentFilename = "";
int currentSector = -1;

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
Expand All @@ -37,6 +40,7 @@ private void logAppend(string msg)
}
private void reloadEdit(int sectorIndex)
{
currentSector = sectorIndex;
if (sectorIndex == -1)
{
comboBox1.SelectedIndex = 0;
Expand Down Expand Up @@ -123,7 +127,7 @@ private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
return;
}
reloadList();
logAppend("打开了"+ofd.FileName);
logAppend("打开了" + ofd.FileName);
}

private void reloadList()
Expand Down Expand Up @@ -188,7 +192,7 @@ private void block0Edit_Validating(object sender, CancelEventArgs e)
const string pattern = @"[0-9A-Fa-f]{32}";
TextBox tb = ((TextBox)sender);
string content = tb.Text.Trim();
if (!(Regex.IsMatch(content, pattern)))
if (!(Regex.IsMatch(content, pattern)&&content.Length==32))
{
tb.BackColor = Color.Tomato;
//e.Cancel = true;
Expand All @@ -205,7 +209,7 @@ private void keyAEdit_Validating(object sender, CancelEventArgs e)
const string pattern = @"[0-9A-Fa-f]{12}";
TextBox tb = ((TextBox)sender);
string content = tb.Text.Trim();
if (!(Regex.IsMatch(content, pattern)))
if (!(Regex.IsMatch(content, pattern) && content.Length == 12))
{
tb.BackColor = Color.Tomato;
//e.Cancel = true;
Expand All @@ -217,10 +221,51 @@ private void keyAEdit_Validating(object sender, CancelEventArgs e)
}
}

private void buttonSaveSectorEdit_Click(object sender, EventArgs e)
private static byte[] Hex2Block(string hex,int bytelen)
{
this.ValidateChildren();
hex = hex.Replace(" ", "");
byte[] returnBytes = new byte[bytelen];
for (int i = 0; i < bytelen; i++)
returnBytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
return returnBytes;
}

private void buttonSaveSectorEdit_Click(object sender, EventArgs e)
{
if (currentSector >= 0 && currentSector <= 15)
{
this.ValidateChildren();
if (block0Edit.BackColor != Color.Aquamarine
|| block1Edit.BackColor != Color.Aquamarine
|| block2Edit.BackColor != Color.Aquamarine
|| keyAEdit.BackColor != Color.Aquamarine
|| keyBEdit.BackColor != Color.Aquamarine)
{
MessageBox.Show("当前扇区数据仍有错误,不能执行修改。");
return;
}
currentS50.Sectors[currentSector].Block[0] = Hex2Block(block0Edit.Text.Trim(),16);
currentS50.Sectors[currentSector].Block[1] = Hex2Block(block1Edit.Text.Trim(), 16);
currentS50.Sectors[currentSector].Block[2] = Hex2Block(block2Edit.Text.Trim(), 16);
byte[] kA = Hex2Block(keyAEdit.Text.Trim(), 6);
byte[] kB = Hex2Block(keyBEdit.Text.Trim(), 6);
byte[] ac = new byte[4] {
(byte)comboBox1.SelectedIndex,
(byte)comboBox2.SelectedIndex,
(byte)comboBox3.SelectedIndex,
(byte)comboBox4.SelectedIndex };
byte[] kC = Utils.GenAC(ac);
List<byte> list3=new List<byte>(kA);
list3.AddRange(kC);
list3.AddRange(kB);
byte[] block3 = list3.Take(16).ToArray();
currentS50.Sectors[currentSector].Block[3] = block3;
for (int i = 0; i < 16; i++)
{
dataGridView1.Rows[i].Cells[0].Value = currentS50.Sectors[i].Info(i);
}
logAppend("已更新扇区" + currentSector.ToString());
}
}

private void comboBox1_Validating(object sender, CancelEventArgs e)
Expand Down Expand Up @@ -249,7 +294,7 @@ private void 检查全卡ToolStripMenuItem_Click(object sender, EventArgs e)
string msg = "该文件存在以下错误:\n";
for (int i = 0; i < 16; i++)
{
msg += "扇区" + i.ToString()+"\n";
msg += "扇区" + i.ToString() + "\n";
if ((res[i] & 0x01) == 0x01)
{
msg += "该扇区UID校验值错误,请点击打开扇区0来自动更正。\n";
Expand All @@ -271,5 +316,29 @@ private void 检查全卡ToolStripMenuItem_Click(object sender, EventArgs e)
logAppend(msg);
}
}

private void 修改UIDToolStripMenuItem_Click(object sender, EventArgs e)
{
reloadEdit(-1);
byte[] buid = new byte[4];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(buid);
string uid = Interaction.InputBox("请输入需要更改的UID卡号,共8位十六进制数,如E44A3BF1。", "请输入UID号", Form1.hex(buid), -1, -1).Trim();
string pat = "[0-9A-Fa-f]{8}";
if (!Regex.IsMatch(uid, pat))
{
MessageBox.Show("输入的UID号不合法", "InputError", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
buid = Hex2Block(uid, 4);
byte bcc=(byte)(buid[0]^buid[1]^buid[2]^buid[3]);
currentS50.Sectors[0].Block[0][0] = buid[0];
currentS50.Sectors[0].Block[0][1] = buid[1];
currentS50.Sectors[0].Block[0][2] = buid[2];
currentS50.Sectors[0].Block[0][3] = buid[3];
currentS50.Sectors[0].Block[0][4] = bcc;
logAppend("UID已改为" + Form1.hex(buid) + ",计算得到BCC=" + Form1.hex(new byte[]{bcc}));
reloadEdit(0);
}
}
}
11 changes: 0 additions & 11 deletions MifareOneTool/GitHubUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ class GitHubUpdate
{
Version localVersion;
Version remoteVersion;
//public static T FromJSON<T>(this string input)
//{
// try
// {
// return JsonConvert.DeserializeObject<T>(input);
// }
// catch (Exception ex)
// {
// return default(T);
// }
//}
public GitHubUpdate(string GitHubR)
{
this.localVersion = Assembly.GetExecutingAssembly().GetName().Version;
Expand Down

0 comments on commit 62436b8

Please sign in to comment.