def encrypt_mode2(plaintext, start_position): current_disk1 = DISK1 # this disk is fixed but used for on-the-fly index change so it's relatively turning current_disk2 = setstr(DISK2, start_position) # let's turn the disk to the right index ciphertext = str() for char in plaintext: encoded_char = switch_alphabet(current_disk1, current_disk2, char) ciphertext += encoded_char if char in NUMBERS: current_disk2 = setstr(current_disk2, encoded_char) return ciphertext
def decrypt_mode2(plaintext, start_position): current_disk1 = DISK1 current_disk2 = setstr(DISK2, start_position) ciphertext = str() for char in plaintext: decoded_char = switch_alphabet(current_disk2, current_disk1, char) if decoded_char in NUMBERS: current_disk2 = setstr(current_disk2, char) else: ciphertext += decoded_char return ciphertext
def decrypt_mode1(ciphertext, start_position): plaintext = str() current_disk1 = DISK1 current_disk2 = setstr(DISK2, start_position) # let's turn the disk to the right index for char in ciphertext: if char in DISK1: current_disk1 = setstr(DISK1, char) else: decoded_char = switch_alphabet(current_disk2, current_disk1, char) if decoded_char not in NUMBERS: # numbers are discarded during decryption plaintext += decoded_char return plaintext
def encrypt_mode1(plaintext, start_position, change_index=None): # in this mode, at anytime, the position can be changed # when it happens, output the outer disk's character that is aligned with the inner disk's starting position current_disk1 = DISK1 # this disk is fixed but used for on-the-fly index change so it's relatively turning current_disk2 = setstr(DISK2, start_position) # let's turn the disk to the right index ciphertext = str() for i, char in enumerate(plaintext): if char in DISK1: ciphertext += switch_alphabet(current_disk1, current_disk2, char) if change_index and i % change_index == change_index - 1: # time to change position new_position = current_disk1[randrange(len(current_disk2))] ciphertext += new_position current_disk1 = setstr(current_disk1, new_position) return ciphertext
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 hex2bin("0123456789ABCDEF") == "\x01\x23\x45\x67\x89\xAB\xCD\xEF" assert "0123456789ABCDEF" == bin2hex("\x01\x23\x45\x67\x89\xAB\xCD\xEF") assert list(struct.unpack(">16L", ASCII[:64])) == as_words(ASCII[:64], 512, 32, True)
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 hex2bin("0123456789ABCDEF") == "\x01\x23\x45\x67\x89\xAB\xCD\xEF" assert "0123456789ABCDEF" == bin2hex("\x01\x23\x45\x67\x89\xAB\xCD\xEF") assert list(struct.unpack(">16L",