#include #include #include #include #include /* * Decrypt *len bytes of ciphertext */ unsigned char *aes_decrypt(EVP_CIPHER_CTX *e, unsigned char *ciphertext, int *len) { /* plaintext will always be equal to or lesser than length of ciphertext*/ int p_len = *len, f_len = 0; unsigned char *plaintext = malloc(p_len); EVP_DecryptInit_ex(e, NULL, NULL, NULL, NULL); EVP_DecryptUpdate(e, plaintext, &p_len, ciphertext, *len); EVP_DecryptFinal_ex(e, plaintext+p_len, &f_len); //*len = p_len + f_len; return plaintext; } int aes_init(unsigned char *key_data, int key_data_len, EVP_CIPHER_CTX *e_ctx, EVP_CIPHER_CTX *d_ctx){ int i, nrounds = 5; unsigned char key[32], iv[32]; //Some robust programming to start with //Only use most significant 32 bytes of data if > 32 bytes if(key_data_len > 32) key_data_len =32; //Copy bytes to the front of the key array for (i=0;i 32 bytes if(key_data_len > 32) key_data_len =32; //Copy bytes to the front of the key array for (i=0;i> 48); trialkey[26] = (unsigned char) (trialLowBits >> 40); trialkey[27] = (unsigned char) (trialLowBits >> 32); trialkey[28] = (unsigned char) (trialLowBits >> 24); trialkey[29] = (unsigned char) (trialLowBits >> 16); trialkey[30] = (unsigned char) (trialLowBits >> 8); trialkey[31] = (unsigned char) (trialLowBits); //if(counter ==56){ EVP_CIPHER_CTX en, de; if (aes_init(trialkey, trial_key_length, &en, &de)) { printf("Couldn't initialize AES cipher\n"); return -1; } //if(counter ==56){ plaintext = (char *)aes_decrypt(&de, (unsigned char *)cipher_in, &cipher_length); EVP_CIPHER_CTX_cleanup(&en); EVP_CIPHER_CTX_cleanup(&de); //printf("\nKey %lu:%s plain: %s",counter, trialkey, plaintext); int y; if (strncmp(plaintext, plain_in, 28)); //printf("\nFAIL: enc/dec failed for \"%s\"\n", plain_in); else{ printf("\nOK: enc/dec ok for \"%s\"\n", plaintext); printf("Key No.:%d:", counter); for(y=0;y<32;y++){ printf("%c",trialkey[y]); } printf("\n"); break; } free(plaintext); //} } }