def decode(string): encoded = Str(string[:string.find("11") + 1]) num = generate_numbers(len(encoded)) value = 0 for i in encoded.indexes("1"): value += num[i + 1] return value
def first_sifra(data, key): """first bellaso substitution cipher""" alphabet = "abcdefghilmnopqrstvxyz" # bellaso didn't use a 26 chars alphabet constant_part, rotated_part = alphabet[:11], Str( alphabet[11:]) # and it was split in two parts, used differently pairs = [ alphabet[i:i + 2] for i in range(0, len(alphabet), 2) ] #each pair of letters of the key gets a different substitution alphabet order = "aeiovcgmqsy" # this is the order of pairs, rotation-wise. # now let's generate the substitution alphabet for each pair # at each step (following the right order), the rotated part is rotated left one character alphabets = dict() for index, char in enumerate(order): #let's get the pair by its first char in the order current_pair = [pair for pair in pairs if char in pair][0] alphabets[current_pair] = constant_part + (rotated_part >> index) output = str() for char, current_key in zip(data, key): pair = [p for p in alphabets if current_key in p] if pair: char = substitute(char, alphabets[pair[0]]) output += char return output
def second_sifra(data, key, alphabet_key): #now the substitution alphabet is generated from a passphrase consonants = mix_alphabet(alphabet_key, "bcdfghlmnpqrstxyz") #then we'll work on the vowels, but we'll insert them every 3 character #let's split the consonants string consonants_blocks = split_string_blocks(consonants, 3) #and parse the key for its used vowels vowels = mix_alphabet(alphabet_key, "aeiou") #let's merge the consonants and vowels data alphabet = "".join(i + j for i, j in zip_extend(consonants_blocks, list(vowels))) #assert alphabet == "rmqacntupsbidfgehlxoyz" constant_part, rotated_part = alphabet[:11], Str(alphabet[11:]) #now to generate the pairs, we'll do the same, but merge the vowel every char blocks consonants_blocks = split_string_blocks(consonants, 1) pairsString = Str("".join( i + j for i, j in zip_extend(consonants_blocks, list(vowels)))) pairs = pairsString.splitblock(2) #now we have the pairs, the initial substitution alphabet #let's generate the 'rotated' alphabet for each pair alphabets = dict() for index, pair in enumerate(pairs): #let's get the pair by its first char in the order alphabets[pair] = constant_part + (rotated_part >> index) # now the actual encryption # for this cipher, the xth character of the key is used to decrypt the xth word (space separated) of the plaintext output = str() key_index = 0 for char in data: pair = [p for p in alphabets if key[key_index] in p] if pair: char = substitute(char, alphabets[pair[0]]) if char == ' ': key_index += 1 output += char return output
def second_sifra(data, key, alphabet_key): #now the substitution alphabet is generated from a passphrase consonants = mix_alphabet(alphabet_key, "bcdfghlmnpqrstxyz") #then we'll work on the vowels, but we'll insert them every 3 character #let's split the consonants string consonants_blocks = split_string_blocks(consonants, 3) #and parse the key for its used vowels vowels = mix_alphabet(alphabet_key, "aeiou") #let's merge the consonants and vowels data alphabet = "".join(i + j for i, j in zip_extend(consonants_blocks, list(vowels))) #assert alphabet == "rmqacntupsbidfgehlxoyz" constant_part, rotated_part = alphabet[:11], Str(alphabet[11:]) #now to generate the pairs, we'll do the same, but merge the vowel every char blocks consonants_blocks = split_string_blocks(consonants, 1) pairsString = Str("".join(i + j for i, j in zip_extend(consonants_blocks, list(vowels)))) pairs = pairsString.splitblock(2) #now we have the pairs, the initial substitution alphabet #let's generate the 'rotated' alphabet for each pair alphabets = dict() for index, pair in enumerate(pairs): #let's get the pair by its first char in the order alphabets[pair] = constant_part + (rotated_part >> index) # now the actual encryption # for this cipher, the xth character of the key is used to decrypt the xth word (space separated) of the plaintext output = str() key_index = 0 for char in data: pair = [p for p in alphabets if key[key_index] in p] if pair: char = substitute(char, alphabets[pair[0]]) if char == ' ': key_index += 1 output += char return output
#Kabopan - Readable Algorithms. Public Domain, 2007-2009 from kbp.types import ( \ sub_string, add_string, Str, List, Byte, Dword, Word, Qword, Oword, DQword) assert sub_string("abc", "aBc") == "_B_" assert add_string("abc", "_B_") == "aBc" s = Str("abcdefghij") assert [ s, s << 1, s >> 1, (s << 1) >> 1, s << 2, s << 3, s << 4] == \ ['abcdefghij', 'bcdefghija', 'jabcdefghi', 'abcdefghij', 'cdefghijab', 'defghijabc', 'efghijabcd'] assert s.setstart("d") == "defghijabc" s = Str('abcaba') assert s.indexes("a") == [0, 3, 5] assert s.indexes("d") == [] s = Str("abcdefghij") import pprint assert [s.insert("1", 2), s.insert("12", 2), s.overwrite("1", 2), s.overwrite("12", 2)] == \ ['ab1cdefghij', 'ab12cdefghij', 'ab1defghij', 'ab12efghij'] assert [s.splitblock(2), s.splitblock(3)] == [['ab', 'cd', 'ef', 'gh', 'ij'], ['abc', 'def', 'ghi', 'j']] assert List([1, 2, 3, 4]) >> 1 == [4, 1, 2, 3] assert List([1, 2, 3, 4]) >> 4 == [1, 2, 3, 4] assert List([1, 2, 3, 4]) << 1 == [2, 3, 4, 1]