#include #include #include #include #include #include using namespace std; int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt) { /* Allow enough space in output buffer for additional block */ unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int inlen, outlen; EVP_CIPHER_CTX *ctx; /* * Bogus key and IV: we'd normally set these from * another source. */ unsigned char iv[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //unsigned char iv[] = "0000000000000000"; unsigned char key[16]; for(int i = 0; i < 16; i++) { if(i < inkey.length()) { key[i] = inkey.at(i); } else { key[i] = '\x20'; } } //string str( key, key + sizeof key / sizeof key[0] ); //cout << str << endl; /* Don't set key or IV right away; we want to check lengths */ ctx = EVP_CIPHER_CTX_new(); EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, do_encrypt); OPENSSL_assert(EVP_CIPHER_CTX_key_length(ctx) == 16); OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) == 16); /* Now we can set key and IV */ EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt); for (;;) { inlen = fread(inbuf, 1, 256, in); if (inlen <= 0) break; if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } fwrite(outbuf, 1, outlen, out); } if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) { /* Error */ EVP_CIPHER_CTX_free(ctx); return 0; } cout << "I made it." << endl; if(outlen > 0) cout << outbuf << endl; fwrite(outbuf, 1, outlen, out); EVP_CIPHER_CTX_free(ctx); return 1; } int main() { cout << "Test." << endl; FILE *input; FILE *output; //FILE *original; input = fopen("ciphertext.txt", "rb"); output = fopen("testing.txt", "wb"); //original = fopen("plaintext.txt", "r"); ifstream words("words.txt"); ifstream original("plaintext.txt"); string key = "0123456789abcdef"; string secret = ""; getline(original, secret); char line[256]; unsigned char keyc[16]; while(!words.eof()) { key = ""; getline(words, key); if(key.length() > 16) { continue; } //cout << key << endl; do_crypt(input, output, key, 0); fclose(output); output = fopen("testing.txt", "r"); if (NULL != output) { fseek (output, 0, SEEK_END); int size = ftell(output); if (0 != size) { fgets(line, sizeof(line), output); cout << key << "\t" << secret << "\t" << line << endl; } } fclose(output); output = fopen("testing.txt", "wb"); } fclose(input); fclose(output); words.close(); original.close(); return 0; }