EADST

Encrypt ONNX Model

ONNX model can be encrypted with the following code, which can be compiled with a .so file to improve code safety.

import hashlib
from Crypto import Random
from Crypto.Cipher import AES

def load_graph(path):
    with open(path, 'rb') as f:
        protobuf_byte_str = f.read()
    return protobuf_byte_str


def encrypt_file(raw, _key):
    bs = 32
    key = hashlib.sha256(_key.encode()).digest()
    s = raw
    raw = s + str.encode((bs - len(s) % bs) * chr(bs - len(s) % bs))
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return (iv + cipher.encrypt(raw))


def decrypt_file(enc, _key):
    key = hashlib.sha256(_key.encode()).digest()
    iv = enc[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    s = cipher.decrypt(enc[AES.block_size:])
    return s[:-ord(s[len(s) - 1:])]


def main():
    input_path = 'test.onnx'
    output_path = 'test_encode.onnx'
    _key = 'Password123!'

    # encode
    nodes_binary_str = load_graph(input_path)
    nodes_binary_str = encrypt_file(nodes_binary_str, _key)
    with open(output_path, 'wb') as f:
        f.write(nodes_binary_str)

    # decode
    nodes_binary_str = load_graph(output_path)
    nodes_str_decrypt = decrypt_file(nodes_binary_str, _key)

    session = onnxruntime.InferenceSession(nodes_str_decrypt)
    # ort_inputs = {session.get_inputs()[0].name: onnx_input}
    # outputs = session.run(None, ort_inputs)


if __name__ == "__main__":
    main()
相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
域名 Video 递归学习法 Algorithm Quantization LeetCode Knowledge GIT tar DeepStream CAM Vim YOLO BeautifulSoup Transformers Dataset Sklearn Base64 Qwen OpenCV OpenAI Michelin AI UI GoogLeNet UNIX Heatmap VGG-16 API logger CC Conda 版权 Bert 阿里云 scipy Linux Agent Image2Text Search 飞书 Cloudreve llama.cpp News Bipartite icon 算法题 tqdm Anaconda 图标 torchinfo PDF Domain 多线程 Llama git-lfs Diagram TensorRT Paper RAR Python Food Statistics Mixtral SAM Miniforge Git VSCode transformers 继承 FP16 Numpy CSV TTS 搞笑 CV 强化学习 Jupyter MD5 Quantize Color Tiktoken Card WAN Plate OCR Data Markdown COCO 多进程 SQLite QWEN v2ray Hungarian Input XML Firewall LoRA 顶会 Random Paddle Animate 净利润 Nginx CEIR TSV Clash C++ Pytorch CUDA ChatGPT CTC EXCEL Qwen2 Baidu Plotly NLP Proxy Translation ONNX Ptyhon Qwen2.5 图形思考法 Interview Shortcut Bin VPN FastAPI Tensor NameSilo uWSGI Tracking LLM Gemma BTC 公式 RGB PDB Jetson HuggingFace 报税 Use Vmess XGBoost Google TensorFlow GGML Ubuntu NLTK BF16 腾讯云 Template Distillation 云服务器 Windows Land ModelScope Pickle SQL FP8 Hotel Bitcoin InvalidArgumentError Disk 第一性原理 WebCrawler DeepSeek CLAP 关于博主 v0.dev SVR Password HaggingFace printf GPT4 LLAMA Pandas Excel FlashAttention Docker Freesound LaTeX 证件照 Django Website Permission Attention mmap SPIE 财报 PyCharm Streamlit git 签证 hf Logo Math GPTQ Datetime 音频 PyTorch Magnet diffusers Hilton Web Breakpoint Augmentation Crawler IndexTTS2 FP64 ResNet-50 uwsgi Zip JSON Safetensors Review FP32 Github PIP Pillow Claude
站点统计

本站现有博文322篇,共被浏览791465

本站已经建立2487天!

热门文章
文章归档
回到顶部