def replaceImg(self, newImgContent, index): """ 替换IMG :param newImgContent: 新IMG文件流 :param index: 替换序号 :return: 替换结果 """ header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() # 计算替换后的位置的差值 if index >= imgCount: return False replaceTargetSize = len(imgContentSplits[index]) size = len(newImgContent) addressMinus = size - replaceTargetSize # 在被替换的img之后的indexContent位置偏移增加 差值 if index < imgCount - 1: for key in list(dictB.keys())[index + 1:]: dictB[key][0] = toByte(dictA[key][0] + addressMinus) dictB[index][1] = toByte(size) imgContentSplits[index] = newImgContent self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return True return False
def addDictImg(self, imgNameContentDict): """ 从IMG字典流内容中添加,可对应 extractImg方法使用 :param imgNameContentDict: IMG字典流词典,[IMG名称, IMG文件流] :return: 新NPK文件流 """ header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() lenOfMotoNpk = len(self.allContent) addCount = len(imgNameContentDict.keys()) addCountFlag = 0 lenOfAddIndexC = 264 * addCount for key, value in dictB.items(): dictB[key][0] = toByte(dictA[key][0] + lenOfAddIndexC) for value in imgNameContentDict.values(): addContentLength = 0 if addCountFlag > 0: for i in range(addCountFlag): addContentLength += len(imgNameContentDict[i][-1]) # print(addContentLength) dictB[imgCount + addCountFlag] = [ toByte(lenOfMotoNpk + lenOfAddIndexC + addContentLength), value[0], value[2] ] if imgCount == 0: dictB[imgCount + addCountFlag][0] = toByte( toInt(dictB[imgCount + addCountFlag][0]) + 32) imgContentSplits.append(value[-1]) addCountFlag += 1 imgCount += addCount self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return self.allContent
def password_(paName, codeType): code = encode_(paName, codeType) passW = [] for i in range(32): a = toInt(code[i * 4:4 + i * 4]) + 2020 b = toInt(code[::-1][i * 4:4 + i * 4]) + 2020 passW += [toByte(a), toByte(b)] return passW
def reCombination(header, imgCount, dictB, imgContentSplits): imgIndexContent = b'' imgContents = b'' # print(header) for value in dictB.values(): # print(value) for element in value: imgIndexContent += element sHA = shaDecode(header + toByte(imgCount) + imgIndexContent) for single in imgContentSplits: imgContents += single npkContent = header + toByte( imgCount) + imgIndexContent + sHA + imgContents return npkContent
def password_(paName, codeType): code = encode_(paName, codeType) passW = [] # tLine = "2448992054080" for i in range(32): a = toInt(code[i * 4:4 + i * 4]) + 2020 b = toInt(code[::-1][i * 4:4 + i * 4]) + 2020 # if len(a) < len(tLine): # a += " " * (len(tLine) - len(a)) # if len(b) < len(tLine): # b += " " * (len(tLine) - len(b)) passW += [toByte(a), toByte(b)] return passW
def addImg(self, newImgContent, imgName): """ 增加一个IMG文件 :param newImgContent: IMG文件流 :param imgName: IMG名称 :return: """ header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() address = len(self.allContent) + 264 # 多加了一个索引的偏移大小 size = len(newImgContent) if imgCount == 0: imgCount += 1 # 这里地址偏移多加了 32字节的SHA验证 dictB = {0: [toByte(address + 32), toByte(size), toByte(imgName)]} imgContentSplits.append(newImgContent) self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return self.allContent if imgCount >= 1: for index in range(imgCount): dictB[index][0] = toByte(dictA[index][0] + 264) dictB[imgCount] = [toByte(address), toByte(size), toByte(imgName)] imgContentSplits.append(newImgContent) imgCount += 1 self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return self.allContent
def encode_(code, codeType): if len(code + passwordData[codeType]) < 256: codeL = list(code + passwordData[codeType]) for i in range(256 - len(code + passwordData[codeType])): codeL.insert(int(random() * len(code + passwordData[codeType])), " ") code = "".join(codeL) return toByte(code)
def login(password, codeType): disk_ = get_disk_ID() enCodeA = b'' enCodeB = b'' for i in range(32): a = toByte(toInt(password[i * 2]) - 2020) b = toByte(toInt(password[i * 2 + 1]) - 2020)[::-1] enCodeA += a enCodeB = b + enCodeB trueCode = toString(enCodeA + enCodeB).replace(' ', '') if disk_ + passwordData[codeType] == trueCode: return True else: print("硬盘码为:\t" + disk_) print("密码不正确已退出") return False
def insertImg(self, newImgContent, imgName, index=-1, insertMode=False): """ 插入IMG :param newImgContent: 插入的IMG文件流 :param imgName: 插入的IMG文件名称 :param index: 插入序号 :param insertMode: 正序倒序标识符 :return: """ header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() # -------从末尾插入且是顺序时,或者NPK为空时,直接使用add方法 if (index == -1 and insertMode is False) or imgCount == 0: self.addImg(newImgContent, imgName) return True imgCount += 1 size = toByte(len(newImgContent)) name = toByte(imgName) # 倒序插入 if insertMode: address = toByte(dictA[index][0] + 264) newDictB = {index: [address, size, name]} for key, value in dictB.items(): if key < index: dictB[key][0] = toByte(dictA[key][0] + 264) newDictB[key] = dictB[key] if key >= index: dictB[key][0] = toByte(dictA[key][0] + 264 + toInt(size)) newDictB[key + 1] = dictB[key] dictB = dictSort(newDictB) imgContentSplits.insert(index + 1, newImgContent) self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return True # 默认顺序插入 # -----确定新插入img的indexContent # -----位置偏移为插入目标 + 264 + 插入目标imgContent的长度 address = toByte(dictA[index][0] + 264 + dictA[index][1]) newDictB = {index + 1: [address, size, name]} for key, value in dictB.items(): if key <= index: dictB[key][0] = toByte(dictA[key][0] + 264) newDictB[key] = dictB[key] elif key > index: # ----------插入位置之后的imgIndex偏移后延 264 + 新插入imgContent的size dictB[key][0] = toByte(dictA[key][0] + 264 + toInt(size)) newDictB[key + 1] = dictB[key] dictB = dictSort(newDictB) imgContentSplits.insert(index + 1, newImgContent) self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return True
def renameImg(self, newName, index=-1, reNameMode=False): """ 修改IMG名称 :param newName: 新名称 :param index: 修改编号 :param reNameMode: 全修改标识符,reNameMode=True时,修改NPK内所有img名为 输入newName + index(img编号) + .img 形式 :return: """ header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() if reNameMode: for key in dictB.keys(): dictB[key][-1] = toByte(newName + str(key) + '.img') self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return True print(dictA[index][-1], "\t更名为\t{}".format(newName)) newName = toByte(newName) dictB[index][-1] = newName self.allContent = self.updateSplitData(header, imgCount, dictB, imgContentSplits) return True
def delImg(self, index=None, delAll=False): """ 删除IMG :param index: 删除编号或列表 :param delAll: 全删除标识符 :return: 删除成功/失败 """ # 全删除模式,则将NPK更新为空NPK if delAll: self.allContent = empty_NPK return True header, imgCount, dictA, dictB, imgContentSplits = self.getSplitData() if index is None: return False if isinstance(index, int): index = [index] if isinstance(index, list): index = list(set(index)) index = [ i_ if i_ > 0 else i_ + self.imgCount for i_ in list(set(index)) if ((0 <= i_ < self.imgCount) or ( i_ < 0 and i_ + self.imgCount >= 0)) ] if len(index) >= imgCount: self.allContent = empty_NPK return True addressIndex = [] for key in dictA.keys(): addressIndex.append(dictA[key][0]) addressIndexBak = addressIndex[:] for element in index: if element < 0: element = imgCount + element addressIndex.remove(dictA[element][0]) for key, value in dictB.items(): if key > element and dictA[element][0] not in addressIndex: dictB[key][0] = toByte( toInt(dictB[key][0]) - dictA[element][1]) if key > element and dictA[element][0] in addressIndex: continue newImgContents = [] newDictB = {} newFlag = 0 for lasTelement in addressIndex: lastIndex = addressIndexBak.index(lasTelement) newImgContents.append(imgContentSplits[lastIndex]) newDictB[newFlag] = dictB[lastIndex] newFlag += 1 imgCount -= len(index) for key, value in newDictB.items(): newDictB[key][0] = toByte( toInt(newDictB[key][0]) - 264 * len(index)) self.allContent = self.updateSplitData(header, imgCount, newDictB, newImgContents) return True return False