diff options
| author | Benedict <benedict@0xb8000.de> | 2016-03-22 19:11:22 +0100 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2017-02-21 13:00:25 +0100 |
| commit | 4ed371cc378a3b579d46ed89a2677769a6d5ea24 (patch) | |
| tree | 9e9bb09f2798954540bc62d452258a74923c5e8d /lib/lib.c | |
| parent | b586255b0dc1d6940c4a552a7125c9875e8adba6 (diff) | |
completed set 2 challenge 11
Diffstat (limited to 'lib/lib.c')
| -rw-r--r-- | lib/lib.c | 50 |
1 files changed, 42 insertions, 8 deletions
@@ -81,7 +81,6 @@ static void three_bytes_to_base64(char * encode, int bytes_to_print, char *resul // last six bit11s four = encode[2] & 0x3F; ret[3] = base64_encode[four]; - ret[4] = '\0'; if(bytes_to_print-- > 0) result[3] = ret[3]; } @@ -378,12 +377,11 @@ static int frequent_histogramm_matchs(char *string, int length) * encode the given string into base64 and stores it in result */ -void encode_to_base64(char *encode, char *result) +void encode_to_base64(char *encode, int length_encode, char *result) { - int length = strlen(encode); - int rounds = length / 3; - int bytes_last_round = length % 3; + int rounds = length_encode / 3; + int bytes_last_round = length_encode % 3; int i; for (i=0;i<rounds;i++) { @@ -391,11 +389,15 @@ void encode_to_base64(char *encode, char *result) } // in der letzen runde nicht mehr alle ausgeben // nur noch 3-leftover + result[rounds*4] = '\0'; + if (bytes_last_round > 0) { - three_bytes_to_base64(encode + i*3, 1+bytes_last_round, &result[i*4]); + three_bytes_to_base64(&encode[i*3], (4-bytes_last_round), &result[i*4]); + + for(i=bytes_last_round;i>=0;i--) + result[rounds*4+(4-bytes_last_round)] = '='; - for(i=0;i<(3-bytes_last_round);i++) - result[i*4+(1+bytes_last_round)] = '='; + result[rounds*4+4] = '\0'; } } @@ -522,3 +524,35 @@ int read_base64_file(const char *file, char **out) return file_pos; } + +int count_equal_blocks(char *string, int length_string, int blocksize) +{ + int number_blocks = length_string / blocksize; + printf("number_blocks: %i", number_blocks); + int hits = 0, i, j, k; + + for(i=0;i<number_blocks;i++) { + for(j=1;j<number_blocks;j++) { + for(k=0;k<blocksize;k++) { + if(string[i*blocksize+k] != string[j*blocksize+k]) + break; + if ( k == (blocksize-1)) + hits++; + } + } + } + printf("hits:%i\n", hits); + return hits; + +} + +int string_is_ecb_encrypted(char *string, int length_string, int blocksize) +{ + /** + * equal cleartext blocks results in equal ciphertext blocks + * in ECB mode, so basically we are just counting equal blocks... + */ + return (count_equal_blocks(string, length_string, blocksize) > (length_string/blocksize)); +} + + |
