def encrypt(self, message, key=None): key = key or self.key N = len(key) result = [''] * len(message) for i, char in enumerate(message): result[i] = shift_char( char, key[i % N]) if key[i % N] != '_' else key[i % N] return ''.join(result)
def caesar_cipher_encrypt(input_stream, output_stream, shift, decode=False): if decode: shift *= -1 while True: buffer = input_stream.read(READ_WRITE_BUFFER_SIZE) if not buffer: break output_buffer = ''.join(shift_char(c, shift) for c in buffer) output_stream.write(output_buffer)
def vigenere_cipher_encrypt(input_stream, output_stream, key, decode=False): key_digits = map(lambda x: get_char_num(x), key) if decode: key_digits = list(map(lambda x: -x, key_digits)) j = 0 while True: buffer = input_stream.read(READ_WRITE_BUFFER_SIZE) if not buffer: break output_buffer_chars = [] for c in buffer: shift = key_digits[j % len(key_digits)] output_buffer_chars.append(shift_char(c, shift)) j += 1 output_stream.write(''.join(output_buffer_chars))
def translate_reverse(self, char): cumulative_rotor_rotation = self._window_position + self.ring_setting shift_index = shift_num(alphaord(char), cumulative_rotor_rotation) return shift_char(char, self.reverse_shifts[shift_index])
def translate_forward(self, char): cumulative_rotor_rotation = 0 shift_index = shift_num(alphaord(char), cumulative_rotor_rotation) return shift_char(char, self.forward_shifts[shift_index])
def encrypt_shift(self, text, key=None): ''' C = (P + K) % 26 ''' key = self.key if key is None else key return "".join(shift_char(char, key) for char in text)
def model_similarity_metric(curr_model, base_model, curr_model_shift=0): return sum( model_value_similarity( value, base_model.get(shift_char(char, curr_model_shift), 0)) for char, value in curr_model.items())
def translate(self, char): return shift_char(char, self.shifts[alphaord(char)])