本文实例讲述了C++加密解密php代码的方法。分享给大家供大家参考。具体实现方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
#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++程序设计有所帮助。