def encode(plaintext, key): square = mix_alphabet(key, ALPHABET25) plaintext = plaintext.replace(" ","").upper() #let's split the plaintext in digraph, while inserting X to avoid single letter digraphs i = 0 digraphs = list() while i < len(plaintext) - 1: if i == len(plaintext) - 1: # wrong if last char is X plaintext = insert_string(plaintext, i + 1, 'X') elif i < len(plaintext) - 1 and plaintext[i] == plaintext[i + 1]: plaintext = insert_string(plaintext, i + 1, 'X') digraphs += [plaintext[i:i + 2]] i += 2 ciphertext = str() for digraph in digraphs: #get both chars, their coordinates char1, char2 = list(digraph) row1, column1 = get_coordinates(square, char1) row2, column2 = get_coordinates(square, char2) encoded_row1, encoded_column1 = row1, column1 encoded_row2, encoded_column2 = row2, column2 #apply transformation rules if row1 != row2 and column1 != column2: # making a rectangle - take the other 2 corners, with the same row respectively encoded_row1, encoded_column1 = row1, column2 encoded_row2, encoded_column2 = row2, column1 elif row1 != row2 and column1 == column2: # same column - get the caracter down encoded_row1 = (row1 + 1) % 5 encoded_row2 = (row2 + 1) % 5 elif row1 == row2 and column1 != column2: # same row - get the caracter right encoded_column1 = (column1 + 1) % 5 encoded_column2 = (column2 + 1) % 5 #output resulting chars encoded_char1 = get_char(square, encoded_row1, encoded_column1) encoded_char2 = get_char(square, encoded_row2, encoded_column2) ciphertext += encoded_char1 + encoded_char2 return ciphertext
def encode(plaintext, key): square = mix_alphabet(key, ALPHABET25) plaintext = plaintext.replace(" ", "").upper() #let's split the plaintext in digraph, while inserting X to avoid single letter digraphs i = 0 digraphs = list() while i < len(plaintext) - 1: if i == len(plaintext) - 1: # wrong if last char is X plaintext = insert_string(plaintext, i + 1, 'X') elif i < len(plaintext) - 1 and plaintext[i] == plaintext[i + 1]: plaintext = insert_string(plaintext, i + 1, 'X') digraphs += [plaintext[i:i + 2]] i += 2 ciphertext = str() for digraph in digraphs: #get both chars, their coordinates char1, char2 = list(digraph) row1, column1 = get_coordinates(square, char1) row2, column2 = get_coordinates(square, char2) encoded_row1, encoded_column1 = row1, column1 encoded_row2, encoded_column2 = row2, column2 #apply transformation rules if row1 != row2 and column1 != column2: # making a rectangle - take the other 2 corners, with the same row respectively encoded_row1, encoded_column1 = row1, column2 encoded_row2, encoded_column2 = row2, column1 elif row1 != row2 and column1 == column2: # same column - get the caracter down encoded_row1 = (row1 + 1) % 5 encoded_row2 = (row2 + 1) % 5 elif row1 == row2 and column1 != column2: # same row - get the caracter right encoded_column1 = (column1 + 1) % 5 encoded_column2 = (column2 + 1) % 5 #output resulting chars encoded_char1 = get_char(square, encoded_row1, encoded_column1) encoded_char2 = get_char(square, encoded_row2, encoded_column2) ciphertext += encoded_char1 + encoded_char2 return ciphertext
assert byteswap(0x12345678, 2) == 0x7856 #incorrect use but expected result assert nibbleswap(0x1234, 2) == 0x2143 assert gethyphenstr(r"c:\WINDOWS\system32\drivers\http.sys") == r"c:\WINDOW [...] \http.sys" assert getbinstr(0) == "0" assert getbinstr(8) == "1000" assert countmissing(0, 8) == 0 assert countmissing(3, 8) == 5 assert countmissing(8, 8) == 0 assert insert_string("abcd", 2, "1") == "ab1cd" assert zip([1, 2], [0]) == [(1, 0)] assert zip_extend([1, 2], [0], 0) == [(1, 0), (2, 0)] assert zip_extend([0], [1, 2], 0) == [(1, 0), (2, 0)] assert rorstr("abc") == "cab" assert [rorstr("abc", i) for i in range(4)] == ['abc', 'cab', 'bca', 'abc'] assert setstr("abcde","d") == "deabc" assert list(slice_and_pad("0001" + "0000" + "0", 4)) == ['0001', '0000', '01__'] assert list(slice_and_pad("0001" + "0000" + "00", 4)) == ['0001', '0000', '001_'] assert list(slice_and_pad("0001" + "0000" + "000", 4)) == ['0001', '0000', '0001', 'extr'] assert list(slice_and_pad("0001" + "0000" + "0000", 4)) == ['0001', '0000', '0000', '1___']
assert byteswap(0x12345678, 4) == 0x78563412 assert byteswap(0x12345678, 2) == 0x7856 #incorrect use but expected result assert nibbleswap(0x1234, 2) == 0x2143 assert gethyphenstr( r"c:\WINDOWS\system32\drivers\http.sys") == r"c:\WINDOW [...] \http.sys" assert getbinstr(0) == "0" assert getbinstr(8) == "1000" assert countmissing(0, 8) == 0 assert countmissing(3, 8) == 5 assert countmissing(8, 8) == 0 assert insert_string("abcd", 2, "1") == "ab1cd" assert zip([1, 2], [0]) == [(1, 0)] assert zip_extend([1, 2], [0], 0) == [(1, 0), (2, 0)] assert zip_extend([0], [1, 2], 0) == [(1, 0), (2, 0)] assert rorstr("abc") == "cab" assert [rorstr("abc", i) for i in range(4)] == ['abc', 'cab', 'bca', 'abc'] assert setstr("abcde", "d") == "deabc" assert list(slice_and_pad("0001" + "0000" + "0", 4)) == ['0001', '0000', '01__'] assert list(slice_and_pad("0001" + "0000" + "00", 4)) == ['0001', '0000', '001_'] assert list(slice_and_pad("0001" + "0000" + "000",