Exemple #1
0
def parseArgs(args):
    count = int(args.count, 16)
    if args.mode == 'ff' or args.mode == 'fs':
        if not os.path.exists(args.data):
            sys.stdout.write('Invalid Data!\n')
            return
        file = open(args.data, 'r')
        data = file.read()
        file.close()
    else: 
        data = args.data
    cipher = Cipher()
    out = ""
    if args.task == 'ip':
        out = cipher.IP(args.key, count, args.direct, args.bearer, data)
    elif args.task == 'ci': 
        out = cipher.encrypt(args.key, count, args.direct, args.bearer, data)
    else: 
        out = cipher.decrypt(args.key, count, args.direct, args.bearer, data)
    if args.mode == 'ss' or args.mode == 'fs':
        output(out, "")
    elif args.output:
        output(out, args.output)
    else: 
        outPath = raw_input('Please specify output path:')
        output(out, outPath)
class AES_Batch:
    def __init__(self):
        self.cipher = Cipher()

    def parseBatch(self, input, output):
        outFile = open(output, 'wb')
        writer = csv.writer(outFile)
        if not os.path.exists(input) or input == output:
            outFile.close()
            os.remove(output)
            return 'Invalid input path! \n'
        with open(input, 'rb') as csvfile:
            reader = csv.reader(csvfile, delimiter = ',')
            isHead = True
            for row in reader:
                if isHead:
                    isHead = False
                    continue
                id = row[0]
                task = row[1]
                key = row[2]
                cnt = int(row[3], 16)
                direct = row[4]
                bearer = int(row[5])
                source = row[7]

                if source == 'f':
                    if not os.path.exists(row[6]):
                        return 'Invalid path at task ' + id + '\n'
                    dataFile = open(row[6], 'r')
                    data = dataFile.read()
                    dataFile.close()
                elif source == 's':
                    data = row[6]
                else:
                    return 'Invalid data source ' + id + '\n'

                if task == 'Integrity Check':
                    out = self.cipher.IP(key, cnt, direct, bearer, data)
                elif task == 'Cipher':
                    out = self.cipher.encrypt(key, cnt, direct, bearer, data)
                elif task == 'Decipher':
                    out = self.cipher.decrypt(key, cnt, direct, bearer, data)
                else:
                    return 'Invalid task ' + id + '\n'
                writer.writerow((id, out))
        outFile.close()
        return ''
Exemple #3
0
def parseBatch(args):
    outFile = open(args.output, 'wb')
    writer = csv.writer(outFile)
    if not os.path.exists(args.batch):
        sys.stdout.write('Invalid input path! \n')
        return
    with open(args.batch, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter = ',')
        isHead = True
        for row in reader:
            if isHead:
                isHead = False
                continue
            id = row[0]
            task = row[1]
            key = row[2]
            cnt = int(row[3], 16)
            direct = row[4]
            bearer = int(row[5])
            source = row[7]

            if source == 'f':
                if not os.path.exists(row[6]):
                    sys.stdout.write('Invalid path at task ' + id + '\n')
                    exit()
                dataFile = open(row[6], 'r')
                data = dataFile.read()
                dataFile.close()
            elif source == 's':
                data = row[6]
            else:
                sys.stdout.write('Invalid data source ' + id + '\n')
            print(id + " : " + data)

            cipher = Cipher()
            if task == 'Integrity Check':
                out = cipher.IP(key, cnt, direct, bearer, data)
            elif task == 'Cipher':
                out = cipher.encrypt(key, cnt, direct, bearer, data)
            elif task == 'Decipher':
                out = cipher.decrypt(key, cnt, direct, bearer, data)
            else:
                sys.stdout.write('Invalid task ' + id + '\n')
                exit()
            writer.writerow((id, out))

    outFile.close()
Exemple #4
0
class AES_GUI(wx.Frame):

    def __init__(self, parent, title):
        self.cipher = Cipher()
        super(AES_GUI, self).__init__(parent, title=title,
            size=(960, 840))

        self.InitUI()
        self.Centre()
        self.Show()

    def onExit(self, event):
        self.Close()

    def InitUI(self):

        panel = wx.Panel(self)

        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox0 = wx.BoxSizer(wx.HORIZONTAL)
        taskList = ['IP Check', 'SRB Cipher', 'SRB Decipher', 'IP + SRB Cipher', 'SRB Decipher + IP',
                    'DRB Cipher', 'DRB Decipher']
        self.task = wx.RadioBox(panel, label='Task', choices=taskList)
        hbox0.Add(self.task,flag=wx.ALIGN_RIGHT|wx.ALL, border=20)
        vbox.Add(hbox0)

        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        directs = ['uplink', 'downlink']
        self.direction = wx.RadioBox(panel, label='Direction', choices=directs)
        hbox2.Add(self.direction,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        cnt_lb = wx.StaticText(panel, label='Count')
        hbox2.Add(cnt_lb,flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.TOP, border=20)
        self.cnt = wx.TextCtrl(panel)
        hbox2.Add(self.cnt,flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.TOP, border=20)
        bearer_lb = wx.StaticText(panel, label='Bearer')
        hbox2.Add(bearer_lb,flag=wx.ALIGN_RIGHT|wx.RIGHT|wx.TOP, border=20)
        self.bearer = wx.TextCtrl(panel)
        hbox2.Add(self.bearer,flag=wx.ALIGN_RIGHT|wx.TOP, border=20)
        vbox.Add(hbox2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)

        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        key_lb = wx.StaticText(panel, label='   IP Key')
        hbox1.Add(key_lb,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        self.key = wx.TextCtrl(panel)
        hbox1.Add(self.key, proportion=1)
        vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)

        hbox1_SRB = wx.BoxSizer(wx.HORIZONTAL)
        key_lb_SRB = wx.StaticText(panel, label='SRB Key')
        hbox1_SRB.Add(key_lb_SRB,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        self.key_SRB = wx.TextCtrl(panel)
        hbox1_SRB.Add(self.key_SRB, proportion=1)
        vbox.Add(hbox1_SRB, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)

        hbox1_DRB = wx.BoxSizer(wx.HORIZONTAL)
        key_lb_DRB = wx.StaticText(panel, label='DRB Key')
        hbox1_DRB.Add(key_lb_DRB,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        self.key_DRB = wx.TextCtrl(panel)
        hbox1_DRB.Add(self.key_DRB, proportion=1)
        vbox.Add(hbox1_DRB, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)

        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
        data_lb = wx.StaticText(panel, label='  Stream')
        hbox3.Add(data_lb,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        self.data = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
        self.data.SetMinSize(wx.Size(-1, 180))
        hbox3.Add(self.data, proportion=1,flag=wx.EXPAND|wx.BOTTOM, border=20)
        vbox.Add(hbox3, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=20)

        hbox3_btn = wx.BoxSizer(wx.HORIZONTAL)
        load = wx.Button(panel, label='Load')
        load.Bind(wx.EVT_BUTTON, self.onOpenFile)
        hbox3_btn.Add(load, flag = wx.ALIGN_RIGHT|wx.LEFT, border=52)
        clear = wx.Button(panel, label='Clear')
        clear.Bind(wx.EVT_BUTTON, self.onClearData)
        hbox3_btn.Add(clear,flag=wx.ALIGN_RIGHT|wx.LEFT, border=10)
        vbox.Add(hbox3_btn, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)

        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
        output_lb = wx.StaticText(panel, label='  Output')
        hbox4.Add(output_lb,flag=wx.ALIGN_RIGHT|wx.RIGHT, border=10)
        self.output = wx.TextCtrl(panel, style=wx.TE_MULTILINE|wx.TE_READONLY)
        self.output.SetMinSize(wx.Size(-1, 180))
        hbox4.Add(self.output, proportion=1,flag=wx.EXPAND|wx.BOTTOM, border=20)
        vbox.Add(hbox4, flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=20)

        hbox4_btn = wx.BoxSizer(wx.HORIZONTAL)
        process = wx.Button(panel, label='Process')
        process.Bind(wx.EVT_BUTTON, self.onProcess)
        hbox4_btn.Add(process, flag = wx.ALIGN_RIGHT|wx.LEFT, border=52)
        copy = wx.Button(panel, label='Copy')
        copy.Bind(wx.EVT_BUTTON, self.onCopy)
        hbox4_btn.Add(copy, flag = wx.ALIGN_RIGHT|wx.LEFT, border=10)
        clear = wx.Button(panel, label='Clear All')
        clear.Bind(wx.EVT_BUTTON, self.onClear)
        hbox4_btn.Add(clear,flag=wx.ALIGN_RIGHT|wx.LEFT, border=180)
        vbox.Add(hbox4_btn, proportion=1,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=20)
        panel.SetSizer(vbox)

        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()
        loadMenuItem = fileMenu.Append(wx.NewId(), "Load Config",
                                       "Load Config from file")
        self.Bind(wx.EVT_MENU, self.onLoad, loadMenuItem)
        saveMenuItem = fileMenu.Append(wx.NewId(), "Save Config",
                                       "Save Config to file")
        self.Bind(wx.EVT_MENU, self.onSave, saveMenuItem)
        exitMenuItem = fileMenu.Append(wx.NewId(), "Exit",
                                       "Exit the application")
        self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)
        menuBar.Append(fileMenu, "&File")
        helpMenu = wx.Menu()
        wikiMenuItem = helpMenu.Append(wx.NewId(), "User Guideline", "Find User Guideline")
        menuBar.Append(helpMenu, "&Help")
        self.Bind(wx.EVT_MENU, self.onHelp, wikiMenuItem)
        self.SetMenuBar(menuBar)

    def onSave(self, event):
        wildcard = "All files (*.*)|*.*"
        config0List = ['COUNT', 'BEARER', 'IP_KEY', 'SRB_KEY', 'DRB_KEY']
        config1List = [self.cnt, self.bearer, self.key, self.key_SRB, self.key_DRB]
        dlg = wx.FileDialog(
            self, message="Save file as ...",
            defaultDir=os.curdir,
            defaultFile="", wildcard= wildcard, style=wx.SAVE
            )
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            filehandle=open(os.path.join(self.dirname, self.filename),'wb')
            for config0, config1 in zip(config0List, config1List):
                filehandle.write(config0 + ':' + config1.GetValue())
            filehandle.close()
        dlg.Destroy()

    def onHelp(self, event):
        webbrowser.open('https://twiki.int.net.nokia.com/bin/view/Main/LteKddiEnbLemaBuild', new = 2, autoraise = True)
    def onLoad(self, event):
        wildcard = "All files (*.*)|*.*"
        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultDir= os.getcwd(),
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            try:
                file = open(dlg.GetPath(), 'r')
                configMap = {}

                for line in file:
                    config = line.split(':')
                    configMap[config[0].strip(' ')] = config[1].strip(' ')
                file.close()

                if configMap.has_key('COUNT'):
                    self.cnt.SetValue(configMap.get('COUNT'))
                if configMap.has_key('BEARER'):
                    self.bearer.SetValue(configMap.get('BEARER'))
                if configMap.has_key('IP_KEY'):
                    self.key.SetValue(configMap.get('IP_KEY'))
                if configMap.has_key('SRB_KEY'):
                    self.key_SRB.SetValue(configMap.get('SRB_KEY'))
                if configMap.has_key('DRB_KEY'):
                    self.key_DRB.SetValue(configMap.get('DRB_KEY'))
            except:
                wx.MessageBox("Invalid config!", "Error")
            return
        dlg.Destroy()

    def onProcess(self, event):
        try:
            task = self.task.GetStringSelection()
            direct = self.direction.GetStringSelection()
            if (self.cnt.GetValue()==''):
                wx.MessageBox('Count required!', 'Error')
                return
            else:
                count = int(self.cnt.GetValue(), 16)
            if (self.bearer.GetValue()==''):
                wx.MessageBox('Bearer required!', 'Error')
                return
            else:
                bearer = int(self.bearer.GetValue())
            if (self.data.GetValue()==''):
                wx.MessageBox('Data required!', 'Error')
                return
            else:
                data = self.data.GetValue().replace('0x', '')

            if task == 'IP Check':
                if self.key.GetValue() == '':
                    wx.MessageBox('IP Key required!', 'Error')
                    return
                else:
                    key = self.key.GetValue().replace('0x', '')
                self.output.SetValue(self.cipher.IP(key, count, direct, bearer, data))
            elif task == 'SRB Cipher':
                if self.key_SRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key = self.key_SRB.GetValue().replace('0x', '')
                self.output.SetValue(self.cipher.encrypt(key, count, direct, bearer, data))
            elif task == 'SRB Decipher':
                if self.key_SRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key = self.key_SRB.GetValue().replace('0x', '')
                self.output.SetValue(self.cipher.decrypt(key, count, direct, bearer, data))
            elif task == 'DRB Cipher':
                if self.key_DRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key = self.key_DRB.GetValue().replace('0x', '')
                self.output.SetValue(self.cipher.encrypt(key, count, direct, bearer, data))
            elif task == 'DRB Cipher':
                if self.key_DRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key = self.key_DRB.GetValue().replace('0x', '')
                self.output.SetValue(self.cipher.decrypt(key, count, direct, bearer, data))
            elif task == 'IP + SRB Cipher':
                if self.key.GetValue() == '':
                    wx.MessageBox('IP Key required!', 'Error')
                    return
                else:
                    key_IP = self.key.GetValue().replace('0x', '')
                if self.key_SRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key_RSB = self.key_SRB.GetValue().replace('0x', '')
                self.output.SetValue(self.RSB_Cipher(key_IP, key_RSB, count, direct, bearer, data))
            elif task == 'SRB Decipher + IP':
                if self.key.GetValue() == '':
                    wx.MessageBox('IP Key required!', 'Error')
                    return
                else:
                    key_IP = self.key.GetValue().replace('0x', '')
                if self.key_SRB.GetValue() == '':
                    wx.MessageBox('SRB Key required!', 'Error')
                    return
                else:
                    key_RSB = self.key_SRB.GetValue().replace('0x', '')
                self.output.SetValue(self.RSB_Decipher(key_IP, key_RSB, count, direct, bearer, data))
            else:
                wx.MessageBox('Task invalid!', 'Error')
                return
        except:
            wx.MessageBox("Invalid input!", "Error")
            return

    def RSB_Cipher(self, key_IP, key_RSB, count, direct, bearer, data):
        mac_i = self.cipher.IP(key_IP, count, direct, bearer, data)
        ciphered = self.cipher.encrypt(key_RSB, count, direct, bearer, data[2:]+mac_i)
        return data[:2] + " " + ciphered[:-8] + " " + ciphered[-8:]

    def RSB_Decipher(self, key_IP, key_RSB, count, direct, bearer, data):
        deciphered = self.cipher.decrypt(key_RSB, count, direct, bearer, data[2:])
        ip = self.cipher.IP(key_IP, count, direct, bearer, data[:2]+deciphered[:-8])
        if ip == deciphered[-8:]:
            ip_check = 'pass'
        else:
            ip_check = 'fail'
        return data[:2] + "  " + deciphered[:-8] + " " + deciphered[-8:] + " " + ip_check

    def onClear(self, event):
        for ctrlText in [self.key, self.cnt, self.bearer, self.data, self.output]:
            ctrlText.SetValue('')

    def onClearData(self, event):
        self.data.SetValue('')

    def onOpenFile(self, event):
        wildcard = "All files (*.*)|*.*"
        dlg = wx.FileDialog(
            self, message="Choose a file",
            defaultDir= os.getcwd(),
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.CHANGE_DIR
            )
        if dlg.ShowModal() == wx.ID_OK:
            file = open(dlg.GetPath(), 'r')
            self.data.SetValue(file.read())
            file.close()
        dlg.Destroy()

    def onCopy(self, event):
        """"""
        self.dataObj = wx.TextDataObject()
        self.dataObj.SetText(self.output.GetValue())
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(self.dataObj)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox("Unable to open the clipboard", "Error")