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

本站现有博文333篇,共被浏览922328

本站已经建立2628天!

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