From 670fe4b90a9a9dfbc7956416d727ba70cf9554f0 Mon Sep 17 00:00:00 2001 From: Krzysztof KWIATKOWSKI Date: Sun, 14 Jun 2015 01:07:44 +0200 Subject: [PATCH] Set one almost done --- Makefile | 5 +- MatsanoCrypto.sublime-workspace | 513 ++++++++++++++++++++++++++++---- etc/set1_t6.txt | 51 +++- etc/set1_t6.txt.orig | 64 ---- etc/set1_t7.txt | 64 ++++ etc/set1_t8.txt | 204 +++++++++++++ set1/ecb.c | 51 ++++ set1/ecb.h | 6 + set1/runner.cpp | 63 +++- set1/runner.h | 12 +- set1/xor_char_finder.cpp | 5 +- utils/base64.cpp | 4 +- utils/base64.h | 2 +- utils/runner.cpp | 4 +- 14 files changed, 890 insertions(+), 158 deletions(-) delete mode 100644 etc/set1_t6.txt.orig create mode 100644 etc/set1_t7.txt create mode 100644 etc/set1_t8.txt create mode 100644 set1/ecb.c create mode 100644 set1/ecb.h diff --git a/Makefile b/Makefile index 6c2177e..edb18c1 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,11 @@ all: g++ -g -I${INC} -c set1/xor_char_finder.cpp -o obj/xor_char_finder.o g++ -g -I${INC} -c utils/hamming.c -o obj/hamming.o g++ -g -I${INC} -c set1/runner.cpp -o obj/set1_runner.o + g++ -g -I${INC} -c set1/ecb.c -o obj/ecb.o g++ -g -I${INC} -c main.cpp -o obj/main.o - g++ -g -lpthread -o main obj/common.o obj/base64.o obj/xor_char_finder.o obj/xor.o \ + g++ -g -lpthread -lcrypto -o main obj/common.o obj/base64.o obj/xor_char_finder.o obj/xor.o \ obj/hamming.o obj/utils_runner.o \ - obj/set1_runner.o obj/main.o + obj/set1_runner.o obj/main.o obj/ecb.o clean: rm -rf obj diff --git a/MatsanoCrypto.sublime-workspace b/MatsanoCrypto.sublime-workspace index e0877d6..a54971e 100644 --- a/MatsanoCrypto.sublime-workspace +++ b/MatsanoCrypto.sublime-workspace @@ -3,6 +3,10 @@ { "selected_items": [ + [ + "bes", + "best_key_size" + ], [ "freq", "frequency_analysis" @@ -510,10 +514,6 @@ [ "subsci", "subscriber" - ], - [ - "subs", - "subscribe" ] ] }, @@ -523,15 +523,15 @@ "file": "set1/runner.cpp", "settings": { - "buffer_size": 10713, + "buffer_size": 13139, "line_ending": "Unix" } }, { - "file": "utils/hamming.c", + "file": "utils/base64.cpp", "settings": { - "buffer_size": 1696, + "buffer_size": 8008, "line_ending": "Unix" } }, @@ -539,7 +539,67 @@ "file": "set1/xor_char_finder.h", "settings": { - "buffer_size": 3136, + "buffer_size": 3437, + "line_ending": "Unix" + } + }, + { + "file": "set1/xor_char_finder.cpp", + "settings": + { + "buffer_size": 6055, + "line_ending": "Unix" + } + }, + { + "contents": "p=\"i'm 'ack knD*i'm Ringin'eth be%- *A rockSne on T-o mi$e sHileethe llY*GirlS yell OIneecs= sY in th_ ack O#*me EWehL th$t's gyNj DeShay cu1ti+' a%- Them Z'I HHittI+- ha=d eNd t-e gixlIoS goIn' cra?y OVan -lA's on Nh' mikEi*manoI'inotelazy$ *i'm Lettin'emyedru.akIck in 0I6 conT7els \\\"y iOutheand C BoGin *To jus1 l t i=afLow, leN /y coN&optsogo$*My 5osse-s~O thE side +#include +#include + +uint32_t ecb_decrypt(const unsigned char* i_stream, const uint32_t i_stream_size, const unsigned char* i_pass, const uint32_t i_pass_size, unsigned char* o_buff) +{ + int ret, ret_fin; + ret=ret_fin=0; + EVP_CIPHER_CTX ctx; + + EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL); + EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size); + EVP_DecryptInit (&ctx, NULL, i_pass, NULL); + EVP_DecryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size); + EVP_DecryptFinal (&ctx, o_buff+ret, &ret_fin); + return ret+ret_fin; +} + + +uint32_t ecb_encrypt(const unsigned char* i_stream, const uint32_t i_stream_size, const unsigned char* i_pass, const uint32_t i_pass_size, unsigned char* o_buff) +{ + int ret, ret_fin; + ret=ret_fin=0; + EVP_CIPHER_CTX ctx; + + EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL); + EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size); + EVP_EncryptInit (&ctx, NULL, i_pass, NULL); + EVP_EncryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size); + EVP_EncryptFinal (&ctx, o_buff+ret, &ret_fin); + return ret+ret_fin; +} + +// checks if there are identical blocks in the i_textline +int same_blocks(const char* i_textline, const int i_bs, int* first_block, int* second_block) +{ + int chunk_nb = strlen(i_textline)/i_bs; + for(int i=0; i + +// return output buffer size +uint32_t ecb_decrypt(const unsigned char* i_stream, const uint32_t i_stream_size, const unsigned char* i_pass, const uint32_t i_pass_size, unsigned char* o_buff); +uint32_t ecb_encrypt(const unsigned char* i_stream, const uint32_t i_stream_size, const unsigned char* i_pass, const uint32_t i_pass_size, unsigned char* o_buff); +int same_blocks(const char* i_textline, const int i_bs, int* first_block, int* second_block); \ No newline at end of file diff --git a/set1/runner.cpp b/set1/runner.cpp index b9d9d70..2ae6b94 100644 --- a/set1/runner.cpp +++ b/set1/runner.cpp @@ -7,7 +7,7 @@ #include "utils/base64.h" #include "utils/xor.h" #include "utils/hamming.h" - +#include "set1/ecb.h" char guess_encryption_key_letter(const char ciphertext_hex[]) { @@ -174,19 +174,20 @@ void set1_challenge_6_test() unsigned char hex_buf[1024*1024]; unsigned long len=read_file_to_buffer("etc/set1_t6.txt", base64_buf); base64_buf[len]='\0'; - len=base64_to_hex(base64_buf, len, hex_buf); + len=base64_to_hex((unsigned char*)base64_buf, len, hex_buf); convert_hex_to_string(hex_buf, len, base64_buf); // now base64_buf contains string with ciphertext that is HEX-encoded memset(out_buf,0,sizeof(out_buf)); - int best_key_size = find_best_keysize(base64_buf, strlen(base64_buf),29,30); - decrypt_repeted_xor(base64_buf, out_buf, best_key_size); + int best_key_size = find_best_keysize(base64_buf, strlen(base64_buf)); + best_key_size=decrypt_repeted_xor(base64_buf, out_buf, best_key_size); // sprintf((char*)error_buf, "Wrong keysize. Expected 5 got %d\n", best_key_size); // check(memcmp(expected_plaintext1, out_buf, sizeof(expected_plaintext1))==0, out_buf); + printf("Key size: %d. Content:\n ", best_key_size); for(int i=0; i