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

本站现有博文321篇,共被浏览774391

本站已经建立2464天!

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