Hi, 

I'm trying to read a plaintext file from disk, encrypt
it, and write the encrypted info to disk. [And
eventually do the reversee too.] John Viega's OpenSSL
book listed the a snippet for assembling BIOs. In his
version, the code is reading from a buffer and writing
to a file.

In order to add the capability for reading from file,
would I need to:

(1) create a buffered file BIO for reading 
BIO* filein = BIO_new_file(filein_name, "r");

(2) and create a buffering filter BIO for reading?
bufferin = BIO_new (BIO_f_buffer ());

(3) chain them all together in the proper sequence

(4) start the whole process by using BIO_read()
instead of the for loop that reads from the buffer? 

Any pointers would be greatly appreciated! If there
are easier way to accomplish this I would greatly
appreciate the feedback. Thank you! 

Phoung


Code snippet from John Viega's book:
-----

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_des_ede3_cbc (), 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;
        }
    }
  /* Ensure all of our data is pushed all the way to
the file */
  BIO_flush (cipher);
  /* We now need to free the BIO chain. A call to
BIO_free_all(cipher) would
     accomplish this, but we'll first remove b64 from
the chain for
     demonstration purposes. */
  BIO_pop (b64);
  /* At this point the b64 BIO is isolated and the
chain is cipher-buffer-file.
     The following frees all of that memory */
  BIO_free (b64);
  BIO_free_all (cipher);
}


                
__________________________________ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to