Im supposed to use the following format to write a decryption program. The thing though is I have no idea where to start. I looked through the code and I believe the decrypting part of it lies with the chain that converts the password into the key.but other than that I have no idea where to start. A point in the right direction is appreciated.
Code:
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
int write_data(const char *filename, char *out, int len, unsigned char *key)
{
int total, written;
BIO *cipher, *b64, *buffer, *file;
/* Create a buffered file BIO for writing */
file = BIO_new_file(filename, "w");
if (!file)
return 0;
/* Create a buffering filter BIO to buffer writes to the file */
buffer = BIO_new(BIO_f_buffer());
/* Create a base64 encoding filter BIO */
b64 = BIO_new(BIO_f_base64());
/* Create the cipher filter BIO and set the key. The last parameter of
BIO_set_cipher is 1 for encryption and 0 for decryption */
cipher = BIO_new(BIO_f_cipher());
BIO_set_cipher(cipher, EVP_aes_256_ecb(), key, NULL, 1);
/* Assemble the BIO chain to be in the order cipher-b64-buffer-file */
BIO_push(cipher, b64);
BIO_push(b64, buffer);
BIO_push(buffer, file);
/* This loop writes the data to the file. It checks for errors as if
the underlying file were non-blocking */
for (total = 0; total < len; total += written)
{
if ((written = BIO_write(cipher, out + total, len - total)) <= 0)
{
if (BIO_should_retry(cipher))
{
written = 0;
continue;
}
break;
}
}
BIO_pop(b64);
/* Ensure all of our data is pushed all the way to the file */
BIO_flush(cipher);
BIO_free_all(cipher);
return 0;
}
int main(int argc, char **argv)
{
int i, size, len;
char pass[100];
unsigned char key[64];
char *buf;
BIO *sha, *bio;
FILE *file;
/* Make sure that 2 arguments are provided */
if (argc != 3)
{
printf("Usage: %s plaintext ciphertext\n", argv[0]);
abort();
}
/* Open the plaintext file and copy it into array *buf*. Store
the file size into variable *size* */
file = fopen(argv[1], "r");
if (!file)
{
printf("Cannot open file %s\n", argv[1]);
abort();
}
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
buf = (char *)malloc(size);
fread(buf, 1, size, file);
fclose(file);
/* Ask the user for the encryption password */
printf("Enter encryption password: ");
scanf("%s", pass);
/* Create a digest filter (SHA-256) to convert the password into a
256-bit key */
sha = BIO_new(BIO_f_md());
BIO_set_md(sha, EVP_sha256());
/* Create a memory BIO to store the digest (chain it to *sha*) */
bio = BIO_new(BIO_s_null());
BIO_push(sha, bio);
/* Compute the digest and read it into the *key* string */
BIO_write(sha, pass, strlen(pass));
len = BIO_gets(sha, key, 64);
/* Output the key in hexadecimal */
printf("The key is: ");
for(i = 0; i < len; i++)
printf(":%02X", key[i]);
printf("\n");
/* Call function write_data to encrypt the file */
write_data(argv[2], buf, size, key);
free(buf);
BIO_free_all(sha);
return 0;
}