def _process_progressive(text, key, method, _reverse=False): """ (De)cypher message to progressive caesar (increasing offset). """ base = ord('A') modulo = ord('Z') - base + 1 if method == PROGRESS_GEOMETRIC: # Basic is a specific case of geometric, with key = 1! delta = key elif method == PROGRESS_SHIFT: delta = key key = 1 if _reverse: s = -1 else: s = 1 if ' ' in text: ret = [] for w in text.split(): ret.append("".join(utils.char_shift(c, base, modulo, delta * s) for c in w)) delta = (delta + key) % modulo return " ".join(ret) else: # Mono-word... ret = [] for c in text: ret.append(utils.char_shift(c, base, modulo, delta * s)) delta = (delta + key) % modulo return "".join(ret)
def _process_progressive(text, key, method, _reverse=False): """ (De)cypher message to progressive caesar (increasing offset). """ base = ord('A') modulo = ord('Z') - base + 1 if method == PROGRESS_GEOMETRIC: # Basic is a specific case of geometric, with key = 1! delta = key elif method == PROGRESS_SHIFT: delta = key key = 1 if _reverse: s = -1 else: s = 1 if ' ' in text: ret = [] for w in text.split(): ret.append("".join( utils.char_shift(c, base, modulo, delta * s) for c in w)) delta = (delta + key) % modulo return " ".join(ret) else: # Mono-word... ret = [] for c in text: ret.append(utils.char_shift(c, base, modulo, delta * s)) delta = (delta + key) % modulo return "".join(ret)
def do_cypher_basic(text, key): """ Cypher message to basic caesar (constant offset). """ # Let’s use a dict here (as it’s a constant one to one mapping). base = ord('A') modulo = ord('Z') - base + 1 _map = {c: utils.char_shift(c, base, modulo, key) for c in set(text)} _map[' '] = ' ' return "".join(_map[c] for c in text)