I have a ml model that uses a vectorizer. This vectorizer contains sensitive data and is stored using pickle as a .pkl file. Show How can I encrypt this pkl file, so that it requires a key to decrypt? I tried using the below code for encryption.
When I use the function on a txt file it works. The problem is that the pkl file does not seems to be encrypted, as it can still be used without decryption. asked Sep 19, 2021 at 10:38
DarioDario 515 bronze badges 5 I couldn't get it to properly encrypt/decrypt a pickle file neither. It gave me distorted data on decryption. So I did the next thing, switch libraries. pyAesCrypt does exactly what you want, so you don't have to reinvent the wheel. See this sample code:
output
answered Sep 20, 2021 at 16:17
Edo AkseEdo Akse 3,3132 gold badges9 silver badges18 bronze badges I need the Nội dung chính
What I triedWhen trying with
Then I tried the solution suggested in this question using
I then tried to install the Tool suggested by the error but got same error message after trying again QuestionWhich is the correct way to install UPDATEThere is no need to install asked Jul 29, 2021 at 20:21 Cedric ZoppoloCedric Zoppolo 3,8295 gold badges28 silver badges48 bronze badges 2 Cedric UPDATED answer is right. Pickle exists within Python 3.9. You don't need pip install pickle. Just use it.
answered Aug 19, 2021 at 18:03 1 I found a way that I'm not sure it's the optimal but it works. I did And then in the script just
UPDATEThere is no need to install answered Jul 29, 2021 at 20:21 Cedric ZoppoloCedric Zoppolo 3,8295 gold badges28 silver badges48 bronze badges For Generalized Summary, for almost all Python versions, you never need to worry for installing 'pickle' as it comes already installed with the python interpreter.
In case this doesn't work, refer to Pickle Install Problems on Stack Overflow. For other Queries and Info, refer to Python Pickle Documentation. answered Oct 24, 2021 at 5:02 Not the answer you're looking for? Browse other questions tagged python python-3.x windows pip pickle or ask your own question.Pickle, là một phần của thư viện Python theo mặc định, là một mô-đun quan trọng bất cứ khi nào bạn cần sự bền bỉ giữa các phiên người dùng. Là một mô-đun, pickle cung cấp khả năng lưu các đối tượng Python giữa các quy trình. Cho dù bạn đang lập trình cho cơ sở dữ liệu , trò chơi, diễn đàn hoặc một số ứng dụng khác phải lưu thông tin giữa các phiên, pickle rất hữu ích để lưu số nhận dạng và cài đặt. Mô-đun pickle có thể lưu trữ những thứ như kiểu dữ liệu như boolean, chuỗi và mảng byte, danh sách, từ điển, hàm, v.v. Lưu ý: Khái niệm tẩy rửa còn được gọi là nối tiếp, sắp xếp và làm phẳng. Tuy nhiên, điểm luôn giống nhau — lưu một đối tượng vào tệp để truy xuất sau này. Pickling hoàn thành điều này bằng cách ghi đối tượng dưới dạng một dòng byte dài. Mã ví dụ về dưa chua bằng PythonĐể ghi một đối tượng vào tệp, bạn sử dụng mã theo cú pháp sau: import pickle Đây là cách một ví dụ trong thế giới thực trông như thế nào: import pickle Đoạn mã này ghi nội dung của object_pi vào file_pi của trình xử lý tệp , đến lượt nó được liên kết với tệp filename_pi.obj trong thư mục thực thi. Để khôi phục giá trị của đối tượng vào bộ nhớ, hãy tải đối tượng từ tệp. Giả sử rằng dưa chua chưa được nhập để sử dụng, hãy bắt đầu bằng cách nhập nó: import pickle Đoạn mã sau khôi phục giá trị của số pi: import pickle Đối tượng sau đó đã sẵn sàng để sử dụng một lần nữa, lần này là object_pi2 . Tất nhiên, bạn có thể sử dụng lại các tên ban đầu, nếu bạn thích. Ví dụ này sử dụng các tên riêng biệt để rõ ràng. Những điều cần nhớ về dưa chuaHãy ghi nhớ những điều này khi sử dụng mô-đun dưa chua:
Mẹo: Ngoài ra, hãy tìm hiểu cách sử dụng giá đỡ để lưu các đối tượng trong Python cho một phương pháp khác để duy trì tính liên tục của đối tượng. |