Überprüfen des Quellcodes
-
Hi zusammen!
Der unten stehende Quellcode ist eine eigene Implementierung des One-Time-Pad Verfahrens auf eine Datei.
Seht ihr im Quellcode Fehler, Anregungen zur Verbesserung bzw Angreifbarkeit des Programms?
(Ob das Passwort durch Viren ausgespäht werden kann, soll nebensächlich betrachtet werden. (kann gerne als Bemerkung angegeben werden)
main.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <math.h> const int MaxIndex=1000; const int SHIFT=256; #include "FuncCrypt.h" #include "FuncEncrypt.h" void main() { time_t t; time(&t); srand((unsigned int)t); int MI=0; char MC[MaxIndex]; printf("choice"); printf("\n"); printf("1 - Crypt"); printf("\n"); printf("2 - Encrypt"); printf("\n"); for(MI=0;MI<=3;++MI) { MI=0; scanf("%s",MC); if(MC[0]=='1') { printf("Crypt\n"); FuncCrypt(); break; } /* if(MC[0]=='1') */ if(MC[0]=='2') { printf("Encrypt\n"); FuncEncrypt(); break; } /* if(MC[0]=='2') */ } /* for(MI=0;MI<=3;++MI) */ getchar(); } /* void main() */
FuncCrypt.h
void FuncCrypt();
FuncEncrypt.h
void FuncEncrypt();
FuncCrypt.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <math.h> extern MaxIndex; extern SHIFT; void FuncCrypt() { FILE *FFileA; FILE *FFileB; FILE *FFileC; char FCA[MaxIndex]; char FCB[]={'c','r','y','p','t','.','t','x','t','\0'}; char FCC[]={'p','a','s','s','w','o','r','d','.','t','x','t','\0'}; int FIA=0; int FIB=0; int FIC=0; printf("Input FileName\n"); scanf("%s", FCA); FFileA = fopen(FCA,"rb"); FFileB = fopen(FCB,"wb"); FFileC = fopen(FCC,"wb"); if(FFileA == NULL) { printf("Input File Error"); } else { if(FFileB == NULL) { printf("Crypt File Error"); } else { if(FFileB == NULL) { printf("Password File Error"); } else { while((FIA = fgetc(FFileA))!=EOF) { FIB = rand() % SHIFT; FIC = FIA+FIB; if(FIC>SHIFT) { FIC = FIC - SHIFT; } fprintf(FFileB,"%c",FIC); fprintf(FFileC,"%i",FIB); fprintf(FFileC,"\r\n"); } /* while((FIA = fgetc(FFileA))!=EOF) */ } /* } else { */ fclose(FFileC); } /* } else { */ fclose(FFileB); } /* } else { */ fclose(FFileA); } /* void FuncCrypt() */
FuncEncrypt.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <math.h> extern MaxIndex; extern SHIFT; void FuncEncrypt() { FILE *FFileD; FILE *FFileE; FILE *FFileF; char FCD[MaxIndex]; char FCE[]={'p','a','s','s','w','o','r','d','.','t','x','t','\0'}; char FCF[]={'e','n','c','r','y','p','t','.','t','x','t','\0'}; int FID=0; int FIE=0; int FIF=0; int FIG=0; printf("Input FileName\n"); scanf("%s", FCD); FFileD = fopen(FCD,"rb"); FFileE = fopen(FCE,"rb"); FFileF = fopen(FCF,"wb"); if(FFileD == NULL) { printf("Input File Error"); } else { if(FFileE == NULL) { printf("Password File Error"); } else { if(FFileF == NULL) { printf("Encrypt File Error"); } else { while((FID = fgetc(FFileD))!=EOF) { for(FIF=0;FIF<=9;++FIF) { FIE = fgetc(FFileE); if(FIE==13) { FID=FID - FIG; if(FID<0) { FID=FID + SHIFT; } fprintf(FFileF,"%c",FID); FIG=0; break; } if(FIE>47 && FIE<58) { FIE = FIE - 48; FIG=FIG*10; FIG=FIG + FIE; } } /* for(FIF=0;FIF<=9;++FIF) */ } /* while((FID = fgetc(FFileD))!=EOF) */ } /* } else { */ fclose(FFileF); } /* } else { */ fclose(FFileE); } /* } else { */ fclose(FFileD); } /* void FuncEncrypt() */
Danke
-
Da das der gleiche Code ist, verweise ich mal auf http://www.c-plusplus.net/forum/326475-full
-
Billy12345 schrieb:
Seht ihr im Quellcode Fehler, Anregungen zur Verbesserung bzw Angreifbarkeit des Programms?
Wo soll man anfangen?
- globale Variablen
- damit einhergehende VLA
- void main
- void-Funktionen ohne Parameter
- Änderung der for-Laufvariable im for-Anweisungsblock
- mehrmaliges fgetc in einer Schleife
- nichtssagende großgeschriebene Variablennamen MI,MC
- hantieren mit \r und 13
- Zeilenendekennung mit 13 kodiert: was machst du mit bei Zeilenende = 10
- kein Überlaufschutz bei scanf
- ...
-
Merke Dir : Wutz mag keine VLA´s !! :-))