上次我把程序发给了另外一个高中同学,他似乎并没有花多少时间就找出来了。但是看他给我的描述,似乎找到不是真正的存放关键数据的内存。还是给廖传政同学修改的差不多,由于要通过画矩形来显示结果,所以必定要提供一个变量来表示真实的数据。如果放在一个函数的局部变量里,由于局部变量是存放在栈中的,只要调用堆栈相同就可以找到。所以放在堆空间更好,但是我却忽略了一点。如下:
-
void *p=malloc(100);
-
free(p);
-
p=malloc(100)
这个式子中,p在free前后的值很多情况下是相同的,这样,即使在堆空间,也可能完全相同。为了防止这种情况,我又使用了随机得到一个堆内存的方法,为了防止栈空间也相同,我同样使用了随机层次的递归,这样一来,我觉得应该会提高破解的难度。更新的代码如下:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <time.h>
-
-
#include <SDL/SDL.h>
-
-
SDL_Surface *screen = NULL;
-
int blood;
-
-
int *encode;
-
int *decode;
-
SDL_Rect *rects;
-
-
int index_of(int array[],size_t size,int value){
-
int index;
-
for(index=0;index<size;index++){
-
if(array[index]==value)
-
return index;
-
}
-
return -1;
-
}
-
-
-
void begin_game(){
-
srand(time(NULL));
-
encode=(int *)malloc(1024*sizeof(int));
-
decode=(int *)malloc(1024*sizeof(int));
-
rects=(SDL_Rect *)malloc(1024*sizeof(SDL_Rect));
-
-
int i,r;
-
for(i=0;i<101;i++){
-
r=rand()%101;
-
if(index_of(encode,i,r)==-1)
-
encode[i]=r;
-
else{
-
i--;
-
continue;
-
}
-
}
-
-
for(i=0;i<101;i++){
-
decode[i]=index_of(encode,101,i);
-
}
-
-
blood=encode[100];
-
}
-
-
void end_game(){
-
free(encode);
-
free(decode);
-
free(rects);
-
}
-
-
void draw (int layer){
-
if(layer>0)
-
return draw(--layer);
-
-
Uint32 color;
-
color=SDL_MapRGB(screen->format,0,0,0);
-
SDL_FillRect (screen, NULL, color);
-
color = SDL_MapRGB (screen->format, 0xFF, 0, 0);
-
-
SDL_Rect *rect=&rects[rand()%1024];
-
rect->w = decode[blood]*5;
-
rect->h = 20;
-
rect->x = 100;
-
rect->y = 100;
-
SDL_FillRect (screen, rect, color);
-
-
SDL_Flip (screen);
-
SDL_Delay (10);
-
}
-
-
-
-
-
int main (int argc, char *argv[]){
-
char *msg;
-
int done;
-
-
SDL_Init (SDL_INIT_VIDEO);
-
atexit (SDL_Quit);
-
-
screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
-
SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
-
begin_game();
-
-
-
done = 0;
-
SDL_Event event;
-
-
while (!done){
-
SDL_WaitEvent (&event);
-
switch (event.type){
-
case SDL_KEYDOWN:{
-
switch(event.key.keysym.sym){
-
case SDLK_UP:blood = decode[blood]+10>100 ? blood : encode[decode[blood]+10];break;
-
case SDLK_DOWN:blood = decode[blood]-10<0 ? blood : encode[decode[blood]-10];break;
-
}
-
}
-
break;
-
case SDL_QUIT:
-
done = 1;
-
break;
-
default:
-
break;
-
}
-
draw (rand()%10);
-
}
-
-
end_game();
-
-
return 0;
-
}
-