def crypt_modified(inputf_name, key, which_block, which_bit): # Modified crypt for test with plain txt changes # which_block: to toggle `which_bit` in which block allblocks = [] bvfile = BitVector(filename=inputf_name) round_keys = [get_round_key(key, rnum) for rnum in xrange(0, 16)] block = 0 # Keep count of 64 Bit Blocks while (bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(64) if block == which_block: bit_block[which_bit] = bit_block[which_bit] ^ 1 # pad if bit_block size is less than 64 bits bit_block.pad_from_right(64 - len(bit_block)) # Perform 16 rounds for round in xrange(0, 16): bit_block = crypt_round(bit_block, round_keys[round]) LE, RE = bit_block.divide_into_two() bit_block = RE + LE allblocks.append(bit_block) block += 1 bvfile.close_file_object() return allblocks
def crypt_modified(inputf_name, key, which_block, which_bit): # Modified crypt for test with plain txt changes # which_block: to toggle `which_bit` in which block allblocks = [] bvfile = BitVector(filename=inputf_name) round_keys = [get_round_key(key, rnum) for rnum in xrange(0, 16)] block = 0 # Keep count of 64 Bit Blocks while(bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(64) if block == which_block: bit_block[which_bit] = bit_block[which_bit] ^ 1 # pad if bit_block size is less than 64 bits bit_block.pad_from_right(64 - len(bit_block)) # Perform 16 rounds for round in xrange(0, 16): bit_block = crypt_round(bit_block, round_keys[round]) LE, RE = bit_block.divide_into_two() bit_block = RE + LE allblocks.append(bit_block) block += 1 bvfile.close_file_object() return allblocks
def load_bitvectors(data_file, config): n = int(config['num_bits_per_sample']) N = int(config['num_samples']) bv = BitVector(filename=data_file) data = bv.read_bits_from_file(n * N) bv.close_file_object() data = np.array([bit for bit in data], dtype=bool) data = data.reshape((N, n)) samples = [] for sample_idx in range(N): sample = data[sample_idx, :] samples.append(BitVector(bitlist=sample.astype(bool))) return samples
def decrypt(inputf_name, outputf_name): bvfile = BitVector(filename=inputf_name) outf = open(outputf_name, "wb") while(bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(256) assert (len(bit_block) == 256), "Bitblock is not long enough" out = super_power(int(bit_block), d, n, p, q) bit_out = BitVector(intVal=out, size=256) # remove prepended 0s bit_out = bit_out[128:] bit_out.write_to_file(outf) outf.close() bvfile.close_file_object()
def decrypt(inputf_name, outputf_name): bvfile = BitVector(filename=inputf_name) outf = open(outputf_name, "wb") while (bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(256) assert (len(bit_block) == 256), "Bitblock is not long enough" out = super_power(int(bit_block), d, n, p, q) bit_out = BitVector(intVal=out, size=256) # remove prepended 0s bit_out = bit_out[128:] bit_out.write_to_file(outf) outf.close() bvfile.close_file_object()
def crypt(inputf_name, key): allblocks = [] bvfile = BitVector(filename=inputf_name) round_keys = [get_round_key(key, rnum) for rnum in xrange(0, 16)] while (bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(64) # pad if bit_block size is less than 64 bits bit_block.pad_from_right(64 - len(bit_block)) # Perform 16 rounds for round in xrange(0, 16): bit_block = crypt_round(bit_block, round_keys[round]) LE, RE = bit_block.divide_into_two() bit_block = RE + LE allblocks.append(bit_block) bvfile.close_file_object() return allblocks
def crypt(inputf_name, key): allblocks = [] bvfile = BitVector(filename=inputf_name) round_keys = [get_round_key(key, rnum) for rnum in xrange(0, 16)] while(bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(64) # pad if bit_block size is less than 64 bits bit_block.pad_from_right(64 - len(bit_block)) # Perform 16 rounds for round in xrange(0, 16): bit_block = crypt_round(bit_block, round_keys[round]) LE, RE = bit_block.divide_into_two() bit_block = RE + LE allblocks.append(bit_block) bvfile.close_file_object() return allblocks
def encrypt(inputf_name, outputf_name): bvfile = BitVector(filename=inputf_name) outf = open(outputf_name, "wb") while(bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(128) # pad if bit_block size is less than 128 bits if len(bit_block) != 128: b_newline = '00001010' b_string = str(bit_block) + b_newline * ((128 - len(bit_block)) / 8) bit_block = BitVector(bitstring=b_string) assert (len(bit_block) == 128), "Bitvec bitstring error" # prepend with 0s bit_block.pad_from_left(128) out = mod_exp(int(bit_block), e, n) bit_out = BitVector(intVal=out, size=256) bit_out.write_to_file(outf) outf.close() bvfile.close_file_object()
def encrypt(inputf_name, outputf_name): bvfile = BitVector(filename=inputf_name) outf = open(outputf_name, "wb") while (bvfile.more_to_read): bit_block = bvfile.read_bits_from_file(128) # pad if bit_block size is less than 128 bits if len(bit_block) != 128: b_newline = '00001010' b_string = str(bit_block) + b_newline * ( (128 - len(bit_block)) / 8) bit_block = BitVector(bitstring=b_string) assert (len(bit_block) == 128), "Bitvec bitstring error" # prepend with 0s bit_block.pad_from_left(128) out = mod_exp(int(bit_block), e, n) bit_out = BitVector(intVal=out, size=256) bit_out.write_to_file(outf) outf.close() bvfile.close_file_object()
def open_from_file(self, fn): bv = BitVector(filename=fn) print str(self.bit_array.size) bv1 = bv.read_bits_from_file(self.bit_array.size) self.bit_array = self.bit_array | bv1 bv.close_file_object()