C++加密解密php代码的方法
作者:mickelfeng
这篇文章主要介绍了C++加密解密php代码的方法,实例分析了基于C++实现加密解密的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了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++程序设计有所帮助。