diff --git a/MifareOneTool/ClassMifareS50.cs b/MifareOneTool/ClassMifareS50.cs index e2a8ea2..9133362 100644 --- a/MifareOneTool/ClassMifareS50.cs +++ b/MifareOneTool/ClassMifareS50.cs @@ -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; @@ -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; } } @@ -308,6 +309,7 @@ public void LoadFromMfd(string file) public void ExportToMfd(string file) { byte[] fileBuffer = this.SectorsRaw; + File.WriteAllBytes(file, fileBuffer); } } } diff --git a/MifareOneTool/FormHTool.Designer.cs b/MifareOneTool/FormHTool.Designer.cs index cd19c63..6c050dd 100644 --- a/MifareOneTool/FormHTool.Designer.cs +++ b/MifareOneTool/FormHTool.Designer.cs @@ -150,6 +150,7 @@ private void InitializeComponent() this.修改UIDToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.U))); this.修改UIDToolStripMenuItem.Size = new System.Drawing.Size(191, 24); this.修改UIDToolStripMenuItem.Text = "修改UID"; + this.修改UIDToolStripMenuItem.Click += new System.EventHandler(this.修改UIDToolStripMenuItem_Click); // // 检查全卡ToolStripMenuItem // @@ -486,7 +487,7 @@ private void InitializeComponent() this.MainMenuStrip = this.menuStrip1; this.MaximizeBox = false; this.Name = "FormHTool"; - this.Text = "S50HTool"; + this.Text = "S50HTool-beta"; this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); this.groupBox1.ResumeLayout(false); diff --git a/MifareOneTool/FormHTool.cs b/MifareOneTool/FormHTool.cs index 885aed8..17b663a 100644 --- a/MifareOneTool/FormHTool.cs +++ b/MifareOneTool/FormHTool.cs @@ -8,6 +8,8 @@ using System.Windows.Forms; using System.IO; using System.Text.RegularExpressions; +using System.Security.Cryptography; +using Microsoft.VisualBasic; namespace MifareOneTool { @@ -20,6 +22,7 @@ public FormHTool() private S50 currentS50 = new S50(); string currentFilename = ""; + int currentSector = -1; private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { @@ -37,6 +40,7 @@ private void logAppend(string msg) } private void reloadEdit(int sectorIndex) { + currentSector = sectorIndex; if (sectorIndex == -1) { comboBox1.SelectedIndex = 0; @@ -123,7 +127,7 @@ private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) return; } reloadList(); - logAppend("打开了"+ofd.FileName); + logAppend("打开了" + ofd.FileName); } private void reloadList() @@ -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; @@ -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; @@ -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 list3=new List(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) @@ -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"; @@ -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); + } } } diff --git a/MifareOneTool/GitHubUpdate.cs b/MifareOneTool/GitHubUpdate.cs index 2f022e2..66d5e83 100644 --- a/MifareOneTool/GitHubUpdate.cs +++ b/MifareOneTool/GitHubUpdate.cs @@ -14,17 +14,6 @@ class GitHubUpdate { Version localVersion; Version remoteVersion; - //public static T FromJSON(this string input) - //{ - // try - // { - // return JsonConvert.DeserializeObject(input); - // } - // catch (Exception ex) - // { - // return default(T); - // } - //} public GitHubUpdate(string GitHubR) { this.localVersion = Assembly.GetExecutingAssembly().GetName().Version;