C++加密解密php代碼的方法
更新時(shí)間:2015年07月29日 18:13:28 作者:mickelfeng
這篇文章主要介紹了C++加密解密php代碼的方法,實(shí)例分析了基于C++實(shí)現(xiàn)加密解密的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C++加密解密php代碼的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
#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++程序設(shè)計(jì)有所幫助。
相關(guān)文章
一篇文章帶你了解C語言文件操作中的幾個(gè)函數(shù)
這篇文章主要介紹了使用C語言操作文件的基本函數(shù)整理,包括創(chuàng)建和打開以及關(guān)閉文件的操作方法,需要的朋友可以參考下,希望能夠給你帶來幫助2021-09-09
DSP中浮點(diǎn)轉(zhuǎn)定點(diǎn)運(yùn)算--浮點(diǎn)數(shù)的存儲格式
本文主要介紹DSP中浮點(diǎn)數(shù)的存儲格式,很值得學(xué)習(xí)一下,需要的朋友可以參考一下。2016-06-06
C語言線性代數(shù)算法實(shí)現(xiàn)矩陣示例代碼
這篇文章主要為大家介紹了使用C語言線性代數(shù)的算法來實(shí)現(xiàn)矩陣示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
C語言中怎么在main函數(shù)開始前執(zhí)行函數(shù)
C語言中怎么在main函數(shù)開始前執(zhí)行函數(shù)呢?下面小編就大家詳細(xì)的介紹一下。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
C語言如何求整數(shù)的位數(shù)及各位數(shù)字之和
這篇文章主要介紹了C語言如何求整數(shù)的位數(shù)及各位數(shù)字之和,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

