Hi,

I am very new at this. I want to encrypt a string with des3, change it to base64
and put it into a buffer. I have found code in the man pages which seems to
accomplish this execept that it gets written into a file, in this case results.dat.
I would really appreciate it if someone could indicate how to modify this code
so that it puts the output string into a buffer.


        Thanks,

         Dave

#include <stdlib.h>
#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/bio.h>

 int do_crypt(char * inbuffer, int len, FILE *out, int do_encrypt);

 int main(int argc,char * argv[])
 {
   FILE * outfile=fopen("results.dat","wb");
   char thebuffer[]="abcdefghijklmnopqrstuvwxyz0123456789";
   int thelen=strlen(thebuffer);
   int ret = do_crypt(thebuffer,thelen,outfile,1);
 }


int do_crypt(char * inbuf,int inlen, FILE *out, int do_encrypt) { /* Allow enough space in output buffer for additional block */ //inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; char outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int outlen; /* Bogus key and IV: we'd normally set these from * another source. */ unsigned char key[] = "0123456789"; unsigned char iv[] = "12345678"; EVP_CIPHER_CTX ctx; BIO *bio, *b64; /* Don't set key or IV because we will modify the parameters */ EVP_CIPHER_CTX_init(&ctx); EVP_CipherInit_ex(&ctx, EVP_des_ede3(), NULL, NULL, NULL, do_encrypt); EVP_CIPHER_CTX_set_key_length(&ctx, 10); /* We finished modifying parameters so now we can set key and IV */ EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);

    // write to a file
    if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
    {
      fprintf(stderr,"there was an error\n");
      /* Error */
      return 0;
    }
    b64 = BIO_new(BIO_f_base64());
    // Here we attach the file handle for results.dat
    bio = BIO_new_fp(out, BIO_NOCLOSE);
    bio = BIO_push(b64, bio);
    int retr=BIO_write(bio, outbuf,outlen);
    printf("bio_WRITE returns %d \n",retr);
    printf("outlen = %d \n",retr);
    BIO_flush(bio);
    BIO_free_all(bio);
          if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
    {
      /* Error */
      return 0;
    }
    EVP_CIPHER_CTX_cleanup(&ctx);
    return 1;
 }

--
David A. Fournier
Otter Research Ltd
PO Box 2040 Sidney, B.C. V8L 3S3
Canada
Voice/Fax 1-250-655-3364

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to