Thread: openssl decryption

  1. #1
    Registered User
    Join Date
    May 2009

    openssl decryption

    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.
    #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;
    /* Ensure all of our data is pushed all the way to the file */
    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]);
    /* 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]);
    fseek(file, 0, SEEK_END);
    size = ftell(file);
    buf = (char *)malloc(size);
    fread(buf, 1, size, 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]);
    /* Call function write_data to encrypt the file */
    write_data(argv[2], buf, size, key);
    return 0;

  2. #2
    Registered User
    Join Date
    May 2009
    Attention: Ignore this thread. I answered my own question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openssl/RAND_bytes
    By kakashi316 in forum C Programming
    Replies: 5
    Last Post: 03-21-2012, 09:37 AM
  2. Need help with OpenSSL
    By Ricardo_R5 in forum C Programming
    Replies: 0
    Last Post: 05-07-2007, 06:18 PM
  3. Using OpenSSL with Dev-C++
    By Smiley10 in forum C Programming
    Replies: 2
    Last Post: 07-08-2006, 10:27 AM
  4. openssl on win2k
    By rzcodeman in forum Networking/Device Communication
    Replies: 4
    Last Post: 04-09-2004, 07:58 PM
  5. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM