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

本站现有博文337篇,共被浏览961393次

本站已经建立2673天!

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