def bleichenbacher(ciphertext): """ Perform Bleichenbacher attack as described in his paper. """ # Step 1. is only needed when the ciphertext is # not PKCS1 conforming # integer value of ciphertext c = utils.bytes_to_integer(ciphertext) B = 2**(8 * (k - 2)) M = [Interval(2 * B, 3 * B - 1)] # Step 2.A. s = find_smallest_s(ceil(n, 3 * B), c) M = update_intervals(M, s, B) while True: # Step 2.B. if len(M) >= 2: s = find_smallest_s(s + 1, c) # Step 2.C. elif len(M) == 1: a, b = M[0] # Step 4. if a == b: return utils.integer_to_bytes(a % n) s = find_s_in_range(a, b, s, B, c) M = update_intervals(M, s, B)
def decrypt_string(secret_key, ciphertext): enc_integer = utils.bytes_to_integer(ciphertext) integer = decrypt_integer(secret_key, enc_integer) message = utils.integer_to_bytes(integer) return message
def encrypt_string(public_key, message): integer = utils.bytes_to_integer(message) enc_integer = encrypt_integer(public_key, integer) enc_string = utils.integer_to_bytes(enc_integer) return enc_string