def inverse_matrix_2d(matrix, n): a = matrix[0][0] b = matrix[0][1] c = matrix[1][0] d = matrix[1][1] w_1 = inverse_x(a * d - b * c, n) return [[(d * w_1) % n, (-b * w_1) % n], [(-c * w_1) % n, (a * w_1) % n]]
def __init__(self, a, s, alphabet): """ text – повідомлення; crypto – зашифроване повідомлення :param a, :param s: keys for monogram affine cipher :param alphabet: alphabet """ self.a = a self.s = s self.a_1 = inverse_x(a, len(alphabet)) self.s_1 = (-self.a_1 * s) % len(alphabet) self.alphabet = alphabet.lower()
pseudo_list = [x_0] for i in range(n): x_0 = (a * x_0 + c) % m pseudo_list += [x_0] return pseudo_list x_0_6 = [ 2147483769, 2175910232, 4134845115, 1318263442, 1999771405, 769052060, 3994265071 ] # a * 2147483769 + c (mod 2^32) = 2175910232 => a * x_0_6[0] + c (mod 2^32) = x_0_6[1] # a * 2175910232 + c (mod 2^32) = 4134845115 => a * x_0_6[1] + c (mod 2^32) = x_0_6[2] # c (mod 2^32) = x_0_6[1] - a * x_0_6[0] # c (mod 2^32) = x_0_6[2] - a * x_0_6[1] # x_0_6[1] - a * x_0_6[0] = x_0_6[2] - a * x_0_6[1] # a * x_0_6[1] - a * x_0_6[0] = x_0_6[2] - x_0_6[1] # a * (x_0_6[1] - x_0_6[0]) = x_0_6[2] - x_0_6[1] mod_2_32 = 2**32 q1 = (x_0_6[2] - x_0_6[1]) % mod_2_32 q2 = (x_0_6[1] - x_0_6[0]) % mod_2_32 a = (q1 * inverse_x(q2, mod_2_32)) % mod_2_32 # c (mod 2^32) = x_0_6[1] - a * x_0_6[0] => c = x_0_6[1] - a * x_0_6[0] (mod 2^32) c = (x_0_6[1] - (a * x_0_6[0]) % mod_2_32) % mod_2_32 print(gen_pseudo_random(a, c, mod_2_32, x_0_6[0], 7))