php代码怎样加密?

rd_software 2002-01-26 10:25:06
微软的asp等一些脚本语言都可以加密,且在用IE浏览的时候能自动解析,
php有这方面的功能吗?或则是说有没有其他方法让php加密,但又能让客户使用?
...全文
596 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexxing 2002-01-27
  • 打赏
  • 举报
回复
楼上的好象答非所问,人家问的应该是*.PHP文件本身的加密,而不是数据加密
数据加密一般都支持的

*.PHP文件本身的加密,最好的应该是zend引擎,可惜现在国内的虚拟主机好象没见过有支持的

有个笨点的办法就是自己做处理程序,把程序格式压缩(去掉一切空格、换行和注释),这样程序就很读懂了
再狠一点,统一修改所有的自定义符号,例如变量都改成 $v0001, $v0002 之类,谁看得懂?
cknuke 2002-01-27
  • 打赏
  • 举报
回复
虎翼的主机支持 zend ,给大家一个zend 编译器:
http://51st.51.net/ck/modules.php?name=Downloads&d_op=viewdownload&cid=1
bombshell 2002-01-27
  • 打赏
  • 举报
回复
PHP文件的源码都是明文,这对于某些商业用途来说,并不适合。
因此考虑使用加密的手段保护源码。

实在不耐烦等待zend出编译器,而且编译和加密本质上不是一回
事儿。自己动手、开始修改。

一、基本原理
考虑截获PHP读取源文件的接口。一开始,我考虑从Apache和PHP
之间的接口处 处理,参见apache的src/modules/php4/mod_php4.c
(这个是PHP用static方式编译进apache,make install 后的文件),
在send_php()函数中截获文件指针,采用临时文件的方式,解密后
替换文件指针。这种方 法经过测试实践,证明是可行的。但是,必
须使用两次文件操作,效率低下,而且对于DSO方式不可采用。
由此,重新考虑截获PHP读取文件并装载至缓存的过程,经过费力
的寻找,发现在Zend引擎中 zend-scanner.c是做此处理的。开始对
此文件修改。

二、实现方法示意
采用libmcrypt作为加 密模块,现在采用的是DES方法ECB模式加密,
下面是文件加密的源代码:

/* ecb.c-------------------cut here-----------*/
/* encrypt for php source code version 0.99 beta
we are using libmcrypt to encrypt codes, please
install it first.
compile command line:
gcc -O6 -lmcrypt -lm -o encryptphp ecb.c
please set LD_LIBRARY_PATH before use.
GNU copyleft, designed by wangsu , miweicong */

#define MCRYPT_BACKWARDS_COMPATIBLE 1
#define PHP_CACHESIZE 8192
#include <mcrypt.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


main(int argc, char** argv)
{

int td, i,j,inputfilesize,filelength;
char filename[255];
char password[12];
FILE* ifp;
int readfd;
char *key;
void *block_buffer;
void *file_buffer;
int keysize;
int decode=0;
int realbufsize=0;
struct stat *filestat;


if(argc == 3) {
strcpy(password,argv[1]);
strcpy(filename,argv[2]);
} else if(argc == 4 && !strcmp(argv[1],"-d")){
strcpy(password,argv[2]);
strcpy(filename,argv[3]);
decode=1;
printf("Entering decode mode ... n");
} else {
printf("Usage: encryptphp [-d] password filenamen");
exit(1);
}


keysize=mcrypt_get_key_size(DES);
key=calloc(1, mcrypt_get_key_size(DES));

gen_key_sha1( key, NULL, 0, keysize, password, strlen(password));
td=init_mcrypt_ecb(DES, key, keysize);

if((readfd=open(filename,O_RDONLY,S_IRUSR|S_IWUSR|S_IRGRP))==-1){
printf("FATAL: Can't open file to read");
exit(3);
}

filestat=malloc(sizeof(stat));

fstat(readfd,filestat);
inputfilesize=filestat->st_size;
printf("filesize is %d n",inputfilesize);
filelength=inputfilesize;

inputfilesize=((int)(floor(inputfilesize/PHP_CACHESIZE))+1)*PHP_CACHESIZE;

if((file_buffer=malloc(inputfilesize))==NULL){
printf("FATAL: can't malloc file buffer.n");
exit(2);
}
if((block_buffer=malloc(PHP_CACHESIZE))==NULL){
printf("FATAL: can't malloc encrypt block buffer.n");
exit(2);
}

j=0;
while(realbufsize=read (readfd,block_buffer, PHP_CACHESIZE)){
printf(".");
if(!decode){
if(realbufsize<PHP_CACHESIZE){
for(i=realbufsize;i<PHP_CACHESIZE;i++){
((char *)block_buffer)[i]=' ';
}
}
mcrypt_ecb (td, block_buffer, PHP_CACHESIZE);
} else {
mdecrypt_ecb (td, block_buffer, realbufsize);
}
memcpy(file_buffer+j*PHP_CACHESIZE,block_buffer,PHP_CACHESIZE);
j++;
}

close(readfd);

if((ifp=fopen(filename,"wb"))==NULL){
printf("FATAL: file access error.n");
exit(3);
}
fwrite ( file_buffer, inputfilesize, 1, ifp);

free(block_buffer);
free(file_buffer);
free(filestat);
fclose(ifp);
printf("n");

return 0;

}
/*--- end of ecb.c ------------------------------------*/

因为ECB模式是块长度确定的块加密,这里填充了一 些空字符。

然后,修改php代码中 Zend/zend-scanner.c 如下:
(我的php版本是4.01pl2, SUNsparc/solaris 2.7, gcc 2.95;)

文件前加入:
#define MCRYPT_BACKWARDS_COMPATIBLE 1
#include <mcrypt.h>

然后,注释掉大约3510行前后的YY_INPUT的定义。

然后, 修改大约5150行前后的yy_get_next_buffer()函数:
函数头加上定义:
void *tempbuf;
char *key;
char debugstr[255];
int td,keysize;
int x,y;
FILE *fp;
然后 ,注释掉
YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]),
yy_n_chars, num_to_read );
这一句。
改为:

tempbuf=malloc(num_to_read);
if((yy_n_chars=fread(tempbuf,1,num_to_read,yyin))!=0){
/*decode*/
#define password "PHPphp111222"
#define debug 0

keysize=mcrypt_get_key_size(DES);
key=calloc(1, mcrypt_get_key_size(DES));
gen_key_sha1( key, NULL, 0, keysize, password, strlen(password));
td=init_mcrypt_ecb(DES, key, keysize);
mdecrypt_ecb(td, tempbuf, yy_n_chars);
memcpy((&yy_current_buffer->yy_ch_buf[number_to_move]),tempbuf,yy_n_chars);
if(debug){
fp=fopen("/tmp/logs","wb");
fwrite("nstartn",7,1,fp);
fwrite(tempbuf,1,yy_n_chars,fp);
fwrite("nenditn",7,1,fp);
fclose(fp);
}
}
free(tempbuf);

然后,编译php,按正常方法安装即可,因为我对于libtool不太熟
悉,因此我选择static方式,并在 configure时加入了--with-mcrypt,
这样我就不用自己手工修改Makefile

三、测试及结果
编 译php,apache后,用ecb.c编译出来的encryptphp加密了几个文件,
分别为<1K,10K+,和40K+,在处理 40K大小文件时出错,别的文件均正
常。
这是因为块的ECB加密方式决定了必须使用定长块,所以,请 诸位同好
指点采用何种流加密方式可以兼顾到zend每次读取8192字节的缓存处
理方式。(其他平台上 zend每次读取的块长度可能有所不同)


这只是原理,源码遵从GNU,需要注意,libmcrypt提供的某些加密方式不是free的。
cknuke 2002-01-26
  • 打赏
  • 举报
回复
php有加密函数啊 比如crypt 或者 mcrypt可能要看服务器是否支持了,不过我这找到一个加密函数,你看看吧:
<?php
//$key = "This is supposed to be a secret key !!!"; 估计是密阴
$key="这是一个密阴";
function keyED($txt,$encrypt_key)
{
$encrypt_key = md5($encrypt_key);
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}

function encrypt($txt,$key)
{
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($encrypt_key,$ctr,1) .
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
return keyED($tmp,$key);
}

function decrypt($txt,$key)
{
$txt = keyED($txt,$key);
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}

$string = "需要加密的字符串,如密码等.";

// 开始加密 encrypt $string, and store it in $enc_text
$enc_text = encrypt($string,$key);

// 开始解密 decrypt the encrypted text $enc_text, and store it in $dec_text
$dec_text = decrypt($enc_text,$key);

print "原始字符串 : $string <Br>\n";
print "加密后字串 : $enc_text <Br>\n";
print "解密后字串 : $dec_text <Br>\n";
?>

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧