66 폴더 암호화 도구 (Folder Encrypter)
지정된 폴더를 선택하여 암호화하고 잠글 수 있는 소프트웨어를 개발해 봅시다. 올바른 비밀번호를 입력하지 않으면 폴더 내부의 파일을 읽거나 복사하거나 이동할 수 없도록 보호하는 것이 목표입니다.
이 프로젝트는 파일 시스템의 재귀적 접근과 강력한 암호화 알고리즘(AES 등)을 다루는 방법을 익히기에 아주 좋습니다. 특히 폴더 전체를 하나의 안전한 저장소로 만들거나, 개별 파일을 모두 암호화하는 방식을 고민해 보세요.
66.1 주요 개발 포인트
- 재귀적 파일 암호화: 폴더 내부의 모든 하위 폴더와 파일들을 순회하며 암호화합니다.
- 강력한 암호화 알고리즘 (AES-256): 현대적인 암호화 표준을 사용하여 데이터를 안전하게 보호합니다.
- 비밀번호 해싱 및 검증: 사용자의 비밀번호를 솔트(Salt)와 함께 해싱하여 안전하게 저장하고 비교합니다.
- 메타데이터 관리: 암호화된 파일의 원본 이름, 크기, 권한 등을 보관했다가 복호화 시 복구합니다.
- 사용자 인터페이스 (GUI): 폴더를 선택하고 암호를 입력하기 위한 편리한 창을 구현합니다.
66.2 Python 구현 예시 (간단한 폴더 순회 및 암호화 로직 시뮬레이션)
import os
import hashlib
from Crypto.Cipher import AES
def get_key_from_password(password):
"""
사용자의 비밀번호를 SHA-256 해시로 변환하여 암호화 키로 사용합니다.
"""
return hashlib.sha256(password.encode()).digest()
def aes_encrypt(data, key):
"""
AES 알고리즘을 사용하여 데이터를 암호화합니다.
EAX 모드를 사용하여 인증된 암호화를 수행합니다.
"""
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
# nonce, tag, ciphertext를 순서대로 결합하여 반환합니다.
return cipher.nonce + tag + ciphertext
def encrypt_folder(folder_path, password):
"""
폴더 내의 모든 파일을 암호화합니다.
"""
print(f"'{folder_path}' 폴더 암호화 중...")
key = get_key_from_password(password)
for root, dirs, files in os.walk(folder_path):
for file in files:
# 이미 암호화된 파일은 건너뜁니다.
if file.endswith(".locked"):
continue
file_path = os.path.join(root, file)
print(f"암호화 중: {file_path}")
try:
with open(file_path, 'rb') as f:
data = f.read()
encrypted_data = aes_encrypt(data, key)
with open(file_path + '.locked', 'wb') as f:
f.write(encrypted_data)
# 원본 파일 삭제 (선택 사항, 여기서는 안전을 위해 유지하거나 주석 처리 가능)
# os.remove(file_path)
except Exception as e:
print(f"오류 발생 ({file_path}): {e}")
print("\n폴더 암호화가 완료되었습니다.")
if __name__ == "__main__":
test_folder = "my_secrets"
# 테스트 폴더와 파일 생성
if not os.path.exists(test_folder):
os.makedirs(test_folder)
with open(os.path.join(test_folder, "test.txt"), "w") as f:
f.write("이것은 비밀 문서입니다.")
my_password = os.environ.get("MY_PASSWORD", "your_password_here")
encrypt_folder(test_folder, my_password)