uffffffffffff

by santoprova on July 21st, 2010
No notes
Syntax: C
Show lines - Hide lines - Show in textbox - Download
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
 
 
 
char *buffer; //puntatore al buffer di lettura file
char *bufferFinale;
 
long lunghezzaFile; //variabile grandezza file letto
 
 
char initVect[11]; //puntatore vettore di inizializzazione
 
char myPassword[]="cazzodibuddha";
int numIterazioni=20;
 
int leggiFile(char *nomefile); //prototipo funzione lettura
int scriviFile(char *destFile); //prototipo funzione lettura
int generaInitVect(); //prototipo funzione generazione vettore init 10 caratteri casuali tra 0 e 255 ASCII
 
int main(int argc, char **argv ) // passaggio variabili alla main fun
{
 
    printf("Valore argc: %i\n", argc);
    if (argc!=2) {
        printf ("Errore: inserire [percorso\\]nomefile");
        return 1;
    }
 
    //printf("La stringa passata รจ: %s\n", argv[1]);
 
 
    int a=leggiFile(argv[1]);
 
    if(a!=0)
    {
        printf("Errore generico");
        return 1;
    } else {
        printf("Lunghezza del file letto: %ld\n", lunghezzaFile);
    }
 
 
 
    int x;
    //debug mostra il file letto!
    //printf("Contenuto del buffer:\n");
    //for (x=0;x<=lunghezzaFile;x++){
    //    printf("%c",buffer[x]);
    //}
 
 
 
 
    //generazione vettore inizializzazione
    a= generaInitVect();
 
 
    printf ("\n\n Valore initVect : -->%s<--; \n lunghezza iniVect:%i \n", initVect, strlen(initVect));
 
    //generazione skey = mykey + vettore inizializzazione
    char * skey=malloc(strlen(myPassword)+strlen(initVect)+1);
    strcpy(skey,myPassword);
    strcat(skey,initVect);
    printf ("\n\n Valore skey : -->%s<--; \n lunghezza skey:%i \n", skey, strlen(skey));
 
 
 
    //preparazione
    int state_array[255];
 
    for (x=0;x<256;x++){
        state_array[x]=x;
    }
    //printf ("\n\n Valore state_array[100] : -->%i<-- \n", state_array[100]);
 
 
    // generazione state array
    unsigned int aa=0;
    int swap;
    int k;
 
    for (k=1;k<numIterazioni+1;k++){ //personalizzazione !
        for (x=0;x<256;x++){
             aa=(aa + state_array[x] + skey[x%strlen(skey)])%256;
 
             //printf("%i) aa=%i; state_array[%i]=%i; \n", x, aa, x,state_array[x]);
             swap=state_array[x];
             state_array[x]=state_array[aa];
             state_array[aa]=swap;
        }
 
    } //next k
 
    /*controllo state_array solo per debug
    printf("\nState array\n");
    for (x=0;x<256;x++){
        printf("Statearray[%i] = %i \n", x, state_array[x]);
    }
    */
 
long idx;
//copio buffer in buffer numerico
 /*
 
bufferNumerico=(int *)malloc(lunghezzaFile+1);
for (idx=0;idx<lunghezzaFile+1;idx++){
    bufferNumerico[idx]=buffer[idx];
    }
 */
 
//cifratura !!
 
int a1=0;
int b1=0;
int index;
    for (idx=0;idx<lunghezzaFile;idx++){
         a1 = (a1 + 1) % 256;
         b1 = (b1 + (int)buffer[a1]) % 256;
         swap = buffer[a1];
         state_array[a1] = state_array[b1];
         state_array[b1] = swap;
         index = ((int)buffer[a1] + (int)buffer[b1]) % 256;
 
        buffer[idx] = state_array[index] ^ (int)buffer[idx];
    }
 
 
//concateno il vettore di inizializzazione con il buffer cifrato
 
bufferFinale=(char *)malloc(strlen(buffer)+11); //allocazione mem per il buffer
strcpy(bufferFinale,initVect);
strcat(bufferFinale,buffer);
 
//printf("Lunghezza buffer finale %ld \n", strlen(bufferFinale));
 
 
 
    // nome del nuovo file ... praticamente concateno l'estensione .copy
    // ma possibil eche non c'e' niente di meglio?
    int lenStrNomeFile=strlen(argv[1]) + 6;
 
    //char destFile[lenStrNomeFile];
    char * destFile = malloc(lenStrNomeFile);
 
    strcpy(destFile,argv[1]);
    strcat(destFile,".copy");
    //printf ("lunghezza nuova stringa: %i \n", lenStrNomeFile);
    printf ("\n\n Nome file destinazione: %s \n", destFile);
 
    //scrittura file destinazione
    a= scriviFile(destFile);
 
 
 
 
 
free(buffer); //boo??
return 0;
}
 
int generaInitVect(){
 
    //initVect=malloc(3);
    int x;
    int ran;
    srand(time(NULL));
    for (x=0;x<10;x++){
 
        ran= rand() % 256;
 
 
        initVect[x]=(char) ran ;
 
        printf("ran= %i **%c**\n",x+200, (char)x+200);
    }
 
    return 0;
 
}
 
 
int leggiFile(char *nomefile)
{
	FILE *file; //puntatore
	file = fopen(nomefile, "rb"); //apertura per lettura binaria
	if (!file) { return 2; } //impossibile aprire il file ... non esiste
 
	//determina la lunghezza del file
	fseek(file, 0, SEEK_END);
	lunghezzaFile=ftell(file);
	fseek(file, 0, SEEK_SET);
 
 
 
	buffer=(char *)malloc(lunghezzaFile+1); //allocazione mem per il buffer
 
	if (!buffer)
	{
		fclose(file);
		return 3; //errore allocazione ... memory error?
	}
 
 
	fread(buffer, lunghezzaFile, 1, file); //Lettura file nel buffer
	fclose(file);
 
	printf ("File letto correttamente!\n");
 
 
	return 0;
}
 
 
int scriviFile(char *destFile){
    printf ("\n\n File destinazione passato alla fun di scrittura: %s \n", destFile);
 
    FILE * fp;
 
    fp=fopen(destFile,"wb");
    if (!fp) {
    printf ("Errore apertura file per la scrittura ...");
    return 1;
    }
 
    fwrite(bufferFinale, strlen(bufferFinale), 1, fp);
    //fwrite(buffer, strlen(buffer) , 1, fp);
 
 
  fclose(fp);
 
return 0;
}
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS