def otp(text, key_txt, encdec=-1): """encdec = -1 for decode, 1 for encode text = the text to encode or decode. key = the key (pad) to use""" pad = "" out = "" c = "" key = key_txt.upper() for x in key: if x in alphabet(upper=True): pad += x for x in text: uc = " " if x in alphabet(upper=True): uc = "A" if x in alphabet(): uc = "a" if uc != " ": if len(pad) == 0: pad = "AAAAAAAA" c = ord(x) - ord(uc[0]) + encdec * (ord(pad[0]) - ord("A")) c = (c + 26) % 26 c = chr(ord(uc[0]) + c) pad = pad[1:len(pad)] out += c return out
def bifid_key(letters): AB = letters.upper().replace("J", "I").replace(" ", "") + alphabet(True).replace("J", "") _(AB) ret = "" for l in AB: if l in ret: continue ret += l _(ret) return ret
def tabula_recta(text, key) -> str: """Encodes text based on the provided key according to the Vingenere Cypher""" k = key.strip().upper() txt = text.strip().upper() alpha = list(alphabet(True)) while len(k) < len(txt): k += key.strip().upper() answer = "" for i, x in enumerate(txt): cipher = rotate(alpha, alphabet_loc(x, 0)) loc = alphabet_loc(k[i], 0) answer += cipher[loc] return answer
def tabula_recta_decode(text, key) -> str: """Decodes to original text based on the provided cyphered text and the provided key""" k = key.strip().upper() txt = text.strip().upper() alpha = list(alphabet(True)) while len(k) < len(txt): k += key.strip().upper() answer = "" for i, x in enumerate(txt): pos = alpha.index(k[i]) for idx in range(0, 25): cypher = rotate(alpha, idx) if cypher[pos] == x: answer += alpha[idx] break return answer
#!/usr/bin/env python # -*- coding: utf-8 -*- from ivonet.crypto.key_word_substitution import KeyWordSubstitution from ivonet.string.alphabet import alphabet __doc__ = """ This module does simple substitution on text based an a alphabet and indexing """ DEFAULT_ALPHABET = (" " + alphabet()).upper() def numbers_to_text(number, alpha=DEFAULT_ALPHABET): """ # Numbers 2 text This function translates via number substitution a number to text. This is a very fragile function as it will only work in certain cases. """ return "".join([alpha[int(x)] for x in number]) def text_to_numbers(text, alpha=DEFAULT_ALPHABET): """ # Text 2 numbers Function to translate text to numbers via number substitution. Find the index of the letter in the alphabet to find the number. """ return "".join([str(alpha.index(x)) for x in text.upper()])