So I went ahead an wrote this little program. What it does is it takes a password for a seed and generates a (pseudo-random) bit-pattern using rule 30 from cellular automata. It then XORs our input file with the same-length pattern. In theory this should be secure, given I would start padding the plaintext and only use a password once?
import sys
def rule30(a, b, c):
return (a ^ (b | c)) & 1
def next_generation(current_gen):
padded_gen = [0] + current_gen + [0]
new_gen = [0] * len(padded_gen)
for i in range(1, len(padded_gen) - 1):
new_gen[i] = rule30(padded_gen[i-1], padded_gen[i], padded_gen[i+1])
return new_gen
def file_to_binary(filename):
with open(filename, "rb") as file:
content = file.read()
binary_content = [int(b) for byte in content for b in format(byte, '08b')]
return binary_content
def xor_binary_lists(list1, list2):
return [a ^ b for a, b in zip(list1, list2)]
def binary_to_file(binary_list, filename):
byte_list = [int(''.join(str(b) for b in binary_list[i:i+8]), 2) for i in range(0, len(binary_list), 8)]
with open(filename, "wb") as file:
file.write(bytes(byte_list))
def string_to_binary(seed_string):
binary_str = ''.join(format(ord(c), '08b') for c in seed_string)
return [int(b) for b in binary_str]
def rule30_xor(input_filename, output_filename, password):
input_binary = file_to_binary(input_filename)
input_length = len(input_binary)
seed = string_to_binary(password)
generation = seed
while len(generation) < input_length:
generation = next_generation(generation)
xor_result = xor_binary_lists(input_binary, generation)
binary_to_file(xor_result, output_filename)
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python rule30.py input_file output_file password")
sys.exit(1)
input_filename = sys.argv[1]
output_filename = sys.argv[2]
password = sys.argv[3]
rule30_xor(input_filename, output_filename, password)
Given the following output, can either the password or input file be recovered?
$ hexdump -x output.txt
0000000 411c 1c99 b25d 9bbc e751 c32e 8c28 0162
0000010 8bcc b8eb ce8a d2f8 fec7
What else am I missing from a security standpoint?