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

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

本站已经建立2533天!

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