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

本站现有博文339篇,共被浏览991679次

本站已经建立2703天!

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