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

本站现有博文323篇,共被浏览795922

本站已经建立2493天!

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