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

本站现有博文324篇,共被浏览809228

本站已经建立2512天!

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