finished task 4.
This commit is contained in:
parent
52c47a4166
commit
cab23d5ef5
4 changed files with 25202 additions and 41 deletions
2
hw3/task4/.gitignore
vendored
Normal file
2
hw3/task4/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
find_key
|
||||||
|
testing.txt
|
|
@ -1,12 +1,15 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
|
||||||
using namespace std;
|
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)
|
int do_crypt(FILE *in, FILE *out, string inkey, int do_encrypt)
|
||||||
{
|
{
|
||||||
/* Allow enough space in output buffer for additional block */
|
/* 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
|
* Bogus key and IV: we'd normally set these from
|
||||||
* another source.
|
* 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())
|
inkey += ' ';
|
||||||
{
|
|
||||||
key[i] = inkey.at(i);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
// Converting the input key into an unsigned char array.
|
||||||
key[i] = '\x20';
|
unsigned char key[17];
|
||||||
}
|
strcpy((char*) key, inkey.c_str());
|
||||||
}
|
|
||||||
//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 */
|
/* Don't set key or IV right away; we want to check lengths */
|
||||||
ctx = EVP_CIPHER_CTX_new();
|
ctx = EVP_CIPHER_CTX_new();
|
||||||
EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, do_encrypt);
|
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 */
|
/* Now we can set key and IV */
|
||||||
EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
|
EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, do_encrypt);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
inlen = fread(inbuf, 1, 256, in);
|
inlen = fread(inbuf, 1, 1024, in);
|
||||||
if (inlen <= 0)
|
if (inlen <= 0)
|
||||||
break;
|
break;
|
||||||
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
|
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)) {
|
if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
|
||||||
/* Error */
|
/* Error */
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "I made it." << endl;
|
|
||||||
if(outlen > 0)
|
|
||||||
cout << outbuf << endl;
|
|
||||||
fwrite(outbuf, 1, outlen, out);
|
fwrite(outbuf, 1, outlen, out);
|
||||||
|
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
EVP_CIPHER_CTX_cleanup(ctx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
cout << "Test." << endl;
|
// Initialize files.
|
||||||
|
|
||||||
FILE *input;
|
FILE *input;
|
||||||
FILE *output;
|
FILE *output;
|
||||||
//FILE *original;
|
|
||||||
|
|
||||||
|
// Open the needed files.
|
||||||
input = fopen("ciphertext.txt", "rb");
|
input = fopen("ciphertext.txt", "rb");
|
||||||
output = fopen("testing.txt", "wb");
|
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 words("words.txt");
|
||||||
ifstream original("plaintext.txt");
|
ifstream original("plaintext.txt");
|
||||||
|
|
||||||
|
// Just an initial key. It will be overwritten.
|
||||||
string key = "0123456789abcdef";
|
string key = "0123456789abcdef";
|
||||||
|
|
||||||
|
// Variable to storing the goal string.
|
||||||
string secret = "";
|
string secret = "";
|
||||||
getline(original, secret);
|
getline(original, secret);
|
||||||
|
|
||||||
char line[256];
|
// Variable for storing output when we read
|
||||||
|
// from the file that we wrote to.
|
||||||
unsigned char keyc[16];
|
string templine = "";
|
||||||
|
|
||||||
while(!words.eof())
|
while(!words.eof())
|
||||||
{
|
{
|
||||||
|
// Get the current key to test with.
|
||||||
key = "";
|
key = "";
|
||||||
getline(words, key);
|
getline(words, key);
|
||||||
|
|
||||||
|
// Only test it if it's less that 16 characters.
|
||||||
if(key.length() > 16)
|
if(key.length() > 16)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//cout << key << endl;
|
|
||||||
|
|
||||||
|
// Try to decrypt with this key.
|
||||||
do_crypt(input, output, key, 0);
|
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);
|
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);
|
// Output the key and the output text
|
||||||
int size = ftell(output);
|
// to prove we know the key and we
|
||||||
|
// successfully decrypted the message.
|
||||||
if (0 != size)
|
cout << key << endl;
|
||||||
|
cout << templine << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
fgets(line, sizeof(line), output);
|
// We didn't decrypt it, try again,
|
||||||
cout << key << "\t" << secret << "\t" << line << endl;
|
// overwrite whatever was in the file.
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(output);
|
|
||||||
output = fopen("testing.txt", "wb");
|
output = fopen("testing.txt", "wb");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
fclose(input);
|
fclose(input);
|
||||||
fclose(output);
|
|
||||||
words.close();
|
words.close();
|
||||||
original.close();
|
original.close();
|
||||||
|
|
||||||
|
|
10
hw3/task4/find_key.sh
Executable file
10
hw3/task4/find_key.sh
Executable 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
25129
hw3/task4/words_hex.txt
Normal file
File diff suppressed because it is too large
Load diff
Reference in a new issue