def decrypt(text, size): """Decrypts text using the Scytale cipher text : string size : int < len(text) """ if type(text) is not str: raise SkytaleError("Can only decrypt strings.") if type(size) is not int: raise SkytaleError("size must be int.") if not size < len(text): raise SkytaleError( "the size of each collumn must be less than the \ length of the text to be encrypted" ) text = utils.fix_text(text) width = int(math.ceil(len(text) / size)) skytale = np.array(["0"] * width * size).reshape(width, size) for i, letter in enumerate(text): skytale[i % width, i / width] = letter skytale.flatten() plain_text = "".join(str(skytale)) plain_text = utils.fix_text(plain_text) return plain_text
def __init__(self, scene, config, name, guns, number): super().__init__(scene, config, "WeaponCluster", number) self.guns = guns self.number_display = [o for o in self.top.childrenRecursive if 'NUMBER' in o][0] self.name_display = [o for o in self.top.childrenRecursive if 'NAME' in o][0] self.number_display.text = str(self.number) self.name_display.text = name utils.fix_text(self.number_display) utils.fix_text(self.name_display)
def __init__(self, scene, conf, guns, number): super().__init__(scene, conf, "WeaponCategory", number) self.guns = guns self.number_display = [o for o in self.top.childrenRecursive if 'NUMBER' in o][0] self.name_display = [o for o in self.top.childrenRecursive if 'NAME' in o][0] self.image_display = [o for o in self.top.childrenRecursive if 'IMAGE' in o][0] self.number_display.text = str(self.number) self.name_display.text = self.config['TEXT'] self.image_display.color = [self.config['IMAGE_OFFSET'], 0, 0, 1] utils.fix_text(self.number_display) utils.fix_text(self.name_display)
def decrypt(text, password): """Decrypts text using the Vigenere cipher D(text[i]) = (text[i] - password[i]) % 26 text, password : string """ password = utils.fix_text(password) length = len(password) A = ord('A') return ''.join([Caesar.decrypt(letter, ord(password[index % length]) - A) for index, letter in enumerate(utils.fix_text(text))])
def decrypt(text, key): """Decrypts text using the One-Time Pad cipher D(x) = Vigenere.decrypt(text, key) text : string key : string """ if type(text) is not str: raise OneTimePadError('Can only encrypt strings.') if type(key) is not str: raise OneTimePadError('key must be a string.') if len(key) < len(text): raise OneTimePadError('key must be at least the same length as text.') return Vigenere.decrypt(utils.fix_text(text), utils.fix_text(key))
def generate_grid(password): """Generates the grid in the form of a dictionary Each letter is translated into a tuple of row and collumn password : string """ if type(password) is not str: raise PlayfairError('Password must be a string.') alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ" grid = dict() rev_grid = dict() password = utils.fix_text(password) i = 0 for letter in password: if letter == 'J': letter = 'I' if letter not in grid: grid[letter] = (i / 5, i % 5) alphabet = alphabet.replace(letter, '') i += 1 for letter in alphabet: grid[letter] = (i / 5, i % 5) i += 1 for key, value in grid.iteritems(): rev_grid[value] = key return grid, rev_grid
def decrypt(text, offset, factor=1): """Decrypts text letter : character """ if type(text) is not str: raise CaesarError('Can only decrypt strings.') offset = offset % 26 if factor % 2 == 0 or factor % 13 == 0: raise CaesarError("factor value must not be divisible by 2 or 13.") factor = factor % 26 factor_inv = utils.modinv(factor, 26) def decrypt_letter(letter): """Decrypts a single letter letter : character """ if not utils.is_letter(letter): raise CaesarError("`letter` must be a single English \ capital letter.") A = ord('A') return chr((factor_inv * (ord(letter) - A - offset)) % 26 + A) return ''.join([decrypt_letter(letter) for letter in utils.fix_text(text)])
def generate_digraphs(text, double_padding='X', end_padding='Z'): """Splits the text into digraphs if a digraph consists of a double letter, double_padding is introduced between them. If at the end of the text we have an odd number of letters, an end_padding is introdiced. text : string double_padding : character end_padding : character """ double_padding = check_padding(double_padding, "double") end_padding = check_padding(end_padding, "end") text = utils.fix_text(text) text = text.replace('J', 'I') counter = 0 while counter < len(text): if counter + 1 == len(text): # we have reached the end of the text_fixed yield text[counter] + end_padding break elif text[counter] != text[counter + 1]: # we just need to create a normal digraph yield text[counter] + text[counter + 1] counter += 2 else: # we have a double letter digraph, so we add the double padding yield text[counter] + double_padding counter += 1
def decrypt(text, password): """Decrypts text using the Vigenere cipher D(text[i]) = (text[i] - password[i]) % 26 text, password : string """ if type(text) is not str: raise VigenereError('Can only decrypt strings.') if type(password) is not str: raise VigenereError('Password must be a string.') password = utils.fix_text(password) length = len(password) A = ord('A') return ''.join([Caesar.decrypt(letter, ord(password[index % length]) - A) for index, letter in enumerate(utils.fix_text(text))])
def decrypt(text, seed): """Decrypts text using the One-Time Pad cipher D(x) = Caesar.decrypt(x, random) text : string seed : hashable """ random.seed(seed) return ''.join(Caesar.decrypt(letter, random.randint(0, 25)) for letter in utils.fix_text(text))
def encrypt(text, size): """Encrypts text using the Scytale cipher text : string size : int < len(text) """ if type(text) is not str: raise SkytaleError('Can only encrypt strings.') if type(size) is not int: raise SkytaleError('size must be int.') if not size < len(text): raise SkytaleError("the size of each collumn must be less than the \ length of the text to be encrypted") text = utils.fix_text(text) width = int(math.ceil(len(text) / float(size))) skytale = np.array(['0'] * width * size).reshape(size, width) for i, letter in enumerate(text): skytale[i % size, i / size] = letter skytale.flatten() cipher_text = ''.join(str(skytale)) cipher_text = utils.fix_text(cipher_text) return cipher_text
def encrypt(text, offset, factor=1): """Encrypts text letter : character """ offset = offset % 26 if factor % 2 == 0 or factor % 13 == 0: raise CaesarError("factor value must not be divisible by 2 or 13.") factor = factor % 26 def encrypt_letter(letter): """Encypts a sinlge letter letter : character """ if not utils.is_letter(letter): raise CaesarError("`letter` must be a single English \ capital letter.") A = ord('A') return chr((factor * (ord(letter) - A) + offset) % 26 + A) return ''.join([encrypt_letter(letter) for letter in utils.fix_text(text)])
def __init__(self, scene, config, gun): super().__init__(scene, config, "WeaponSingle", gun.number) self.log.info(self.M("creating_weapon_item", name=gun.name, number=gun.number)) self.gun = gun self.guns = [gun] self.number_display = [o for o in self.top.childrenRecursive if 'NUMBER' in o][0] self.name_display = [o for o in self.top.childrenRecursive if 'NAME' in o][0] self.ammo_display = [o for o in self.top.childrenRecursive if 'AMMO' in o][0] utils.fix_text(self.number_display) utils.fix_text(self.name_display) utils.fix_text(self.ammo_display) self.number_display.text = str(gun.number) self.name_display.text = str(gun.name) self._ammo_display_pos = self.ammo_display.localPosition.copy()