Skip to content

Instantly share code, notes, and snippets.

@ibaaj
Created September 19, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibaaj/fee80cc4396d1f92d5a5d93b51de07ad to your computer and use it in GitHub Desktop.
Save ibaaj/fee80cc4396d1f92d5a5d93b51de07ad to your computer and use it in GitHub Desktop.
vig
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#define MAX_ASCII_ALPHABET_LOWERCASE 122
#define MIN_ASCII_ALPHABET_LOWERCASE 97
struct Caractere {
char c;
int index;
float valeur;
};
int cmp(const void *a,const void *b)
{
struct Caractere *a1 = (struct Caractere *)a;
struct Caractere *a2 = (struct Caractere*)b;
if((*a1).valeur>(*a2).valeur)return -1;
else if((*a1).valeur<(*a2).valeur)return 1;
else return 0;
}
char* read_crypt_file (char* filename)
{
char * buffer = 0;
FILE* fp = fopen(filename, "r");
long longueur;
if(fp == NULL){
printf("impossible ouvrir fichier \n");
exit(EXIT_FAILURE);
}
fseek (fp, 0, SEEK_END);
longueur = ftell (fp);
fseek (fp, 0, SEEK_SET);
buffer = malloc (longueur);
if (buffer)
{
fread (buffer, 1, longueur, fp);
}
fclose (fp);
return buffer;
}
struct Caractere* freqTab(char* text){
float tab[26], sum=0.0;
int bufferCharNb,
tailleText,
i,
nbCharAlphabetLowerCase = 0,
stats[26];
struct Caractere* results = malloc(26*sizeof(struct Caractere));
if(results == NULL){
printf("pb alloc mem \n");
exit(1);
}
for(i = 0; i < 26; ++i){
tab[i] = 0.0;
stats[i] = 0;
}
tailleText = strlen(text);
assert(strcmp(text, "") != 0);
for(i = 0; i < tailleText; i++)
{
bufferCharNb = (int) text[i];
if(bufferCharNb < MIN_ASCII_ALPHABET_LOWERCASE ||\
bufferCharNb > MAX_ASCII_ALPHABET_LOWERCASE)
{
continue;
}
bufferCharNb = (int) text[i] - MIN_ASCII_ALPHABET_LOWERCASE;
stats[bufferCharNb]++;
nbCharAlphabetLowerCase++;
}
for(i = 0; i < 26; ++i){
tab[i] = (float) stats[i] / (float) nbCharAlphabetLowerCase;
sum += tab[i];
printf("%d %f\n", i, tab[i]);
}
for(i = 0; i < 26; ++i)
{
results[i].valeur = tab[i];
results[i].index = i;
}
qsort(results,26,sizeof(results[0]),cmp);
for(int i=0;i<26;i++)
printf("%c : %f \n", results[i].index + MIN_ASCII_ALPHABET_LOWERCASE,\
results[i].valeur);
return results;
}
struct Caractere** nfreqTab(char *s, int tailleCle){
int i,j, tailleText = strlen(s);
struct Caractere* results[tailleCle];
float tab[tailleCle][26];
int stats[tailleCle][26];
int bufferCharNb;
int nbCharparCaractereDeCle[tailleCle];
for(i=0; i < tailleCle; ++i){
results[i] = (struct Caractere*) malloc(26*sizeof(struct Caractere));
nbCharparCaractereDeCle[i] = 0;
for(j=0; j < 26; ++j)
{
tab[i][j] = 0.0;
stats[i][j] = 0;
}
}
for(i=0, j = 0; i < tailleText; ++i, j++){
if(j >= tailleCle)
j = 0;
if (j==0) putchar(s[i]);
bufferCharNb = (int) s[i] - MIN_ASCII_ALPHABET_LOWERCASE;
stats[j][bufferCharNb]++;
nbCharparCaractereDeCle[j]++;
}
putchar('\n');
printf("\n\n\n");
for(i=0; i < tailleCle; ++i)
for(j = 0; j < 26; ++j){
tab[i][j] = (float) stats[i][j] / (float) nbCharparCaractereDeCle[i];
}
for(i=0; i < tailleCle; ++i){
for(j = 0; j < 26; ++j){
results[i][j].c = j + MIN_ASCII_ALPHABET_LOWERCASE;
results[i][j].valeur = tab[i][j];
results[i][j].index = j;
}
qsort(results[i],26,sizeof(results[i][0]),cmp);
}
for(i=0; i < tailleCle; ++i){
for(j=0;j<26;j++)
printf("%c : %f - index alpha: %d - le 'e' : %c \n", results[i][j].index + MIN_ASCII_ALPHABET_LOWERCASE,\
results[i][j].valeur, results[i][j].index + MIN_ASCII_ALPHABET_LOWERCASE,\
(results[i][j].index - 4) + MIN_ASCII_ALPHABET_LOWERCASE);
printf("-----------\n");
}
return NULL;
}
int decode(char* cletested, char* chiffre){
char *cle, *text;
int tailleCle, tailleText,
i, j,
bufferCharNb;
char bufferCharCle, bufferCharText;
tailleCle = strlen(cletested);
tailleText = strlen(chiffre);
cle = malloc(sizeof(char)*tailleCle);
text = malloc(sizeof(char)*tailleText);
if(cle == NULL ||\
text == NULL){
printf("erreur d'allocation mémoire ou arg nul?..\n ");
exit(1);
}
strcpy(cle, cletested);
strcpy(text, chiffre);
for(i = 0; i < tailleCle; ++i ){
bufferCharNb = (int) cle[i];
if(bufferCharNb < MIN_ASCII_ALPHABET_LOWERCASE ||\
bufferCharNb > MAX_ASCII_ALPHABET_LOWERCASE)
{
printf("la cle n'est pas un mot écrit en minuscule. \n");
exit(1);
}
}
// i -> parcours le tableau du texte
// j -> parcours la clé
for(int i = 0, j = 0; i < tailleText; i++, j++)
{
// Quand on a dépassé la taille de la clé, on redémarre au départ
if (j >= tailleCle)
{
j = 0;
}
// si le caractère n'est pas un caractère minuscule alphabétique,
// on désincrémente la clé
// on affiche le char et on continue le loop
bufferCharNb = (int) text[i];
if(bufferCharNb < MIN_ASCII_ALPHABET_LOWERCASE ||\
bufferCharNb > MAX_ASCII_ALPHABET_LOWERCASE){
j--;
printf("%c",text[i]);
continue;
}
// sinon on fait le calcul
bufferCharNb = (((int) text[i] - MIN_ASCII_ALPHABET_LOWERCASE)\
- (((int) cle[j]) - MIN_ASCII_ALPHABET_LOWERCASE)\
% 26)\
+ MIN_ASCII_ALPHABET_LOWERCASE;
// et on affiche
printf("%c", bufferCharNb);
}
return NULL;
}
void sansPonctuationOuMAJ(char *p)
{
char *src = p, *dst = p;
while (*src)
{
if(islower((unsigned char)*src)){
*dst++ = *src++;
}
else {
src++;
}
}
*dst = 0;
}
int main() {
char* s = read_crypt_file("./crypt1.txt");
int tailleCleImaginee = 9;
sansPonctuationOuMAJ(s);
printf("%s\n", s);
nfreqTab(s, tailleCleImaginee);
//
//printf("%s", s);
decode("aaemecai",s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment