def encrypt(plaintext_file_name, ciphertext_file_name, exp, n, block_size): cipher_file = open(ciphertext_file_name, "w") # Generate a random one-time pad. pad = generate_pad(block_size) # Convert the one-time pad to a long so that it can be encrypted with RSA. pad_as_long = string_to_long(pad) # Encrypt the one-time pad using RSA. encrypted_pad = modular_exponentiation(pad_as_long, exp, n) # Write the encrypted one-time pad to the ciphertext file, followed by a newline. cipher_file.write(str(encrypted_pad) + "\n") plaintext_file = open(plaintext_file_name, "rb") while True: # Read the next block of the plaintext file. plaintext_block = plaintext_file.read(block_size) if len(plaintext_block) == 0: break # done if no characters left to read else: cipher_file.write(xor_block(pad, plaintext_block)) cipher_file.close() plaintext_file.close()
def decrypt(ciphertext_file_name, decrypted_file_name, exp, n, block_size): cipher_file = open(ciphertext_file_name, "rb") # Read the encrypted one-time pad a character at a time, until hitting the newline. encrypted_pad_text = "" while True: digit = cipher_file.read(1) if digit == "\n": break else: encrypted_pad_text += digit # Convert the encrypted one-time pad to a long int so that it can be decrypted with RSA. encrypted_pad = long(encrypted_pad_text) # Decrypt the one-time pad with RSA. pad_as_long = modular_exponentiation(encrypted_pad, exp, n) # Convert the one-time pad to a string of bytes. pad = long_to_string(pad_as_long, block_size) decrypted_file = open(decrypted_file_name, "w") while True: # Process the next block of the ciphertext. ciphertext_block = cipher_file.read(block_size) if len(ciphertext_block) == 0: break # done when no ciphertext characters remain else: decrypted_file.write(xor_block(pad, ciphertext_block)) cipher_file.close() decrypted_file.close()
def encrypt(plaintext_file_name, ciphertext_file_name, exp, n, block_size): cipher_file = open(ciphertext_file_name, "w") # Generate a random one-time pad. pad = generate_pad(block_size) # Convert the one-time pad to an int so that it can be encrypted with RSA. pad_as_int = str_to_int(pad) # Encrypt the one-time pad using RSA. encrypted_pad = modular_exponentiation(pad_as_int, exp, n) # Write the encrypted one-time pad to the ciphertext file, followed by a newline. cipher_file.write(str(encrypted_pad) + "\n") plaintext_file = open(plaintext_file_name, "rb") while True: # Read the next block of the plaintext file. plaintext_block = plaintext_file.read(block_size) if len(plaintext_block) == 0: break # done if no characters left to read else: # Construct a block of ciphertext. ciphertext_block = "" # XOR each byte in the plaintext block with a byte from the one-time pad, # and concatenate it to the ciphertext block. for i in range(len(plaintext_block)): ciphertext_block += chr(ord(plaintext_block[i]) ^ ord(pad[i])) cipher_file.write(ciphertext_block) cipher_file.close() plaintext_file.close()
def decrypt(ciphertext_file_name, decrypted_file_name, exp, n, block_size): cipher_file = open(ciphertext_file_name, "rb") # Read the encrypted one-time pad a character at a time, until hitting the newline. encrypted_pad_text = "" while True: digit = cipher_file.read(1) if digit == "\n": break else: encrypted_pad_text += digit # Convert the encrypted one-time pad to an int so that it can be decrypted with RSA. encrypted_pad = int(encrypted_pad_text) # Decrypt the one-time pad with RSA. pad_as_int = modular_exponentiation(encrypted_pad, exp, n) # Convert the one-time pad to a string of bytes. pad = int_to_str(pad_as_int, block_size) decrypted_file = open(decrypted_file_name, "w") while True: # Process the next block of the ciphertext. ciphertext_block = cipher_file.read(block_size) if len(ciphertext_block) == 0: break # done when no ciphertext characters remain else: # Construct a block of decrypted plaintext. decrypted_block = "" # XOR each byte in the ciphertext block with a byte from the one-time pad, # and concatenate it to the decrypted plaintext block. for i in range(len(ciphertext_block)): decrypted_block += chr(ord(ciphertext_block[i]) ^ ord(pad[i])) decrypted_file.write(decrypted_block) cipher_file.close() decrypted_file.close()
def fermat_test(m): return modular_exponentiation(2, m-1, m) == 1
def fermat_test(m): return modular_exponentiation(2, m - 1, m) == 1