MIFARE Classic Command Flow Diagram
這個程式將圖片中的 MIFARE Classic Command Flow Diagram 完整繪製在畫布上(包含中英文對照、虛線錯誤返回路徑、實線流程箭頭以及右側的「典型交易時間」資訊),並提供「上一步 / 下一步 / 重置」按鈕,讓您可以一個步驟、一個步驟地高亮顯示並檢視每個階段的說明與路徑走向。
程式功能
雙語完整重現:完整對照您提供的左圖(NXP 原廠英文規範與典型交易時間
~2.5 ms、~2 ms等)與右圖(繁體中文流程圖),包含所有實線正常路徑與虛線錯誤返回路徑 。視覺化狀態區分:
步驟 0(總覽):顯示完整的彩色流程圖。
逐步執行(步驟 1 ~ 8):當前執行的方塊與進入該步驟的箭頭會以亮橘色粗框高亮,已經歷過的步驟保留薄荷綠,尚未執行的步驟轉為淺灰色,讓流程走向一目了然。
右側同步解說面板:每按一次「下一步」,右側會同步更新該階段所屬的通訊程序(識別與選卡、三階段認證、記憶體操作)以及底層協定細節(如
REQA 0x26、WUPA 0x52、數值加減需搭配Transfer寫入等)。
- 卡片喚醒與選卡(ISO/IEC 14443-3)
- 讀卡機送出
REQA(僅喚醒 IDLE 卡片)或WUPA(喚醒 IDLE 與 HALT 卡片),卡片回應ATQA確認支援 14443A。 - 透過防碰撞指令取得卡片 4-byte 或 7-byte UID(搭配 BCC 校驗),再送出
SELECT並由卡片回傳SAK確認卡片容量與狀態(如 MIFARE Classic 1K / 4K)。
- 讀卡機送出
- 扇區認證(Authentication)
- MIFARE Classic 以**扇區(Sector)**為單位進行金鑰認證(1K 共 16 個扇區、每扇區 4 個 Block,每 Block 16 bytes)。
- 使用
0x60(Key A)或0x61(Key B)搭配目標區塊編號進行三階段挑戰-回應(3-pass authentication)與 Crypto-1 加密,未通過認證則無法執行讀寫或數值指令。 - 若要切換到其他扇區操作,必須重新針對新扇區執行認證指令。
- 記憶體與值區塊指令
- Read (
0x30) / Write (0xA0):針對已認證扇區內的區塊進行 16 bytes 讀寫(Block 0 製造商資料區通常唯讀;各扇區最後一個 Block 為 Sector Trailer,存放 Key A、存取控制位元與 Key B)。 - 值區塊(Value Block)指令:支援
Increment、Decrement、Restore、Transfer,用於 32-bit 有號整數餘額加減與暫存器轉移。
- Read (
import tkinter as tk
from tkinter import ttk
class MifareFlowApp:
def __init__(self, root):
self.root = root
self.root.title("MIFARE Classic Command Flow Diagram - 互動式步驟教學")
self.root.geometry("1120x780")
self.root.configure(bg="#F4F6F9")
self.current_step = 0
# 定義所有步驟的資料:包含高亮節點、高亮連線、階段標題與詳細說明
self.steps = [
{
"title": "準備就緒 (Ready)",
"phase": "尚未開始",
"desc": "點擊下方「下一步 (Next) ➔」按鈕,開始逐步解析 MIFARE Classic 卡片的指令通訊流程。",
"nodes": [],
"edges": []
},
{
"title": "步驟 1:通電啟動 (POR - Power On Reset)",
"phase": "Identification and Selection Procedure (識別與選卡程序)",
"desc": "卡片進入讀卡機的射頻 (RF) 磁場範圍內,獲得電力並完成內部重置 (POR),進入 IDLE (閒置) 狀態,等待讀卡機發送尋卡指令。",
"nodes": ["por"],
"edges": []
},
{
"title": "步驟 2:尋卡請求 / 喚醒卡片 (REQA / WUPA)",
"phase": "Identification and Selection Procedure (識別與選卡程序)",
"desc": "• Request Standard (REQA, 0x26):尋卡請求,僅喚醒處於 IDLE 狀態的卡片。\n"
"• Request All (WUPA, 0x52):喚醒所有卡片,包含處於 HALT (暫停) 狀態的卡片。\n"
"卡片收到後會回傳 ATQA (Answer To Request)。",
"nodes": ["reqa", "wupa"],
"edges": ["e_por_split", "e_por_reqa", "e_por_wupa"]
},
{
"title": "步驟 3:防衝突迴圈 (Anticollision Loop - Get Identifier)",
"phase": "Identification and Selection Procedure (~2.5 ms 無衝突)",
"desc": "當磁場中有多張卡片同時回應時,透過防衝突演算法逐一取得每張卡片的唯一識別碼 (UID)。\n"
"• 無衝突約需 ~2.5 ms;若為 7-byte UID 需額外 +1 ms;每次發生碰撞約 +1 ms。\n"
"• 若發生錯誤或逾時(虛線),將退回初始狀態重新尋卡。\n"
"• 註:若已知卡片 UID(例如剛從 HALT 喚醒),可由 WUPA 直接跳過防衝突進入選卡(右側旁路)。",
"nodes": ["anticoll"],
"edges": ["e_reqa_down", "e_wupa_down", "e_merge_anticoll", "e_anticoll_err", "e_wupa_bypass"]
},
{
"title": "步驟 4:選擇卡片 (Select Card)",
"phase": "Identification and Selection Procedure (識別與選卡程序)",
"desc": "讀卡機發送 SELECT 指令並帶入目標卡片的 UID,鎖定單一卡片進行後續通訊。\n"
"卡片會回傳 SAK (Select Acknowledge),確認選卡成功並進入 ACTIVE (啟動) 狀態。",
"nodes": ["select"],
"edges": ["e_anticoll_select", "e_select_err"]
},
{
"title": "步驟 5:區段三階段認證 (3 Pass Authentication on specific sector)",
"phase": "Authentication Procedure (~2 ms)",
"desc": "在讀寫任何記憶體區塊之前,必須先針對該區塊所屬的區段 (Sector) 進行金鑰 (Key A 或 Key B) 的三階段加密認證。\n"
"• 典型認證時間約為 ~2 ms。\n"
"• 認證失敗(虛線)將退回至最左側主幹,重新開始尋卡程序。",
"nodes": ["auth"],
"edges": ["e_select_auth", "e_auth_err"]
},
{
"title": "步驟 6:記憶體操作 (Memory Operations)",
"phase": "Memory Operations (記憶體讀寫與數值運算)",
"desc": "認證成功後,可對該區段內的區塊執行以下操作:\n"
"• 讀取區塊 (Read Block):~2.5 ms\n"
"• 寫入區塊 (Write Block):~5.5 ms\n"
"• 減值 / 加值 / 還原 (Decrement / Increment / Restore):~2.5 ms(運算結果會先暫存於卡片內部的揮發性暫存器,需搭配下一步的 Transfer 才會寫入 EEPROM)。",
"nodes": ["read", "write", "dec", "inc", "rest"],
"edges": ["e_auth_bus", "e_bus_read", "e_bus_write", "e_bus_dec", "e_bus_inc", "e_bus_rest"]
},
{
"title": "步驟 7:轉存 (Transfer)",
"phase": "Memory Operations (~4.5 ms)",
"desc": "將上一步驟中「減值 (Decrement)、加值 (Increment) 或還原 (Restore)」在內部暫存器計算的結果,正式寫入指定的非揮發性記憶體區塊中。\n"
"• 典型轉存時間約為 ~4.5 ms。\n"
"• 完成讀寫或轉存後,可沿左側迴圈繼續操作其他區塊,或重新進行其他區段的認證。",
"nodes": ["transfer"],
"edges": ["e_dec_trans", "e_inc_trans", "e_rest_trans", "e_read_loop", "e_write_loop", "e_trans_loop"]
},
{
"title": "步驟 8:暫停 (Halt)",
"phase": "結束通訊 / 進入休眠",
"desc": "所有操作完成後,讀卡機發送 HALT 指令,使卡片進入 HALT (暫停/休眠) 狀態。\n"
"• 進入 HALT 的卡片不會再回應一般的 Request Standard (REQA)。\n"
"• 若未來需要再次操作此卡片,必須發送 Request All / WUPA (右側回溯箭頭) 將其喚醒。",
"nodes": ["halt"],
"edges": ["e_bus_halt", "e_halt_wupa"]
}
]
self.setup_ui()
self.draw_diagram()
self.update_ui()
def setup_ui(self):
# 頂部標題列
header = tk.Frame(self.root, bg="#1E293B", pady=12)
header.pack(fill=tk.X)
tk.Label(
header, text="MIFARE Classic Command Flow Diagram (指令流程圖互動演示)",
font=("Microsoft JhengHei", 15, "bold"), fg="white", bg="#1E293B"
).pack()
# 中央主容器 (左側流程圖畫布 + 右側步驟說明面板)
main_frame = tk.Frame(self.root, bg="#F4F6F9", padx=15, pady=10)
main_frame.pack(fill=tk.BOTH, expand=True)
# 左側 Canvas
canvas_frame = tk.Frame(main_frame, bg="white", bd=1, relief=tk.SOLID)
canvas_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.canvas = tk.Canvas(canvas_frame, width=740, height=580, bg="white", highlightthickness=0)
self.canvas.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# 右側說明與控制面板
right_panel = tk.Frame(main_frame, width=340, bg="#F4F6F9", padx=12)
right_panel.pack(side=tk.RIGHT, fill=tk.Y)
right_panel.pack_propagate(False)
# 步驟進度標籤
self.lbl_progress = tk.Label(
right_panel, text="步驟 0 / 8", font=("Microsoft JhengHei", 11, "bold"),
fg="#0284C7", bg="#E0F2FE", padx=10, pady=4
)
self.lbl_progress.pack(anchor="w", pady=(0, 8))
# 所屬階段
self.lbl_phase = tk.Label(
right_panel, text="", font=("Microsoft JhengHei", 9, "bold"),
fg="#64748B", bg="#F4F6F9", wraplength=310, justify="left"
)
self.lbl_phase.pack(anchor="w", pady=(0, 5))
# 步驟標題
self.lbl_title = tk.Label(
right_panel, text="", font=("Microsoft JhengHei", 13, "bold"),
fg="#0F172A", bg="#F4F6F9", wraplength=310, justify="left"
)
self.lbl_title.pack(anchor="w", pady=(0, 10))
# 步驟詳細說明卡片
desc_card = tk.Frame(right_panel, bg="white", bd=1, relief=tk.SOLID, padx=12, pady=12)
desc_card.pack(fill=tk.BOTH, expand=True, pady=(0, 15))
# 修正:將 10.5 改為整數 10
self.lbl_desc = tk.Label(
desc_card, text="", font=("Microsoft JhengHei", 10),
fg="#334155", bg="white", wraplength=285, justify="left", anchor="nw"
)
self.lbl_desc.pack(fill=tk.BOTH, expand=True)
# 圖例說明
legend_frame = tk.LabelFrame(right_panel, text="圖例說明", font=("Microsoft JhengHei", 9, "bold"), bg="#F4F6F9", fg="#475569", padx=8, pady=6)
legend_frame.pack(fill=tk.X, pady=(0, 12))
tk.Label(legend_frame, text="───▶ 正常流程 (實線)", font=("Microsoft JhengHei", 9), fg="#1E293B", bg="#F4F6F9").pack(anchor="w")
tk.Label(legend_frame, text="- - - ▶ 錯誤 / 逾時返回 (虛線)", font=("Microsoft JhengHei", 9), fg="#64748B", bg="#F4F6F9").pack(anchor="w")
tk.Label(legend_frame, text="■ 亮橘框:當前執行步驟", font=("Microsoft JhengHei", 9), fg="#EA580C", bg="#F4F6F9").pack(anchor="w")
# 控制按鈕區
btn_frame = tk.Frame(right_panel, bg="#F4F6F9")
btn_frame.pack(fill=tk.X, side=tk.BOTTOM)
self.btn_prev = tk.Button(
btn_frame, text="⬅ 上一步", font=("Microsoft JhengHei", 10, "bold"),
bg="#E2E8F0", fg="#1E293B", relief=tk.FLAT, padx=12, pady=8,
cursor="hand2", command=self.prev_step
)
self.btn_prev.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(0, 5))
self.btn_next = tk.Button(
btn_frame, text="下一步 ➔", font=("Microsoft JhengHei", 10, "bold"),
bg="#0284C7", fg="white", relief=tk.FLAT, padx=12, pady=8,
cursor="hand2", command=self.next_step
)
self.btn_next.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(5, 0))
self.btn_reset = tk.Button(
right_panel, text="↺ 重新開始 (Reset)", font=("Microsoft JhengHei", 9),
bg="#F4F6F9", fg="#64748B", relief=tk.FLAT, pady=4,
cursor="hand2", command=self.reset_steps
)
self.btn_reset.pack(fill=tk.X, side=tk.BOTTOM, pady=(0, 6))
def draw_rounded_rect(self, x1, y1, x2, y2, radius=18, **kwargs):
points = [
x1+radius, y1, x1+radius, y1, x2-radius, y1, x2-radius, y1,
x2, y1, x2, y1+radius, x2, y1+radius, x2, y2-radius,
x2, y2-radius, x2, y2, x2-radius, y2, x2-radius, y2,
x1+radius, y2, x1+radius, y2, x1, y2, x1, y2-radius,
x1, y2-radius, x1, y1+radius, x1, y1+radius, x1, y1
]
return self.canvas.create_polygon(points, **kwargs, smooth=True)
def draw_diagram(self):
c = self.canvas
self.node_items = {}
self.edge_items = {}
# 頂部欄位標題
c.create_text(220, 18, text="Transaction Sequence (交易序列)", font=("Microsoft JhengHei", 9, "bold"), fill="#475569")
c.create_text(615, 18, text="Typical Transaction Time (典型交易時間)", font=("Microsoft JhengHei", 9, "bold"), fill="#475569")
# 右側時間軸與說明區塊
# 1. Identification & Selection
c.create_line(515, 70, 515, 275, arrow=tk.LAST, fill="#475569", width=1.5)
c.create_rectangle(535, 110, 725, 145, outline="#334155", width=1.5)
c.create_text(630, 127, text="Identification and Selection\nProcedure", font=("Arial", 8, "bold"), justify="center")
c.create_text(535, 165, text="~2.5 ms without collision\n+ ~1 ms for 7-byte UID\n+ ~1 ms for each collision",
font=("Arial", 8), anchor="nw", fill="#334155")
# 2. Authentication Procedure
c.create_line(515, 290, 515, 355, arrow=tk.LAST, fill="#475569", width=1.5)
c.create_rectangle(535, 295, 725, 318, outline="#334155", width=1.5)
c.create_text(630, 306, text="Authentication Procedure", font=("Arial", 8, "bold"))
c.create_text(535, 326, text="~2 ms", font=("Arial", 8), anchor="nw", fill="#334155")
# 3. Memory Operations
c.create_line(515, 370, 515, 535, arrow=tk.LAST, fill="#475569", width=1.5)
c.create_rectangle(535, 375, 725, 398, outline="#334155", width=1.5)
c.create_text(630, 386, text="Memory Operations", font=("Arial", 8, "bold"))
c.create_text(535, 405, text="~2.5 ms read block\n~5.5 ms write block\n~2.5 ms de-/increment\n~4.5 ms transfer",
font=("Arial", 8), anchor="nw", fill="#334155")
# 繪製連線 (Edges)
def add_edge(name, coords, arrow=tk.NONE, dash=None):
line = c.create_line(*coords, arrow=arrow, dash=dash, fill="#64748B", width=1.6)
self.edge_items[name] = line
# POR 到 REQA / WUPA
add_edge("e_por_split", [110, 62, 110, 88], arrow=tk.LAST)
add_edge("e_por_reqa", [45, 75, 240, 75], dash=(4, 3))
add_edge("e_por_wupa", [240, 75, 240, 88], arrow=tk.LAST, dash=(4, 3))
# REQA / WUPA 到 Anticollision
add_edge("e_reqa_down", [110, 128, 110, 145], arrow=tk.LAST)
add_edge("e_wupa_down", [240, 128, 240, 145], arrow=tk.LAST)
add_edge("e_merge_anticoll", [110, 145, 240, 145, 175, 145, 175, 165], arrow=tk.LAST)
# WUPA 直接到 Select Card 的右側旁路 (Bypass)
add_edge("e_wupa_bypass", [240, 145, 330, 145, 330, 255, 255, 255], arrow=tk.LAST)
# Anticollision 到 Select Card & 錯誤虛線
add_edge("e_anticoll_select", [175, 210, 175, 235], arrow=tk.LAST)
add_edge("e_anticoll_err", [95, 188, 45, 188, 45, 75], arrow=tk.LAST, dash=(4, 3))
# Select Card 到 Auth & 錯誤虛線
add_edge("e_select_auth", [175, 275, 175, 315], arrow=tk.LAST)
add_edge("e_select_err", [95, 255, 45, 255], arrow=tk.LAST, dash=(4, 3))
# Auth 錯誤虛線 (回到左側主幹)
add_edge("e_auth_err", [95, 338, 45, 338, 45, 255], arrow=tk.LAST, dash=(4, 3))
# Auth 到下方操作匯流排 (Bus)
add_edge("e_auth_bus", [175, 360, 175, 385])
c.create_line(60, 385, 420, 385, fill="#64748B", width=1.6)
add_edge("e_bus_read", [60, 385, 60, 405], arrow=tk.LAST)
add_edge("e_bus_write", [130, 385, 130, 405], arrow=tk.LAST)
add_edge("e_bus_dec", [200, 385, 200, 405], arrow=tk.LAST)
add_edge("e_bus_inc", [270, 385, 270, 405], arrow=tk.LAST)
add_edge("e_bus_rest", [340, 385, 340, 405], arrow=tk.LAST)
add_edge("e_bus_halt", [420, 385, 420, 405], arrow=tk.LAST)
# Dec / Inc / Rest 到 Transfer
add_edge("e_dec_trans", [200, 455, 270, 485], arrow=tk.LAST)
add_edge("e_inc_trans", [270, 455, 270, 485], arrow=tk.LAST)
add_edge("e_rest_trans", [340, 455, 270, 485], arrow=tk.LAST)
# Read / Write / Transfer 返回左側循環迴圈
add_edge("e_read_loop", [60, 455, 60, 508, 45, 508])
add_edge("e_write_loop", [130, 455, 130, 508, 60, 508])
add_edge("e_trans_loop", [185, 508, 45, 508, 45, 295, 175, 295], arrow=tk.LAST)
c.create_line(45, 385, 60, 385, arrow=tk.LAST, fill="#64748B", width=1.4)
c.create_line(175, 295, 420, 295, 420, 385, arrow=tk.LAST, fill="#64748B", width=1.4)
# Halt 回到 WUPA (右側大迴圈)
add_edge("e_halt_wupa", [455, 430, 480, 430, 480, 108, 305, 108], arrow=tk.LAST)
# 繪製節點方塊 (Nodes)
def add_box(key, x1, y1, x2, y2, text, rounded=False):
if rounded:
shape = self.draw_rounded_rect(x1, y1, x2, y2, radius=18, fill="#74B896", outline="#529471", width=1.5)
else:
shape = c.create_rectangle(x1, y1, x2, y2, fill="#74B896", outline="#529471", width=1.5)
# 修正:將 8.5 改為整數 9
txt = c.create_text((x1+x2)/2, (y1+y2)/2, text=text, font=("Microsoft JhengHei", 9, "bold"), fill="#0F172A", justify="center")
self.node_items[key] = (shape, txt)
add_box("por", 55, 32, 165, 62, "通電啟動 (POR)", rounded=True)
add_box("reqa", 45, 88, 175, 128, "尋卡請求 (REQA)\nRequest Standard")
add_box("wupa", 185, 88, 305, 128, "喚醒卡片 (WUPA)\nRequest All")
add_box("anticoll", 95, 165, 255, 210, "防衝突迴圈 (取ID)\nAnticollision Loop")
add_box("select", 95, 235, 255, 275, "選擇卡片\nSelect Card")
add_box("auth", 95, 315, 255, 360, "區段三階段認證\n3 Pass Authentication")
# 6 個記憶體操作方塊
add_box("read", 30, 405, 90, 455, "讀取區塊\nRead")
add_box("write", 100, 405, 160, 455, "寫入區塊\nWrite")
add_box("dec", 170, 405, 230, 455, "減值\nDecrement")
add_box("inc", 240, 405, 300, 455, "加值\nIncrement")
add_box("rest", 310, 405, 370, 455, "還原\nRestore")
add_box("halt", 385, 405, 455, 455, "暫停\nHalt")
# Transfer 方塊
add_box("transfer", 185, 485, 355, 530, "轉存 (Transfer)")
# 底部備註 (修正:將 7.5 改為整數 8)
c.create_text(
30, 558, anchor="w", font=("Arial", 8), fill="#64748B",
text="Note: Does not include Personalize UID Usage and SET_MOD_TYPE commands. (Figure 4. MIFARE Classic command flow diagram)"
)
def update_ui(self):
step_data = self.steps[self.current_step]
total = len(self.steps) - 1
# 更新右側文字
self.lbl_progress.config(text=f"進度:步驟 {self.current_step} / {total}")
self.lbl_phase.config(text=f"階段:{step_data['phase']}")
self.lbl_title.config(text=step_data['title'])
self.lbl_desc.config(text=step_data['desc'])
# 更新按鈕狀態
self.btn_prev.config(state=tk.NORMAL if self.current_step > 0 else tk.DISABLED)
if self.current_step == total:
self.btn_next.config(text="已完成 ✔", bg="#16A34A")
else:
self.btn_next.config(text="下一步 ➔", bg="#0284C7")
# 重置所有節點與連線樣式
active_nodes = set(step_data["nodes"])
active_edges = set(step_data["edges"])
visited_nodes = set()
for i in range(1, self.current_step):
visited_nodes.update(self.steps[i]["nodes"])
for key, (shape, txt) in self.node_items.items():
if key in active_nodes:
self.canvas.itemconfig(shape, fill="#FDBA74", outline="#EA580C", width=3)
self.canvas.itemconfig(txt, fill="#0F172A")
elif key in visited_nodes:
self.canvas.itemconfig(shape, fill="#74B896", outline="#3B7A57", width=1.8)
self.canvas.itemconfig(txt, fill="#0F172A")
else:
fill_color = "#74B896" if self.current_step == 0 else "#E2E8F0"
outline_color = "#529471" if self.current_step == 0 else "#CBD5E1"
txt_color = "#0F172A" if self.current_step == 0 else "#64748B"
self.canvas.itemconfig(shape, fill=fill_color, outline=outline_color, width=1.5)
self.canvas.itemconfig(txt, fill=txt_color)
for key, line in self.edge_items.items():
if key in active_edges:
self.canvas.itemconfig(line, fill="#EA580C", width=2.8)
else:
self.canvas.itemconfig(line, fill="#94A3B8" if self.current_step > 0 else "#475569", width=1.5)
def next_step(self):
if self.current_step < len(self.steps) - 1:
self.current_step += 1
self.update_ui()
def prev_step(self):
if self.current_step > 0:
self.current_step -= 1
self.update_ui()
def reset_steps(self):
self.current_step = 0
self.update_ui()
if __name__ == "__main__":
root = tk.Tk()
app = MifareFlowApp(root)
root.mainloop()









