Resolved pb 59 in C
This commit is contained in:
parent
05c4e97723
commit
c3c2e580d0
6 changed files with 250 additions and 18 deletions
142
059-bis.c
Normal file
142
059-bis.c
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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;i<str->length;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;
|
||||||
|
}
|
5
059.hs
Normal file
5
059.hs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import System
|
||||||
|
|
||||||
|
main = do
|
||||||
|
[filename] <- readArgs
|
||||||
|
f <- open(
|
28
059.rb
28
059.rb
|
@ -1,11 +1,4 @@
|
||||||
codes = File.read("cipher1.txt").split(",").map { |x| x.to_i }
|
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)
|
def to_ascii (l)
|
||||||
res=""
|
res=""
|
||||||
l.each do |c|
|
l.each do |c|
|
||||||
|
@ -14,14 +7,13 @@ def to_ascii (l)
|
||||||
res
|
res
|
||||||
end
|
end
|
||||||
|
|
||||||
# (0..255).each do |x|
|
(0..255).each do |x|
|
||||||
# puts "#{x}:\n"
|
(0..255).each do |y|
|
||||||
# (0..255).each do |y|
|
(0..255).each do |z|
|
||||||
# (0..255).each do |z|
|
res=to_ascii(codes,[x,y,z])
|
||||||
# to_ascii(codes,[x,y,z])
|
res.each do |c|
|
||||||
# end
|
case c.ord
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
|
end
|
||||||
puts to_ascii(codes)
|
end
|
||||||
|
|
||||||
|
|
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
93
array.h
Normal file
93
array.h
Normal file
|
@ -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; i<array->length; 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;i<array->length;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
|
BIN
cipher1.enc
BIN
cipher1.enc
Binary file not shown.
Loading…
Reference in a new issue