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

本站现有博文332篇,共被浏览878587

本站已经建立2585天!

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