Tkinter로 만드는 스마트카드 통신 프로그램 - 4

2017. 8. 4. 13:57IT관련

반응형

이전글: Tkinter로 만드는 스마트카드 통신 프로그램 - 3


Mutual AuthAuth


목표

1. Menu 추가함.

2. 기존 코드를 패키지화 할 것.

3. 상호인증(mutual authentication) UI 추가. 


특이사항

1. 패키지화 하면서 __init__.py를 추가함.


소스코드

main.py

# imports
# Use Tkinter for python 2, tkinter for python 3
import tkinter as tk
import scm.scm_frames as scmf

if __name__ == '__main__':
root = tk.Tk()
scmf.MainFrame(root).pack(side='top', fill='both', expand=True)
root.mainloop() # Start GUI


scm/__init__.py

"""__init__.py"""


scm/scm_frames.py

# imports
# Use Tkinter for python 2, tkinter for python 3
import tkinter as tk
import tkinter.scrolledtext as tkst
import tkinter.messagebox as tkmb
from tkinter import ttk
from tkinter import Menu

from smartcard.System import readers
from smartcard.util import toHexString, toBytes


# Define a Auth class
class AuthFrame(tk.Frame):

def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.parent.title('Mutual Auth') # Add a title
self.create_widgets()
self.pack()

def create_widgets(self):
self.lable_enc = ttk.Label(self, text='ENC')
self.lable_enc.grid(column=0, row=0)
self.entry_enc = tk.Entry(self)
self.entry_enc.grid(column=1, row=0, columnspan=2)
self.entry_enc.insert(tk.INSERT, '404142434445464748494A4B4C4D4E4F')

self.label_mac = ttk.Label(self, text='MAC')
self.label_mac.grid(column=0, row=1)
self.entry_mac = tk.Entry(self)
self.entry_mac.grid(column=1, row=1, columnspan=2)
self.entry_mac.insert(tk.INSERT, '404142434445464748494A4B4C4D4E4F')

self.label_dek = ttk.Label(self, text='DEK')
self.label_dek.grid(column=0, row=2)
self.entry_dek = tk.Entry(self)
self.entry_dek.grid(column=1, row=2, columnspan=2)
self.entry_dek.insert(tk.INSERT, '404142434445464748494A4B4C4D4E4F')

self.iv_rule = tk.IntVar()
self.radio_none = tk.Radiobutton(self, text="none", variable=self.iv_rule
, value=0)
self.radio_none.select()
self.radio_none.grid(column=0, row=3)
self.radio_cpg201 = tk.Radiobutton(self, text="cpg 2.0.1", variable=self.iv_rule
, value=1)
self.radio_cpg201.grid(column=1, row=3)
self.radio_cpg211 = tk.Radiobutton(self, text="cpg 2.1.1", variable=self.iv_rule
, value=2)
self.radio_cpg211.grid(column=2, row=3)

self.treeveiw_apps = ttk.Treeview(self)
self.treeveiw_apps.grid(column=0, row=4, columnspan=3)

self.button_auth = ttk.Button(self, text='Get Status...'
, command=self.click_getstatus)
self.button_auth.grid(column=0, row=5, columnspan=3)
self.button_auth.focus_set()

# Click a reset button
def click_getstatus(self):
pass


# Define a MainFrame class
class MainFrame(tk.Frame):

def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.parent.title('Manager') # Add a title
self.connection = None
self.pack()
self.create_widgets()
self.parent.resizable(0, 0) # Disable resizing the GUI

def create_widgets(self):
self.label_reader = ttk.Label(self, text='Reader')
self.label_reader.grid(column=0, row=0)

self.str_reader_name = tk.StringVar() # String variable
self.combo_reader = ttk.Combobox(self, width=30, state='readonly'
, textvariable=self.str_reader_name) # Create a combobox
self.combo_reader.grid(column=1, row=0)
self.combo_reader['values'] = readers()
try:
self.combo_reader.current(0)
except Exception:
pass

self.button_reset = ttk.Button(self, text='Reset...', command=self.click_reset) # Create a button
self.button_reset.grid(column=2, row=0)

self.st_log = tkst.ScrolledText(self, width=50, height=20, wrap=tk.WORD) # Create a scrolledtext
self.st_log.grid(column=0, row=1, columnspan=3)

self.entry_cmd = tk.Entry(self, width=40) # Create a entry
self.entry_cmd.grid(column=0, row=2, columnspan=2)
self.entry_cmd.focus_set()

self.button_cmd = ttk.Button(self, text='Send...', command=self.click_send) # Create a button
self.button_cmd.grid(column=2, row=2)

self.menu_bar = Menu(self.parent) # Create a menu
self.parent.config(menu=self.menu_bar)

self.menu_file = Menu(self.menu_bar, tearoff=0) # Create the File Menu
self.menu_file.add_command(label="Exit", command=self.click_exit) # Add the "Exit" menu and bind a function
self.menu_bar.add_cascade(label="File", menu=self.menu_file)

self.menu_util = Menu(self.menu_bar, tearoff=0) # Create the Util Menu
self.menu_util.add_command(label="Get Status", command=self.click_auth) # Add the "Auth" menu and bind a function
self.menu_bar.add_cascade(label="Util", menu=self.menu_util)

self.menu_help = Menu(self.menu_bar, tearoff=0)
self.menu_help.add_command(label="About", command=self.click_about) # Add the "About" menu and bind a function
self.menu_bar.add_cascade(label="Help", menu=self.menu_help)

# Click a reset button
def click_reset(self):
if self.connection is not None:
self.connection.disconnect()
self.connection = None
try:
for r in readers():
if r.name == self.str_reader_name.get():
self.connection = r.createConnection()
self.connection.connect()
self.st_log.insert(tk.END, 'ATR: '
+ toHexString(self.connection.getATR()) + '\n')
self.st_log.see(tk.END)
break
except Exception:
tkmb.showinfo('Error', 'Please check a card or readers...')

# Click a send button
def click_send(self):
apdu = toBytes(self.entry_cmd.get())
response, sw1, sw2 = self.connection.transmit(apdu)
#print('response: ', response, ' status words: ', "%x %x" % (sw1, sw2))
capdu = '< ' + toHexString(apdu) + '\n'
rapdu = ('> ' + toHexString(response) + '\n> {:02X}'.format(sw1)
+ ' {:02X}'.format(sw2) +'\n')
self.st_log.insert(tk.END, capdu + rapdu)
self.st_log.see(tk.END)

if sw1 == 0x61: # Get Response
getResponse = '00C00000' + '{:02X}'.format(sw2)
apdu = toBytes(getResponse)
response, sw1, sw2 = self.connection.transmit(apdu)
#print('response: ', response, ' status words: ', "%x %x" % (sw1, sw2))
capdu = '< ' + toHexString(apdu) + '\n'
rapdu = ('> ' + toHexString(response) + '\n> {:02X}'.format(sw1)
+ ' {:02X}'.format(sw2) +'\n')
self.st_log.insert(tk.END, capdu + rapdu)
self.st_log.see(tk.END)

# Click a exit menu
def click_exit(self):
self.parent.quit()
self.parent.destroy()
exit()

# Click a exit menu
def click_auth(self):
AuthFrame(tk.Toplevel()).pack(side='top', fill='both', expand=True)

# Click a abount menu
def click_about(self):
tkmb.showinfo('About', 'About...')


반응형