finished task 4.

This commit is contained in:
Alex Huddleston 2018-03-05 23:05:46 -06:00
parent 52c47a4166
commit cab23d5ef5
4 changed files with 25202 additions and 41 deletions

2
hw3/task4/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
find_key
testing.txt

View file

@ -1,12 +1,15 @@
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <ctype.h>
#include <openssl/evp.h>
using namespace std;
// This is the same as what was given, just added the key string as a parameter
// so I could manipulate it in here instead.
int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt)
{
/* Allow enough space in output buffer for additional block */
@ -17,23 +20,20 @@ int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt)
* 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++)
// hardcoding the IV because we already know what it is.
unsigned char iv[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// I'm adding the spaces in here.
for(int i = inkey.length(); i < 16; ++i)
{
if(i < inkey.length())
{
key[i] = inkey.at(i);
inkey += ' ';
}
else
{
key[i] = '\x20';
}
}
//string str( key, key + sizeof key / sizeof key[0] );
//cout << str << endl;
// Converting the input key into an unsigned char array.
unsigned char key[17];
strcpy((char*) key, inkey.c_str());
/* 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);
@ -42,7 +42,7 @@ int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt)
/* Now we can set key and IV */
EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
for (;;) {
inlen = fread(inbuf, 1, 256, in);
inlen = fread(inbuf, 1, 1024, in);
if (inlen <= 0)
break;
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
@ -55,75 +55,95 @@ int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt)
if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
// I added this just in case to make sure thigns cleaned up properly.
EVP_CIPHER_CTX_cleanup(ctx);
return 0;
}
cout << "I made it." << endl;
if(outlen > 0)
cout << outbuf << endl;
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_CTX_cleanup(ctx);
return 1;
}
int main()
{
cout << "Test." << endl;
// Initialize files.
FILE *input;
FILE *output;
//FILE *original;
// Open the needed files.
input = fopen("ciphertext.txt", "rb");
output = fopen("testing.txt", "wb");
//original = fopen("plaintext.txt", "r");
// I prefer ifstreams when dealing with files,
// but I leave the other two as file pointers
// so I don't have to change more code.
ifstream words("words.txt");
ifstream original("plaintext.txt");
// Just an initial key. It will be overwritten.
string key = "0123456789abcdef";
// Variable to storing the goal string.
string secret = "";
getline(original, secret);
char line[256];
unsigned char keyc[16];
// Variable for storing output when we read
// from the file that we wrote to.
string templine = "";
while(!words.eof())
{
// Get the current key to test with.
key = "";
getline(words, key);
// Only test it if it's less that 16 characters.
if(key.length() > 16)
{
continue;
}
//cout << key << endl;
// Try to decrypt with this key.
do_crypt(input, output, key, 0);
// Set the file pointers back to the beginning
// of the files.
rewind(input);
rewind(output);
// Read the file and see if it matches the original
// message, if it does, then we guessed the key.
fclose(output);
output = fopen("testing.txt", "r");
if (NULL != output)
ifstream tempoutput("testing.txt");
getline(tempoutput, templine);
tempoutput.close();
if(templine.compare("This is a top secret.") == 0)
{
fseek (output, 0, SEEK_END);
int size = ftell(output);
if (0 != size)
// Output the key and the output text
// to prove we know the key and we
// successfully decrypted the message.
cout << key << endl;
cout << templine << endl;
break;
}
else
{
fgets(line, sizeof(line), output);
cout << key << "\t" << secret << "\t" << line << endl;
}
}
fclose(output);
// We didn't decrypt it, try again,
// overwrite whatever was in the file.
output = fopen("testing.txt", "wb");
}
}
fclose(input);
fclose(output);
words.close();
original.close();

10
hw3/task4/find_key.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
filename="itworkmaybe.txt"
while read line; do
openssl enc -aes-128-cbc -d -in ciphertext.txt -out itworkmaybe.txt -iv 0000000000000000 -K $line
read aline < "$filename"
if [ "$aline" == "This is a top secret." ]; then
echo $line
break
fi
done < "$1"

25129
hw3/task4/words_hex.txt Normal file

File diff suppressed because it is too large Load diff