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

本站现有博文327篇,共被浏览833327

本站已经建立2538天!

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