C++加密解密php代碼的辦法。本站提示廣大學習愛好者:(C++加密解密php代碼的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C++加密解密php代碼的辦法正文
本文實例講述了C++加密解密php代碼的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
#include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "string.h" char * key = "abcd"; PHP_FUNCTION(encode){ long key_len = strlen(key); char * code, * encode_code; long code_len; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &code, &code_len) == FAILURE){ return; } encode_code = encode(code, code_len, key, key_len); RETURN_STRING(encode_code, 0); } PHP_FUNCTION(decode){ long key_len = strlen(key); char * code, * decode_code; long code_len; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &code, &code_len) == FAILURE){ return; } decode_code = decode(code, code_len, key, key_len); RETURN_STRING(decode_code, 0); } PHP_FUNCTION(run){ char * en_base64_code; long en_base64_code_len; char * decode_code; long key_len = strlen(key); char * eval_code; char * str_name; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &en_base64_code, &en_base64_code_len) == FAILURE){ return; } str_name = zend_make_compiled_string_description("phpencoder" TSRMLS_CC); decode_code = decode(en_base64_code, en_base64_code_len, key, key_len); //解碼 spprintf(&eval_code, 0, " ?>%s<?php ", decode_code); free(decode_code); if(zend_eval_string(eval_code, NULL, str_name TSRMLS_CC) == FAILURE){ //解析掉敗 efree(str_name); efree(eval_code); php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Please make sure '<?php' end with '?>'", PHP_EOL); RETURN_FALSE; } efree(str_name); efree(eval_code); RETURN_TRUE; } inline char * encode(char * code, long code_len, char* key, long key_len){ char * code_encode; int i; long offset = 0, ret_len; code_encode = strdup(code); for(i =0; i<code_len; i++){ if(offset == key_len){ offset = 0; } * (code_encode + i) = * (code + i) ^ * (key + offset); offset ++; } return php_base64_encode(code_encode, code_len, &ret_len); //base64 加密 } inline char * decode(char * code,long code_len, char* key, long key_len){ char * code_decode; char * nobase_code; int i; long offset = 0, ret_len; zend_bool strict = 0; code_decode = strdup(code); nobase_code = php_base64_decode_ex((unsigned char*)code, code_len, &ret_len, strict); //解密,ret_len 前往長度 for(i =0; i<ret_len; i++){ if(offset == key_len){ offset = 0; } * (code_decode + i) = * (nobase_code + i) ^ * (key + offset); offset ++; } * (code_decode + i) = '\0'; return code_decode; }
願望本文所述對年夜家的C++法式設計有所贊助。