def encodePermutation(text, iter, round): textSize = len(text) # Рандомное генерирование ключа перестановки if textSize > lengthBlock: permutation = generateIntKey.generateKey(lengthBlock, iter, round) else: permutation = generateIntKey.generateKey(textSize, iter, round) textBlocks = grouper.grouper(text, lengthBlock) countBlocks = math.ceil(textSize / lengthBlock) stringNewText = "" # Собственно сама перестановка count = 0 for i in range(int(countBlocks)): text = list(textBlocks[i]) newText = [] # Массив для нового текста textSize = len(text) # Длина дополненной строки count += textSize for j in range(textSize): try: newText.append(text[permutation[j]]) except IndexError: print("Permutation Encode Index Error") stringNewText += ''.join(newText) return stringNewText
def _get_predict_facts(self, mode=0): ''' Use tag model and group algorithm generate facts. Argument: mode: 1 means evaluate grouping algorithm. 0 means evaluate entire model. ''' self.predict_list = [] if mode == 0: tags = predict(self.test_sentences) elif mode == 1: with open(self.test_sentences_tag_file, 'r', encoding='utf-8') as f: tags = [i.split(',') for i in f.read().split('\n') if i] g = grouper() for sentence, tag in zip(self.test_sentences, tags): tag = formatter(tag) g.sentence = sentence g.tag = tag g.group() r, _ = g.output() self.predict_list.append( [self._data_formatter(fact_) for fact_ in r])
def encodeRol(text, iter, round): # Генерация ключа и запись в файл key = 1 saveKey.saveKey(key, "ROL", round, iter) # Переводим в ASCII asciiText = [ord(c) for c in text] binaryText = [] for i in range(len(asciiText)): binaryTextBlock = int(Double.double(asciiText[i])) while len(str(binaryTextBlock)) != 8: binaryTextBlock = "0" + str(binaryTextBlock) binaryText.append(binaryTextBlock) encodeText = "" for i in binaryText: temp = i temp = shifttext(temp, 1) temp = ''.join(e for e in temp) encodeText += temp encodeText = grouper.grouper(encodeText, lengthBlock) encodeText = ''.join(chr(int(e, 2)) for e in encodeText) return encodeText
def encodeGamma(text, iter, round, alphabet): textLen = len(text) textBlocks = grouper.grouper(text, lengthBlock) print("textBlocks", textBlocks) # Генерация ключа и запись в файл gamma = generateStringKey.generateKey(lengthBlock, iter, round, alphabet) # Переводим в ASCII asciiGamma = [ord(c) for c in gamma] binaryText = [] for textBlock in textBlocks: asciiText = [ord(c) for c in textBlock] for j in range(lengthBlock): binaryTextBlock = int(Double.double(asciiText[j])) binaryGammaBlock = int(Double.double(asciiGamma[j])) print("jj", binaryTextBlock, binaryGammaBlock, int(binaryTextBlock) ^ int(binaryGammaBlock)) binaryText.append(binaryTextBlock ^ binaryGammaBlock) print(binaryText) encodeText = ''.join(chr(int(e, 2)) for e in binaryText) return encodeText
def encodeSubstitution(plaintext, alphabet, iter, round): key = makeKey(alphabet) saveKey.saveKey(key, "Sub", round, iter) keyMap = dict(zip(alphabet, key)) textBlocks = grouper.grouper(plaintext, lengthBlock) encodeText = "" for i in textBlocks: encodeText += ''.join(keyMap.get(c, c) for c in i) return encodeText
def featureStep1(read1): print 'I am here' groups = grouper.grouper(3, read1) for g in groups: readings1 = filter(None, g) #print len(g) print '============================================================' #print readings #print len(readings1) # throw out readings with fewer signals than our desired resolution if len(readings1) == vector_resolution: yield make_feature_vector(readings1)
def decodePermutation(encodeText, key): textSize = len(encodeText) textBlocks = grouper.grouper(encodeText, lengthBlock) countBlocks = math.ceil(len(textBlocks)) stringOldText = "" for i in range(int(countBlocks)): text = list(textBlocks[i]) oldText = [""] * lengthBlock # Массив для нового текста textSize = len(text) # Длина дополненной строки for j in range(textSize): try: oldText.pop(int(key[j])) oldText.insert(int(key[j]), text[j]) except IndexError: print("Permutation Decode Index Error") stringOldText += ''.join(oldText) return stringOldText
def decodeRol(text, key): # Переводим в ASCII asciiText = [ord(c) for c in text] binaryText = [] for i in range(len(asciiText)): binaryTextBlock = int(Double.double(asciiText[i])) while len(str(binaryTextBlock)) != 8: binaryTextBlock = "0" + str(binaryTextBlock) binaryText.append(binaryTextBlock) decodeText = "" for i in binaryText: temp = i # count += len(str(temp)) temp = shifttext(temp, -1) temp = ''.join(e for e in temp) decodeText += temp decodeText = grouper.grouper(decodeText, lengthBlock) decodeText = ''.join(chr(int(e, 2)) for e in decodeText) return decodeText
def feature_vector_generator(subject, t0, t1, sq, sub_path): '''Returns a generator of feature vectors for subject between t0 and t1. All returned vectors are guaranteed to be equal to or above signal quality sq.''' # get all the readings for subject between t0 and t1 #print('function generator begin called') #print t0 #print t1 #print sq readings = querying.readings(subject, t0, t1, sq, sub_path) #print readings # group readings into lists of length `vector_resolution` #print 'will cal grouper now' groups = grouper.grouper(vector_resolution, readings) # print 'grouper calling finished' for g in groups: readings = filter(None, g) #print readings # print len(readings) # throw out readings with fewer signals than our desired resolution if len(readings) == vector_resolution: yield make_feature_vector(readings)
def bintohex(binlist): """ converts binary strings to hex strings""" hexstring = '' #binary to hex code here rhex_bindict = { '0000': '0', "0001": '1', "0010": '2', "0011": '3', "0100": '4', "0101": '5', "0110": '6', "0111": '7', "1000": '8', "1001": '9', "1010": 'a', "1011": 'b', "1100": 'c', "1101": 'd', "1110": 'e', "1111": 'f' } madestring = '' for x in binlist: madestring = madestring + str(x) tup = grouper.grouper(madestring, 4, None) fourchar = '' binlist = [] for x in tup: fourchar = ''.join(x) binlist.append(fourchar) madelist = [] for x in binlist: madelist.append(rhex_bindict[x]) hexstring = ''.join(madelist) return hexstring
def decodeGamma(text, gamma): textLen = len(text) textBlocks = grouper.grouper(text, lengthBlock) print("textBlocks", textBlocks) # Переводим в ASCII asciiGamma = [ord(c) for c in gamma] binaryText = [] for textBlock in textBlocks: asciiText = [ord(c) for c in textBlock] for j in range(lengthBlock): binaryTextBlock = int(Double.double(asciiText[j])) binaryGammaBlock = int(Double.double(asciiGamma[j])) while len(str(binaryTextBlock)) != 8: binaryTextBlock = "0" + str(binaryTextBlock) while len(str(binaryGammaBlock)) != 8: binaryGammaBlock = "0" + str(binaryGammaBlock) binaryText.append(int(binaryTextBlock) ^ int(binaryGammaBlock)) decodeText = ''.join(chr(int(e, 2)) for e in binaryText) return decodeText
def bintobase(number): """ Function to convert binary strings to a Base64 encription""" #binary to base64 code here binstring = number finalsixstring = '' basedict = { 0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z', 26: 'a', 27: 'b', 28: 'c', 29: 'd', 30: 'e', 31: 'f', 32: 'g', 33: 'h', 34: 'i', 35: 'j', 36: 'k', 37: 'l', 38: 'm', 39: 'n', 40: 'o', 41: 'p', 42: 'q', 43: 'r', 44: 's', 45: 't', 46: 'u', 47: 'v', 48: 'w', 49: 'x', 50: 'y', 51: 'z', 52: '0', 53: '1', 54: '2', 55: '3', 56: '4', 57: '5', 58: '6', 59: '7', 60: '8', 61: '9', 62: '+', 63: '/' } tup = grouper.grouper(binstring, 6, 0) sixchar = '' outlist = [] for x in tup: sixchar = ''.join(x) #print sixchar total = 0 count = 5 for y in sixchar: total += int(y) * 2**count count -= 1 #print total # Totals are correct here indexes are found now to implemenet the Base64 dict. outlist.append(basedict[total]) finalsixstring = ''.join(outlist) return finalsixstring
import json from flask import Flask,request,jsonify from flask_script import Manager from grouper import formatter,grouper from predict import predict app=Flask(__name__) app.config['JSON_AS_ASCII']=False manager=Manager(app) g=grouper() @app.route('/') def index(): return '<h1>hello world!</h1>' @app.route('/ie'): def ie(): sentence = request.args.get('sentence') i=get_ie(sentence) if len(i)!=0: return jsonify(i) def get_ie(sentence): tag=predict([sentence]) g.sentence=sentence g.tag=tag[0]