Hi there everyone! I've created some functions that decypher the vigenere code from a text file, taking care of the frequencies of each letter and all that boring stuff. Now that I have the key, how can I decrypt some words that they (the teachers xD) give me? It'd be great if someone know something about that subject. If you guys want a piece of the code (til I've done) I'll give to you. Greetings from Spain and thanks in advance :D

Recommended Answers

All 4 Replies

Hey can you post some of the code. I know a bit about cryptology

#define MAX_LON 26

typedef struct{
	char letra; // means 'character' in spanish
	float freq;
}let_f;

typedef struct{
	char letra;
        int alt; // key corresponding to that character
	int freq_abs;
	float freq_rel;
}info;

/* ************************************************************************ */
/* ********************************M A I N********************************* */
/* ************************************************************************ */
int main(){
    int *key, i, lon;
    char *texto;

    let_f * cas;
    info * i_cas[MAX_LON];

    FILE* f;

    //1. Creates a text file in order to print the obtained results and
    //   initializes the alphabet (spanish alphabet, without ñ)
    init_c(cas);
    f = fopen ("results.txt","w");
    lon = 0;

    //2. Reads the encripted text
    texto = lee_string("data/textocifrado.txt");

    //3. We obtain the key
    key = decrypt(texto, cas, i_cas, f, &lon);


/* ************************************************************************ */
/* **********************F U N C T I O N S******************************* */
/* ************************************************************************ */

let_f* init_c(let_f * cas){

    cas =(let_f*)malloc(MAX_LON*sizeof(let_f));

    cas[0].letra='E';
    cas[0].freq=13.68;

    cas[1].letra='A';
    cas[1].freq=12.53;

    cas[2].letra='O';
    cas[2].freq=8.68;

    cas[3].letra='S';
    cas[3].freq=7.98;

    cas[4].letra='R';
    cas[4].freq=6.87;

    cas[5].letra='N';
    cas[5].freq=6.71;

    cas[6].letra='I';
    cas[6].freq=6.25;

    cas[7].letra='D';
    cas[7].freq=5.86;

    cas[8].letra='L';
    cas[8].freq=4.97;

    cas[9].letra='C';
    cas[9].freq=4.68;

    cas[10].letra='T';
    cas[10].freq=4.63;

    cas[11].letra='U';
    cas[12].freq=3.93;

    cas[12].letra='M';
    cas[13].freq=3.15;

    cas[13].letra='P';
    cas[14].freq=2.51;

    cas[14].letra='B';
    cas[15].freq=1.42;

    cas[15].letra='G';
    cas[16].freq=1.01;

    cas[16].letra='V';
    cas[17].freq=0.9;

    cas[17].letra='Y';
    cas[18].freq=0.9;

    cas[18].letra='Q';
    cas[19].freq=0.88;

    cas[19].letra='H';
    cas[20].freq=0.7;

    cas[20].letra='F';
    cas[20].freq=0.69;

    cas[21].letra='Z';
    cas[21].freq=0.52;

    cas[22].letra='J';
    cas[22].freq=0.44;

    cas[23].letra='X';
    cas[23].freq=0.22;

    cas[24].letra='W';
    cas[24].freq=0.02;

    cas[25].letra='K';
    cas[25].freq=0;

    return cas;
}

char* lee_string(char * filename){
    char *texto;

    FILE *f;
    f = fopen(filename, "r");

    int length;
    while (!feof(f)){
      getc(f);
      length++; 
    }
    fclose(f); 

    texto = (char*)malloc(length*sizeof(char));

    f = fopen(filename, "r");
    fscanf(f,"%s", texto);

    fclose(f);

    return texto;
}

int* decrypt(char * texto, let_f * cas, info * i_cas[MAX_LON], FILE* f, int * vec){
    int n = 0;
    int * alter;
    int i, k = 0;

    k = check_length(texto, strlen(texto), f);
    alter = (int*)malloc(k*sizeof(int));

    init_i(i_cas , k);

    n = ana_t(texto, i_cas,k);

    ana_r(i_cas, n,k);

    sort(k, cas, i_cas);
    
    for(i=0;i<k;i++){
        alter[i] = count(i_cas, i);
    }

    *vec = k;

    return alter;
}

// Returns the greatest common divisor
// the numbers 6 and 7 in this functions are there
// because I know the repetitions must be made
// of 6 and 7 length words
int check_length(char * string, int l, FILE * f){
    return mcd(busca_r(string, 6, l, f), busca_r(string, 7, l, f));
}

int busca_r(char * string, int x, int y,FILE * f){
    char aux[x+1];
    int actual = 0, i, j;

    for (i=0;i<(y-x);i++){
        for (j=i+x;j<(y-x);j++){
            if (strncmp((string+i), (string+j), x) == 0){
                strncpy(aux,(string+j), x);
                aux[x]='\0';
                wr_info(i-j, aux, f);
                actual = mcd( actual, i-j);
            }
        }
    }
}

void init_i(info* in[MAX_LON], int k){
    int i,j;
    for (i=0; i<MAX_LON;i++){
        in[i] = (info*)malloc(k*sizeof(info));
    }

    for(i=0;i<MAX_LON;i++){
        for (j=0;j<k;j++){
            in[i][j].letra='a'+i;
            in[i][j].freq_abs=0;
            in[i][j].freq_rel=0.0;
            in[i][j].alt=-1;
        }
    }
}

int ana_t(char * text_xifrat, info * i_cas[MAX_LON], int mida){
    int i,j,est;
    for (i=0;i<mida;i++){
        for (j=0;j<(strlen(text_xifrat)/mida); j++){
            est = search_in_E(i_cas, coord(mida,text_xifrat,j,i),i);
            i_cas[est][i].freq_abs++;
        }
    }

    return j;
}

void ana_r(info** in,int k, int x){
	int i,j;

	for(i=0;i<MAX_LON;i++){
            for(j=0;j<x;j++){
                in[i][j].freq_rel = ((float)(in[i][j].freq_abs) / k);
            }
	}
}

void sort(int k, let_f * i_cas, info** in){
    int i,j,aux;

    for (j=0; j<k; j++)
    {
        for (i=0;i<MAX_LON;i++)
        {
            aux = asig(in ,j);
            /* Realizamos la asignacion de la clave */
            in[k][j].alt = dec(i_cas[i].letra, in[k][j].letra);
        }
    }
}

int count(info** in, int j){
    int vec[MAX_LON];
    int k = 0, i;

    for(i=0;i<MAX_LON;i++){
            vec[i] = 0;
    }

    for(i=0;i<MAX_LON;i++){
        // printf("I: %d  J: %d  ALT: %d\n", i, j, in[i][j].alt);
        if(in[i][j].alt>100000){
            in[i][j].alt = -1;
        }
        
        vec[(in[i][j].alt)] = vec[(in[i][j].alt)] + 1;
    }

    for(i=0;i<MAX_LON;i++){
            if (vec[i] > vec[k]){
                    k = i;
            }
    }

    return k;
}

Yeah I know thats boring but... xD And that's only a part of the code, maybe there are missing 3 ó 4 minor functions. So I obtain the key and I was just wondering how can I "use" this key in a word list (Imagine a matrix where each row has a single word in it).

Make a caeser shift program. Then use this to decode using the key. Do you know how vigeneres are encrypted?

It seems you did not understand me. I already have the key but I dont know how to decrypt the words they give to me. When I execute this program I obtain that the key is equal to 157901912, and the word list is that one:

eocqdrp
eicxa
mldarqmr
soipo
zexdo
nlrzcf
rieml
mzlx

You know? So I have to know what these words really are. Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.