Share python hide source code

myuyennguyencat

New member
## Cách ẩn mã nguồn trong Python

Mã nguồn thường là thông tin nhạy cảm và bạn có thể muốn che giấu nó khỏi đôi mắt tò mò.Có một vài cách để làm điều này trong Python.

### 1. Sử dụng thuộc tính `__file__`

Thuộc tính `__file__` của mô -đun hoặc hàm Python trả về đường dẫn đến tệp nơi nó được xác định.Bạn có thể sử dụng điều này để tạo một liên kết cứng đến tệp, điều này sẽ làm cho nó xuất hiện như thể mã nguồn được đặt ở một vị trí khác.

Ví dụ: mã sau tạo một liên kết cứng đến tệp `my_module.py` trong thư mục`/tmp`:

`` `Python
Nhập hệ điều hành

Os.Link (__ File__, '/tmp/my_module.py')
`` `

Bây giờ, nếu bạn cố gắng mở tệp `my_module.py` trong trình soạn thảo văn bản, bạn sẽ được trình bày với nội dung của liên kết cứng, sẽ không chứa mã nguồn.

### 2. Sử dụng thuộc tính `__loader__`

Thuộc tính `__loader__` của mô -đun hoặc hàm Python trả về đối tượng đã tải nó.Bạn có thể sử dụng điều này để có được tham chiếu đến trình thông dịch Python, sau đó bạn có thể sử dụng để ẩn mã nguồn.

Ví dụ: mã sau sử dụng thuộc tính `__loader__` để có được tham chiếu đến trình thông dịch Python và sau đó sử dụng phương thức` getSourceFile () `để đưa đường dẫn đến tệp nguồn:

`` `Python
nhập khẩu sys

Source_file = sys.modules [__ name __] .__ Loader __. GetSourceFile (sys.modules [__ name__])

với Open (Source_File, 'W') là F:
f.write ('')
`` `

Mã này sẽ ghi đè lên tệp nguồn bằng một tệp trống, ẩn hiệu quả mã nguồn.

### 3. Sử dụng mã obfuscator nguồn nguồn

Một mã nguồn obfuscator là một công cụ có thể được sử dụng để che giấu mã nguồn (không thể đọc được).Có một số bộ điều trị mã nguồn khác nhau có sẵn, cả miễn phí và thương mại.

Ví dụ: [chất phân hủy python] (https://www.pythondecompiler.com/) là một công cụ trực tuyến miễn phí có thể được sử dụng để làm xáo trộn mã nguồn Python.

### 4. Sử dụng trình biên dịch

Một cách khác để ẩn mã nguồn là biên dịch nó thành một thực thi nhị phân.Điều này sẽ làm cho mã nguồn không thể truy cập được cho bất kỳ ai không có nhị phân được biên dịch.

Ví dụ: mã sau biên soạn tệp `my_module.py` thành một thực thi nhị phân:

`` `Python
Nhập pyinstaller

pyinstaller.exe ('my_module.py')
`` `

Việc thực thi nhị phân kết quả sẽ không chứa mã nguồn và sẽ chỉ có thể được chạy bởi một người đã cài đặt trình thông dịch Python.

### 5. Sử dụng môi trường ảo

Một môi trường ảo là một cách để tạo ra một môi trường Python riêng biệt được phân lập với phần còn lại của hệ thống của bạn.Điều này có thể hữu ích nếu bạn muốn ẩn mã nguồn dành riêng cho một dự án cụ thể.

Để tạo môi trường ảo, bạn có thể sử dụng lệnh sau:

`` `Python
Python -M Venv Venv
`` `

Điều này sẽ tạo ra một thư mục mới gọi là `venv` trong thư mục hiện tại.Sau đó, bạn có thể kích hoạt môi trường ảo bằng cách chạy lệnh sau:

`` `Python
Nguồn venv/bin/kích hoạt
`` `

Khi môi trường ảo được kích hoạt, bạn có thể cài đặt bất kỳ gói Python nào bạn cần cho dự án của mình.

### Phần kết luận

Đây chỉ là một vài trong số các cách để ẩn mã nguồn trong Python.Cách tốt nhất để ẩn mã nguồn sẽ phụ thuộc vào nhu cầu và yêu cầu cụ thể của bạn.

## hashtags

* #Python
* #mã nguồn
* #OBFuscation
* #bảo vệ
* #Sự riêng tư
=======================================
## How to hide source code in Python

Source code is often sensitive information, and you may want to hide it from prying eyes. There are a few ways to do this in Python.

### 1. Use the `__file__` attribute

The `__file__` attribute of a Python module or function returns the path to the file where it is defined. You can use this to create a hard link to the file, which will make it appear as if the source code is located in a different location.

For example, the following code creates a hard link to the `my_module.py` file in the `/tmp` directory:

```python
import os

os.link(__file__, '/tmp/my_module.py')
```

Now, if you try to open the `my_module.py` file in a text editor, you will be presented with the contents of the hard link, which will not contain the source code.

### 2. Use the `__loader__` attribute

The `__loader__` attribute of a Python module or function returns the object that loaded it. You can use this to get a reference to the Python interpreter, which you can then use to hide the source code.

For example, the following code uses the `__loader__` attribute to get a reference to the Python interpreter, and then uses the `getsourcefile()` method to get the path to the source file:

```python
import sys

source_file = sys.modules[__name__].__loader__.getsourcefile(sys.modules[__name__])

with open(source_file, 'w') as f:
f.write('')
```

This code will overwrite the source file with an empty file, effectively hiding the source code.

### 3. Use a source code obfuscator

A source code obfuscator is a tool that can be used to obfuscate (make unreadable) source code. There are a number of different source code obfuscators available, both free and commercial.

For example, the [Python Decompiler](https://www.pythondecompiler.com/) is a free online tool that can be used to obfuscate Python source code.

### 4. Use a compiler

Another way to hide source code is to compile it into a binary executable. This will make the source code inaccessible to anyone who does not have the compiled binary.

For example, the following code compiles the `my_module.py` file into a binary executable:

```python
import pyinstaller

pyinstaller.exe('my_module.py')
```

The resulting binary executable will not contain the source code, and will only be able to be run by someone who has the Python interpreter installed.

### 5. Use a virtual environment

A virtual environment is a way to create a separate Python environment that is isolated from the rest of your system. This can be useful if you want to hide source code that is specific to a particular project.

To create a virtual environment, you can use the following command:

```python
python -m venv venv
```

This will create a new directory called `venv` in the current directory. You can then activate the virtual environment by running the following command:

```python
source venv/bin/activate
```

Once the virtual environment is activated, you can install any Python packages that you need for your project.

### Conclusion

These are just a few of the ways to hide source code in Python. The best way to hide source code will depend on your specific needs and requirements.

## Hashtags

* #Python
* #SourceCode
* #OBFuscation
* #Security
* #Privacy
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top