Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

ãy viết 1 đoạn mã tự động mã hóa các file illustrator trên máy tính để bảo vệ chúng

  1. User

    ãy viết 1 đoạn mã tự động mã hóa các file illustrator trên máy tính để bảo vệ chúng

  2. Assistant

    Chào bạn,

    Dưới đây là đoạn mã Python sử dụng thư viện cryptography để tự động mã hóa tất cả các file Illustrator (.ai) trên máy tính của bạn để bảo vệ chúng.

    Lưu ý quan trọng:

    • Sao lưu dữ liệu: Trước khi thực hiện, hãy sao lưu các file quan trọng để tránh mất mát dữ liệu.
    • Bảo mật khóa: Khóa mã hóa là duy nhất để giải mã các file. Hãy lưu trữ file khóa (key.key) ở nơi an toàn. Nếu mất khóa này, bạn sẽ không thể giải mã các file đã mã hóa.

    Bước 1: Cài đặt thư viện cần thiết

    Bạn cần cài đặt thư viện cryptography nếu chưa có. Mở terminal hoặc command prompt và chạy:

    pip install cryptography
    

    Bước 2: Đoạn mã Python để mã hóa các file .ai

    import os
    from cryptography.fernet import Fernet
    
    # Hàm tạo và lưu khóa
    def generate_key():
        key = Fernet.generate_key()
        with open("key.key", "wb") as key_file:
            key_file.write(key)
    
    # Hàm tải khóa
    def load_key():
        return open("key.key", "rb").read()
    
    # Hàm mã hóa file
    def encrypt_file(file_path, fernet):
        with open(file_path, "rb") as file:
            file_data = file.read()
        encrypted_data = fernet.encrypt(file_data)
        with open(file_path, "wb") as file:
            file.write(encrypted_data)
        print(f"Đã mã hóa: {file_path}")
    
    # Hàm duyệt và mã hóa các file .ai
    def encrypt_ai_files(directory, fernet):
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(".ai"):
                    file_path = os.path.join(root, file)
                    encrypt_file(file_path, fernet)
    
    if __name__ == "__main__":
        # Kiểm tra xem đã có khóa chưa, nếu chưa thì tạo mới
        if not os.path.exists("key.key"):
            generate_key()
            print("Đã tạo khóa mã hóa và lưu vào 'key.key'. Hãy lưu trữ khóa này an toàn!")
    
        key = load_key()
        fernet = Fernet(key)
    
        # Thư mục chứa các file .ai cần mã hóa
        directory = "Đường_dẫn_tới_thư_mục_của_bạn"  # Thay thế bằng đường dẫn thực tế
    
        encrypt_ai_files(directory, fernet)
    

    Hướng dẫn sử dụng:

    1. Sao chép đoạn mã trên vào một file Python, ví dụ encrypt_ai_files.py.

    2. Chỉnh sửa đường dẫn trong biến directory để trỏ đến thư mục chứa các file Illustrator của bạn.

    3. Chạy script bằng cách mở terminal/cmd và thực hiện:

      python encrypt_ai_files.py
      
    4. Lần đầu chạy, script sẽ tạo một khóa mã hóa và lưu vào key.key. Hãy lưu trữ file này ở nơi an toàn.

    5. Script sẽ tự động duyệt qua thư mục và mã hóa tất cả các file .ai.


    Để giải mã các file đã mã hóa:

    Thêm các hàm sau vào script để giải mã:

    # Hàm giải mã file
    def decrypt_file(file_path, fernet):
        with open(file_path, "rb") as file:
            encrypted_data = file.read()
        decrypted_data = fernet.decrypt(encrypted_data)
        with open(file_path, "wb") as file:
            file.write(decrypted_data)
        print(f"Đã giải mã: {file_path}")
    
    # Hàm duyệt và giải mã các file .ai
    def decrypt_ai_files(directory, fernet):
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(".ai"):
                    file_path = os.path.join(root, file)
                    decrypt_file(file_path, fernet)
    

    Và chỉnh sửa phần if __name__ == "__main__": như sau:

    if __name__ == "__main__":
        # Kiểm tra khóa
        if not os.path.exists("key.key"):
            print("Không tìm thấy khóa mã hóa. Vui lòng đảm bảo rằng 'key.key' tồn tại.")
            exit()
    
        key = load_key()
        fernet = Fernet(key)
    
        # Thư mục chứa các file .ai
        directory = "Đường_dẫn_tới_thư_mục_của_bạn"  # Thay thế bằng đường dẫn thực tế
    
        # Hỏi người dùng muốn mã hóa hay giải mã
        action = input("Bạn muốn mã hóa hay giải mã các file? (nhập 'encrypt' hoặc 'decrypt'): ").lower()
    
        if action == "encrypt":
            encrypt_ai_files(directory, fernet)
        elif action == "decrypt":
            decrypt_ai_files(directory, fernet)
        else:
            print("Lựa chọn không hợp lệ.")
    

    Chú ý:

    • An toàn dữ liệu: Luôn đảm bảo bạn có bản sao lưu trước khi thực hiện mã hóa hoặc giải mã.
    • Bảo mật khóa mã hóa (key.key): Nếu mất khóa này, bạn sẽ không thể truy cập các file đã mã hóa.
    • Bảo vệ quyền riêng tư: Đảm bảo máy tính của bạn an toàn và không có phần mềm độc hại.

    Hy vọng đoạn mã trên sẽ giúp bạn bảo vệ các file Illustrator một cách hiệu quả!