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

本站现有博文333篇,共被浏览905335

本站已经建立2607天!

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