파이썬을 이용한 업무용 파일 암호화 및 복호화

이 글에서는 파이썬을 사용하여 업무용 파일을 암호화하고 복호화하는 방법을 알아보겠습니다.

암호화 처리

파이썬에서 암호화를 수행하기 위해 cryptography 라이브러리를 사용할 수 있습니다. 먼저, 해당 라이브러리를 설치해야합니다. 다음 명령을 실행하여 설치할 수 있습니다:

pip install cryptography

암호화를 위해서는 키가 필요합니다. 키는 무작위의 문자열로 생성될 수 있습니다. 다음과 같이 암호화 키를 생성할 수 있습니다:

from cryptography.fernet import Fernet

key = Fernet.generate_key()

이제 생성된 키를 사용하여 파일을 암호화할 수 있습니다. 예를 들어, plaintext.txt 파일을 암호화하여 ciphertext.bin로 저장하려면 다음과 같은 코드를 작성할 수 있습니다:

from cryptography.fernet import Fernet

key = b'<Your Key>'
cipher_suite = Fernet(key)

with open('plaintext.txt', 'rb') as file:
    plaintext = file.read()

cipher_text = cipher_suite.encrypt(plaintext)

with open('ciphertext.bin', 'wb') as file:
    file.write(cipher_text)

복호화 처리

파일을 복호화하기 위해서는 암호화 단계에서 사용한 동일한 키가 필요합니다. 이 키를 사용하여 ciphertext.bin 파일을 복호화하고 원래의 텍스트 파일을 복원할 수 있습니다. 다음과 같이 코드를 작성할 수 있습니다:

from cryptography.fernet import Fernet

key = b'<Your Key>'
cipher_suite = Fernet(key)

with open('ciphertext.bin', 'rb') as file:
    cipher_text = file.read()

plaintext = cipher_suite.decrypt(cipher_text)

with open('plaintext.txt', 'wb') as file:
    file.write(plaintext)

요약

위의 방법을 사용하여 파이썬을 이용하여 업무용 파일을 암호화하고 복호화할 수 있습니다. 이를 통해 중요한 데이터를 안전하게 보호할 수 있습니다.

참고 자료