def find_key_encrypt(self):
        self.location = 0
        self.sizeofPad = len(open(self.pad, 'r').read(self.pad_max))

        self.key_len = random.randint(32, 256)
        print '[*] Randomly chosen key source length between 32 and 256:', self.key_len
        
        with open(self.pad, 'r') as f:
            while True:
                f.seek(0)
                f.seek(random.randint(0, self.sizeofPad), 0)
                self.location = f.tell()
                if self.location + self.key_len >= self.sizeofPad:
                    print "[!] File location for key too close to end of file, re-trying key"
                else:
                    break

            f.seek(0)
            f.seek(self.location, 0)
            self.key = f.read(self.key_len)
    
        print '[*] Location of key in file', hex(self.location)

        # Do a sha512 has of the key and trim the front 32 bytes (256 bits)
        #self.key = self.key_line
        key_iterations = self.key_iterations
        
        print "[*] Applying %s sha512 hash iterations before encryption" % key_iterations

        while key_iterations > 1:
            
            self.key = hashlib.sha512(self.key).digest()
            key_iterations -= 1
            #print self.key.encode('hex')

        self.key = hashlib.sha512(self.key).digest()[:32]
        
        print '[*] Encryption Key:', self.key.encode('hex')
        
        self.iv = Random.new().read(AES.block_size)
        
        # Using CFB because we don't have to break it up by blocks or use padding

        cipher = AES.new(self.key, AES.MODE_CFB, self.iv)
        
        self.encrypted_msg = cipher.encrypt(self.payload)
        print '[*] Length of encrypted payload', len(self.encrypted_msg), 'and hash:', hashlib.sha512(self.encrypted_msg).hexdigest()
        self.lookup_table = zlib.compress(struct.pack("<I", self.location) + struct.pack("<H", self.key_len) + self.iv + self.encrypted_msg)

        # Encrypt payload payload
        self.payload_loader = base64.b64encode(zlib.compress(cipher.encrypt(self.payload_loader)))
        
        # Gen go formated AES cipher
        
        if self.output_type.lower() in ['both', 'go']:
            print "[*] Generating encrypted payload for the GO output (padding is different than python)"
            go_block_size = 128

            gocipher = AES.new(
                self.key, 
                AES.MODE_CFB, 
                self.iv,
                # DAMN YOU PYTHON and CFB8
                segment_size=go_block_size
            )

            PADDING="{"
            # Normally you don't have to pad CFB, but go is CFB128 -- python is CFB8
            print "\t[*] Length before padding and encoding:", len(self.payload)
            self.base64_encoded_payload = base64.b64encode(self.payload)
            print "\t[*] Length after encoding:", len(self.base64_encoded_payload)
            self.payload = self.base64_encoded_payload + (go_block_size - len(self.base64_encoded_payload) % go_block_size) * PADDING
            print "\t[*] Length after padding:", len(self.payload)
            self.go_encrypted_msg = gocipher.encrypt(self.payload)
            self.go_lookup_table = zlib.compress(struct.pack("<I", self.location) + struct.pack("<H", self.key_len) + self.iv + self.go_encrypted_msg)
dataset/ETHPy150Open Genetic-Malware/Ebowla/encryption/otp_key.py/otp_key.find_key_encrypt/original