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

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

本站已经建立2630天!

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