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

本站现有博文336篇,共被浏览943346

本站已经建立2653天!

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