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

本站现有博文341篇,共被浏览1047791次

本站已经建立2729天!

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