[파이썬] 파일의 디렉토리 내 파일 암호화

File encryption is a crucial aspect of securing sensitive data. Encrypting files ensures that even if someone gains unauthorized access to your files, they won’t be able to understand the contents without the encryption key. In this blog post, we will explore how to encrypt files within a directory using Python.

Choosing a Encryption Algorithm

There are various encryption algorithms available, each with its own level of security and complexity. One popular algorithm is the Advanced Encryption Standard (AES). AES has been widely adopted and is considered secure for most applications.

To encrypt files in a directory, we will be using the pycryptodome library in Python. This library provides a simple interface to perform encryption and decryption operations using AES.

Installing the necessary libraries

To get started, you need to install the pycryptodome library using pip. Open your terminal and run the following command:

pip install pycryptodome

Encrypting files in a directory

Now, let’s dive into the code. We will start by importing the necessary modules:

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import os

Next, we will define a function that takes a file path as input and encrypts the file:

def encrypt_file(file_path, key):
    cipher = AES.new(key, AES.MODE_ECB)
    
    with open(file_path, 'rb') as file:
        plaintext = file.read()
        
    encrypted_data = cipher.encrypt(pad(plaintext, AES.block_size))
    
    with open(file_path, 'wb') as file:
        file.write(encrypted_data)

In this function, we first create an AES cipher object using the provided encryption key. We then read the file contents, pad the plaintext to the appropriate block size, and encrypt the data using the cipher object. Finally, we write the encrypted data back to the file.

To encrypt all the files within a directory, we can iterate over the files using the os.listdir() function and call the encrypt_file() function for each file:

def encrypt_directory(directory_path, key):
    for filename in os.listdir(directory_path):
        file_path = os.path.join(directory_path, filename)
        if os.path.isfile(file_path):
            encrypt_file(file_path, key)

In the above code, we first construct the full path of each file in the directory using os.path.join(). We then check if it is indeed a file using os.path.isfile(), and if so, we call the encrypt_file() function to encrypt the file.

Putting it all together

Now we can encrypt all the files within a directory by calling the encrypt_directory() function with the directory path and encryption key as arguments. For example:

directory_path = "/path/to/directory"
encryption_key = "myencryptionkey"

encrypt_directory(directory_path, encryption_key)

Make sure to replace /path/to/directory with the actual path to the directory you want to encrypt and choose a strong encryption key for encryption_key.

Conclusion

Encrypting files within a directory is an essential step in securing sensitive data. By using the pycryptodome library in Python, we can easily encrypt files using the AES encryption algorithm. In this blog post, we explored how to encrypt files within a directory and discussed the necessary steps and code examples to achieve this. It is essential to follow best practices and ensure the security of encryption keys to maximize the effectiveness of file encryption.

Remember, encryption alone is not enough to guarantee the security of your files. It should be used in combination with other security measures to protect your data effectively.