2008年8月4日 星期一

coding DES 遇到的二進位檔問題

今天把 DES encryption and decryption 的程式寫出了來,也依據檔案大小作邊讀邊寫的動作,解決了原先設定固定大小的buffer儲存fgetc()的值,不會再有overflow了,真感動!!

不過,亂try各種類型的測資時,惱人的問題出現了,目前只能讀ASCII file (如: *.c *.bat *.txt ... ) ,對於 執行檔 (如: *.exe) 及 圖檔 (如: *.jpg *.gif ) 都還不能做完整的加解密 , 上網查了一下資料,查到了 fread() 和 fwrite() 這類的function可以做整串流的文字讀取,看來,只好用 fopen("","rb")的方式去讀寫二進位檔了,在停筆準備這週五的考試前,還是先找了資料,發現這支讀圖檔的程式:

FILE *infile,*outfile;
char infilename[50]="Lena512.raw"; // input file name
char outfilename[50]="Lena.raw"; // output file name
unsigned char **Y; // pointer for Y component
int i;
//open input file
if((infile=fopen(infilename,"rb"))==NULL){
printf("Input file %s can't be opened!\n",infilename);
return 0;
}
//dynamically allocate 2D memory for Y component
Y = (unsigned char **)malloc(height * sizeof(unsigned char *));
for(i = 0; i < i =" 0;">fread(Y[i],width,sizeof(unsigned char),infile);
fclose(infile);
// write Y component
outfile=fopen(outfilename,"wb");

沒有留言: