diff --git a/059-bis.c b/059-bis.c new file mode 100644 index 0000000..0ac7a8d --- /dev/null +++ b/059-bis.c @@ -0,0 +1,142 @@ +#include +#include + +#include "array.h" + +int potential_good_str(char *str) { + int nb_spaces=0; + int nb_ascii=0; + int nb_non_ascii=0; + int nb_words=0; + int wordlen=0; + int maxwordlen=0; + int len; + while (*str) { + if (*str == ' ') nb_spaces++; + if (((*str >= 'a') && (*str <= 'z')) || + ((*str >= 'A') && (*str <= 'Z')) ) + { + nb_ascii++; + wordlen++; + } else { + if ((*str == ' ') || (*str == '.')) { + if (wordlen>maxwordlen) { + maxwordlen = wordlen; + } + if (wordlen>3) { + nb_words++; + } + } + wordlen=0; + } + if ((*str < 'A') || (*str > 'z')) nb_non_ascii++; + len++; + str++; + } + return (nb_words > 50); +} + +int decode(Array str, Array code, Array res) { + int i; + empty_Array(res); + if (str->length > res->max_length) { + return 1; + } + for (i=0;ilength;i++) { + push(res, str->chr[i] ^ code->chr[ i % code->length ]); + } + return 0; +} + + +int readInt(int *value, FILE* fd) { + int tmp=0; + int c=0; + for(c=fgetc(fd); (c != EOF) && (c >= '0') && (c <= '9') ; c=fgetc(fd) ) { + tmp=10*tmp + (c-'0'); + } + *value=tmp; + return c != EOF; +} + +void fill_content(char *filename, Array content) { + FILE *fd = NULL; + int value; + // ouverture du fichier + fd = (FILE *)fopen(filename, "r"); + if (fd == NULL) { + fprintf(stderr, "Impossible d'ouvrir %s\n", filename); + exit(2); + } + + // lecture du fichier + while ( readInt(&value,fd) ) { + push(content,value); + } + pop(content); + + fclose(fd); +} + +int rec_crack(Array content, Array code, Array decoded, char *buffer) { + int i; + char tmp[4]; + if (code->length == code->max_length) { + decode(content,code,decoded); + if (potential_good_str(str_Array(decoded,buffer))) { + fprintf(stderr,"Code: %s\nResult: %s\n",str_Array(code,tmp),buffer); + } + } else { + if (code->length == 1) { + fprintf(stderr,"%c",((code->chr[0]>20) && (code->chr[0]<127))?code->chr[0]:'.'); + } + for (i='a';i<='z';i++) { + push(code,i); + rec_crack(content,code,decoded,buffer); + pop(code); + } + } + return 0; +} + +int crack(Array content) { + Array decoded=new_Array(content->max_length); + Array code=new_Array(3); + char tmp[4]; + char *buffer=(char *)malloc(2000); + char *str; + int sum; + + // --- + push(code,'g'); + push(code,'o'); + push(code,'d'); + decode(content,code,decoded); + fprintf(stderr,"Code: %s\nResult: %s\n",str_Array(code,tmp),str_Array(decoded,buffer)); + for (str=buffer; *str; str++) { + sum += *str; + } + fprintf(stderr, "Somme: %d\n",sum); + // --- + + rec_crack(content,code,decoded,buffer); +} + +int main (int argc, char **argv) { + + Array content=new_Array(2000); + + // récupération des arguments + if (argc<2) { + fprintf(stderr, "Usage %s [file]\n", argv[0]); + exit(1); + } + + // rempli content avec le contenu du fichier 1 + fill_content(argv[1], content); + + crack(content); + + free(content); + return 0; +} diff --git a/059.hs b/059.hs new file mode 100644 index 0000000..19c5e18 --- /dev/null +++ b/059.hs @@ -0,0 +1,5 @@ +import System + +main = do + [filename] <- readArgs + f <- open( diff --git a/059.rb b/059.rb index 5e0536f..8fa53e2 100644 --- a/059.rb +++ b/059.rb @@ -1,11 +1,4 @@ codes = File.read("cipher1.txt").split(",").map { |x| x.to_i } -# words = File.read("words.txt").split(",").map { |s| s[1..-2] } -# $is_word={} -# $potential_codes=[] -# words.each do |w| -# $is_word[w]=true -# end - def to_ascii (l) res="" l.each do |c| @@ -14,14 +7,13 @@ def to_ascii (l) res end -# (0..255).each do |x| -# puts "#{x}:\n" -# (0..255).each do |y| -# (0..255).each do |z| -# to_ascii(codes,[x,y,z]) -# end -# end -# end - -puts to_ascii(codes) - +(0..255).each do |x| + (0..255).each do |y| + (0..255).each do |z| + res=to_ascii(codes,[x,y,z]) + res.each do |c| + case c.ord + end + end + end +end diff --git a/a.out b/a.out new file mode 100755 index 0000000..ed3b0c1 Binary files /dev/null and b/a.out differ diff --git a/array.h b/array.h new file mode 100644 index 0000000..cf79d8b --- /dev/null +++ b/array.h @@ -0,0 +1,93 @@ +#ifndef ARRAY +#define ARRAY + +#define true 1 +#define false 0 + +typedef struct t_array { + char *chr; + int length; + int max_length; +} *Array; + +void free_Array(Array array) { + free(array->chr); + free(array); +} + +Array new_Array(int size) { + fprintf(stderr,"Malloc new_Array: struct\n"); + Array array = (Array)malloc(sizeof(struct t_array)); + if (! array) + return NULL; + fprintf(stderr,"Malloc new_Array: array->chr\n"); + array->chr = (char *)malloc(size); + if (! array->chr) { + free(array); + return NULL; + } + array->length=0; + array->max_length=size; + return array; +} + +char *str_Array(Array array, char *tmp) { + int i; + + if (!tmp) { + fprintf(stderr,"Malloc str_Array\n"); + tmp=(char *)malloc(array->length+1); + } + for (i=0; ilength; i++){ + tmp[i]=array->chr[i]; + } + tmp[i]='\0'; + return tmp; +} + +char *show_Array(Array array, char *res) { + int i,j; + int t; + if (res == NULL) { + fprintf(stderr,"Malloc show_Array\n"); + res=(char *)malloc(4*array->max_length + 1); + } + + fprintf(stderr,"Length = %d\n", array->length); + for (i=0,j=0;ilength;i++) { + t = array->chr[i]; + if (t>=100) { + res[j++]=t/100 + '0'; + j++; + } + if (t>=10) { + res[j++]=t/10 - 10*(t/100) + '0'; + } + res[j++]=t - 10*(t/10) - 100*(t/100) + '0'; + res[j++]=','; + } + res[j]='\0'; + + return res; +} + +int push (Array array, char value) { + if (array->length < array->max_length) { + array->chr[array->length]=value; + array->length++; + return true; + } + return false; +} + +int pop(Array array) { + if (array->length == 0) + return -1; + array->length--; + return array->chr[array->length]; +} +void empty_Array(Array array) { + array->length = 0; +} + +#endif diff --git a/cipher1.enc b/cipher1.enc index 6c9dd8a..e69de29 100644 Binary files a/cipher1.enc and b/cipher1.enc differ