onnxruntime(c++)模型加密与解密部署

该示例展示了如何对ONNX模型进行加密和解密操作,以保护模型的安全。首先,代码定义了一个加密函数,使用指定的密钥对模型文件内容进行异或操作实现加密。接着,加密后的模型被保存到一个新文件中。在解密过程中,相同的加密函数用于还原原始模型数据,然后使用ONNX运行时API创建并加载解密后的模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加密onnx模型和解密读取onnx模型示例:

//加密模型
#ifdef WINVER
void encryptDecrypt(const wchar_t* toEncrypt, int strLength, const wchar_t* key, wchar_t* output)
{
        int keyLength = wcslen(key);
        for (int i = 0; i < strLength; i++)
        {
                output[i] = toEncrypt[i] ^ key[i % keyLength];
        }
}
#else
void encryptDecrypt(const char* toEncrypt, int strLength, const char* key, wchar_t* output)
{
        int keyLength = strlen(key);
        for (int i = 0; i < strLength; i++)
        {
                output[i] = toEncrypt[i] ^ key[i % keyLength];
        }
}
#endif



int main()
{
        bool bRet = true;
        std::string model_path = "11.onnx";
        FILE *pModel1File = fopen(model_path.c_str(), "rb");
        if (NULL == pModel1File)
        {
                bRet = false;

        }
        int length = 0;
        fseek(pModel1File, 0, SEEK_END);
        length = ftell(pModel1File);
        fseek(pModel1File, 0, SEEK_SET);
        ORTCHAR_T* model1rbuffer = nullptr;
        model1rbuffer = new ORTCHAR_T[length];
        if (nullptr == model1rbuffer) {
                bRet = false;
        }

        fread((ORTCHAR_T *)model1rbuffer, 1, length, pModel1File);
        fclose(pModel1File);

        //写入加密模型
        FILE *pOutFile = fopen("1.data", "wb");
        if (NULL == pOutFile)
        {
                return -1;
        }
        #ifdef WINVER
        const wchar_t* key = L"1234";
        const wchar_t* encrypted = new wchar_t[length];
        #else
        const char* key = "1234";
        const char* encrypted = new char[length];
        #endif
        encryptDecrypt(model1rbuffer, length, key, encrypted);
        fwrite(encrypted, 1,  length, pOutFile);
        fclose(pOutFile);
        delete[] encrypted;
        delete[] model1rbuffer;
        system("pause");
        return 0;
}


// 解密模型加载
int main() 
{
        bool bRet = true;
        std::string model_path = "1.data";
        FILE *pModel1File = fopen(model_path.c_str(), "rb");
        if (NULL == pModel1File)
        {
                bRet = false;
        }
        int length = 0;
        fseek(pModel1File, 0, SEEK_END);
        length = ftell(pModel1File);
        fseek(pModel1File, 0, SEEK_SET);
        ORTCHAR_T* model1rbuffer = nullptr;
        model1rbuffer = new ORTCHAR_T[length];
        if (nullptr == model1rbuffer) {
                bRet = false;
        }

        fread((ORTCHAR_T *)model1rbuffer, 1, length, pModel1File);
        fclose(pModel1File);
        #ifdef WINVER
        	const wchar_t* key = L"1234";
        	const wchar_t* encrypted = new wchar_t[length];
        #else
        	const char* key = "1234";
        	const char* encrypted = new char[length];
        #endif
        
        encryptDecrypt(model1rbuffer, length, key, decrypted);

        const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
        OrtEnv* env_;
        g_ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "cyril", &env_);
        OrtSessionOptions* session_options_= nullptr;
        OrtSession* session_= nullptr;
        g_ort->CreateSessionOptions(&session_options_);
        g_ort->SetIntraOpNumThreads(session_options_, 1);
        g_ort->SetSessionGraphOptimizationLevel(session_options_, ORT_ENABLE_ALL);
        //g_ort->CreateSession(env_, model1rbuffer, session_options_, &session_);
        g_ort->CreateSessionFromArray(env_, decrypted, length, session_options_, &session_);
        std::cout << "ok!" << std::endl;
        delete[] model1rbuffer;
        delete[] decrypted;
        system("pause");
        return 0;
}

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值