ancora no???

by santoprova3 on July 23rd, 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;
char *skey;
long lunghezzaFile; //variabile grandezza file letto
 
//char initVect[11]="1234567890"; //puntatore vettore di inizializzazione
char *initVect;
 
char myPassword[]="cazzodibuddhaone";
 
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
 
 
 
    if (argc!=2) {
        printf ("Errore: inserire [percorso\\]nomefile");
        return 1;
    }
 
    int a=leggiFile(argv[1]);
 
    if(a!=0)
    {
        printf("Errore generico");
        return 1;
    }
    long x;
    //generazione vettore inizializzazione
    initVect=(char *)malloc(11);
    a= generaInitVect();
 
      //char * skey=malloc(strlen(myPassword)+10+1);
      skey=(char *)malloc(strlen(myPassword)+10+1);
      for (x=0;x<strlen(myPassword);x++)
      {
          skey[x]=myPassword[x];
      }
      for (x=strlen(myPassword);x<strlen(myPassword)+10;x++)
      {
          skey[x]=initVect[x-strlen(myPassword)];
      }
      skey[strlen(myPassword)+10]=(char)0;
 
    //preparazione state array
    int state_array[257];
    for (x=0;x<257;x++){
        state_array[x]=x;
    }
 
    // generazione state array
 
    int aa=0;
    int swap;
    int k;
    int i1;
    for (k=1;k<numIterazioni+1;k++){ //personalizzazione !
        for (x=0;x<256;x++){
             //aa=(aa + state_array[x] + skey[x%strlen(skey)])%256;
             i1=strlen(myPassword)+10;
             aa=(aa + state_array[x] + skey[x % i1]) % 256;
 
 
             swap=state_array[x];
             state_array[x]=state_array[aa];
             state_array[aa]=swap;
 
 
        }
 
    } //next k
 
 
    long idx;
 
    //cifratura !!
 
    int a1=0;
    int b1=0;
    int index;
    int sswap;
        for (idx=0;idx<lunghezzaFile;idx++){
             a1 = (a1 + 1) % 256;
             //printf("a1 di %ld --->%i-",idx,a1);
             b1 = (b1 + state_array[a1]) % 256;
             //printf("b1=%i<- ",b1);
             sswap = state_array[a1];
             //printf("-sswap=%i<- ",state_array[a1]);
             state_array[a1] = state_array[b1];
             state_array[b1] = sswap;
             index = (state_array[a1] + state_array[b1]) % 256;
 
            buffer[idx] = (int)state_array[index] ^ (int)buffer[idx];
            //printf("-state(index=%i)--->%i\n",index,(int)state_array[index]);
        }
 
      //concatenazione vettore di inizializzazione con il buffer cifrato
 
        bufferFinale=(char *)malloc(lunghezzaFile+10+1);
      for (x=0;x<10;x++)
      {
          bufferFinale[x]=initVect[x];
      }
       for (x=10;x<lunghezzaFile+10;x++)
      {
          bufferFinale[x]=(char)buffer[x-10];
      }
 
 
 
 
 
 
 
    // nome del nuovo file ... concatenazione della estensione .copy
 
    int lenStrNomeFile=strlen(argv[1]) + 6;
    char * destFile = malloc(lenStrNomeFile);
    strcpy(destFile,argv[1]);
    strcat(destFile,".copy");
 
    //scrittura file destinazione
    a= scriviFile(destFile);
 
 
 
 
 
    free(buffer); //boo??
    return 0;
}
 
int generaInitVect(){
 
    int x;
    int ran;
    int max=127;
    int min=0;
    srand(time(NULL));
 
    for (x=0;x<10;x++){
        //ran= (255*rand()/(RAND_MAX+1.0));
        ran=min + ( (max-min+1) * rand() / (RAND_MAX+1.0) );
 
        printf("ran estratto = %i-%c ****\n",ran,(char)ran);
        initVect[x]= (char)ran ;
    }
 
        initVect[10]= (char)0 ;
    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);
 
 
 
 
	return 0;
}
 
int scriviFile(char *destFile){
 
 
    FILE * fp;
 
    fp=fopen(destFile,"wb");
    if (!fp) {
    printf ("Errore apertura file per la scrittura ...");
    return 1;
    }
 
    fwrite(bufferFinale, lunghezzaFile+10, 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