Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <aes.h>
- /*
- * Verschlüsselungsverfahren zum entschlüsseln Camellia-128-CBC
- * Byte Nummer 3
- * Verschlüsselungsverfahren zum verschlüsseln MD5
- */
- static char* SRC_CIPHER_BIN_PATH = "s70307-src-cipher.bin";
- static char* CORRUPT_SRC_KEY_PATH = "s70307-corrupt-src-key.bin";
- static char* DEST_CIPHER_BIN_PATH = "s70307-dest-key.bin";
- static char* DECRYPT_FILE_PDF_PATH = "decrypted-s70307.pdf";
- int CORRUPT_BYTE_POSITION = 3;
- unsigned char* SRC_CIPHER_BIN_TEXT, CORRUPT_SRC_KEY_TEXT;
- char* DECRYPTED_CIPHER;
- unsigned int getFileSize(FILE* data);
- unsigned char* readInFile(FILE *data, char* name);
- int decrypt(unsigned char* cipher, int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char* key, FILE *decrypted_file);
- int bruteForce(unsigned char* cipher,int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char* key, FILE *decrypted_file);
- int main(int argc, char* argv[]) {
- FILE *src_cipher, *cipher_dest, *corrupt_key, *decrypted_file;
- unsigned char* cipher;
- int cipher_len;
- unsigned char iv[EVP_MAX_IV_LENGTH];
- unsigned char key[EVP_MAX_KEY_LENGTH];
- unsigned int key_len;
- /* Check for correct number of values
- if (argc < 5)
- {
- fprintf(stderr, "Usage: %s <snumber> <out>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- */
- /*Opening files binary coded*/
- /* Open cipher file */
- src_cipher = fopen(SRC_CIPHER_BIN_PATH, "rb");
- if (!src_cipher)
- {
- fprintf(stderr, "Failed to load %s\n", SRC_CIPHER_BIN_PATH);
- exit(EXIT_FAILURE);
- }
- /* Saving cipher */
- cipher = readInFile(src_cipher, SRC_CIPHER_BIN_PATH);
- cipher_len = getFileSize(src_cipher);
- //printf("Size of cipher: %i\n", cipher_len);
- /* Open cipher file */
- corrupt_key = fopen(CORRUPT_SRC_KEY_PATH, "rb");
- if (!corrupt_key)
- {
- fprintf(stderr, "Failed to load %s\n", CORRUPT_SRC_KEY_PATH);
- exit(EXIT_FAILURE);
- }
- /* Saving key for decrypting
- key = malloc(EVP_CIPHER_key_length(EVP_camellia_128_cbc()));
- printf("%d", EVP_CIPHER_key_length(EVP_camellia_128_cbc()));*/
- /* Lese die Länge des Session Keys
- if(fread(&key_len_ln, sizeof(key_len_ln), 1, corrupt_key) != 1)
- {
- fprintf(stderr, "Einlesen der Keylaenge.\n");
- }
- printf("DKey Len:: %u\n", key_len_ln);
- key_len = ntohl(key_len_ln);*/
- if(fread(key, EVP_CIPHER_key_length(EVP_camellia_128_cbc()), 1, corrupt_key) != 1)
- {
- fprintf(stderr, "einlesen des keys");
- }
- if (sizeof(key) < EVP_CIPHER_key_length(EVP_camellia_128_cbc())) {
- fprintf(stderr, "Decryption failed - wrong length of used key.\n");
- exit(EXIT_FAILURE);
- }
- key_len = sizeof(key);
- if(fread(iv, EVP_CIPHER_iv_length(EVP_camellia_128_cbc()), 1, corrupt_key) != 1)
- {
- fprintf(stderr, "Lesen des Eingabestreams/-datei fehlgeschlagen.\n");
- }
- printf("Initialization vector: %lu\n", sizeof(iv));
- decrypted_file = fopen(DECRYPT_FILE_PDF_PATH, "wb");
- if (!decrypted_file)
- {
- fprintf(stderr, "Failed to load %s\n", DECRYPT_FILE_PDF_PATH);
- exit(EXIT_FAILURE);
- }
- //decrypt(cipher, cipher_len, iv, key, decrypted_file);
- bruteForce(cipher, cipher_len, iv, key, decrypted_file);
- fclose(src_cipher);
- fclose(corrupt_key);
- printf("test: %d \n", EVP_CIPHER_iv_length(EVP_camellia_128_cbc()));
- return 1;
- }
- int bruteForce(unsigned char* cipher,int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char key[EVP_MAX_KEY_LENGTH], FILE *decrypted_file){
- int i;
- printf("Starting to bruteforce the coorect decrypting key...\n");
- for (i = 0; i < 256; i++) {
- key[CORRUPT_BYTE_POSITION - 0] = i;
- printf("%d = %c\n", i, i);
- //printf("Current key is tested: %s", key);
- if(decrypt(cipher, clen, iv, key, decrypted_file) == 1 ){
- printf("[6] Bruteforcing was successfully\n");
- printf("[7] Match for corrupted byte was found. Which is byte: %d - character: %c \n",key[i],key[i]);
- return 1;
- }
- }
- return 0;
- }
- int decrypt(unsigned char* cipher, int clen, unsigned char iv[EVP_MAX_IV_LENGTH], unsigned char key[EVP_MAX_KEY_LENGTH], FILE *decrypted_file){
- unsigned char buffer[1024];
- unsigned char* buffer_out;
- int length = 16;
- int len_out = 0;
- printf("---------processing---------\n");
- EVP_CIPHER_CTX ctx;
- printf("[1] CTX struct initialized\n");
- /* EVP Chiffre-Kontext initialisieren */
- //EVP_CIPHER_CTX_init(&ctx);
- if( EVP_DecryptInit_ex(&ctx, EVP_camellia_128_cbc(), NULL, key, iv) == 0){
- EVP_CIPHER_CTX_cleanup(&ctx);
- fprintf(stderr,"Error while initializing decryption framework\n");
- return 0;
- }
- printf("[2] Decryption initated\n");
- if(!(buffer_out = malloc(clen*3+1))){
- printf("tmp_out speicherreservierung hat ni geklappt!\n");
- }
- printf("%i",clen);
- if( EVP_DecryptUpdate(&ctx, buffer_out, &len_out, cipher, clen) == 0){
- EVP_CIPHER_CTX_cleanup(&ctx);
- fprintf(stderr,"Encryption of the current block failed");
- return 0;
- }
- printf("[3] Using DecryptUpdate and trying to decrypt.\n");
- if(len_out == 0 || EVP_DecryptFinal_ex(&ctx, buffer_out+len_out, &length) == 0){
- EVP_CIPHER_CTX_cleanup(&ctx);
- fprintf(stderr,"Encryption and writing to file failed.\n");
- return 0;
- }
- printf("[4] DecryptFinal: Decryption sucessfully with a plaintext length: %lu\n\tplaintext:", sizeof(buffer_out));
- /*int d;
- for (d = 0; d < len_out+1000; ++d) {
- printf("%i ", buffer_out[d]);
- }
- printf("\n%i", d);*/
- // printf("The key for decryption is: %s\n", key);
- /* if (fwrite(buffer_out, len_out, 1, decrypted_file) != 1)
- {
- fprintf(stderr, "Writing to disk failed.\n");
- EVP_CIPHER_CTX_cleanup(&ctx);
- fclose(decrypted_file);
- }*/
- int c;
- for (c = 0; c < len_out; ++c) {
- fputc(buffer_out[c], decrypted_file);
- }
- fclose(decrypted_file);
- printf("[5] Created a .pdf file with a size of %i\n", len_out);
- return 1;
- }
- unsigned int getFileSize(FILE* data){
- int fileSize = 0;
- fseek(data, 0L, SEEK_END);
- fileSize = ftell(data);
- rewind(data);
- return fileSize;
- }
- /*
- * Function to read in the file data
- */
- unsigned char* readInFile(FILE *data, char* name){
- unsigned int data_length = 0;
- data_length = getFileSize(data);
- unsigned char *buff = NULL;
- buff = malloc(sizeof(unsigned char)*data_length);
- if(buff==NULL)
- {
- fprintf(stderr,"error reading file");
- }
- fread(buff,sizeof(char),data_length,data);
- fprintf(stderr,"File: %s\t - read succesfully\n",name );
- return buff;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement