Hi again.

For those who showed interest about this class, here's its new version,
using dynamic memory, with some bugs corrected and better commented. Now
it seems to be working very well even for larger buffers. Please, let me
know if you like it or have any suggestions for improving it.

Thanks,
Eduardo ([EMAIL PROTECTED])

Eduardo Sztokbant wrote:
> 
> Hi there.
> 
> I've been working on this C++ class for about a week and now I think it
> is quite ready. It stores a blowfish encrypted string base64 encoded and
> may be very useful for storing passwords into files. I promise I won't
> talk about it anymore if people show no interest. Otherwise, if you like
> it or have any suggestions for improving it, please let me know.
> 
> Bye,
> Eduardo
>
/*
 * EncPass Class
 * Class author: Eduardo Sztokbant ([EMAIL PROTECTED]).
 * Last Modification: July 11th., 2001.
 * (comments optimized for using DOC++)
 */

#include <cstring>       // strlen, bzero
#include <openssl/evp.h> // Enc*(), Dec*()

/** Stores a base64 encoded blowfish encrypted buffer. Very useful to store small 
buffers, like passwords, into files. It also seems to be working very well even for 
larger buffers. This document was last updated at \today (GMT -3).
*/
class EncPass {
 public:
  EncPass(unsigned char key[16], unsigned char iv[8], char *ascbuf);
  EncPass(unsigned char *b64buf);
  unsigned char *getbuf(void);
  char *retrieve(unsigned char key[16], unsigned char iv[8]);
  EncPass::~EncPass();

 private:
  unsigned char *b64buf;
  int buffer_size;

  int encrypt(char *ascbuf, unsigned char iv[8], unsigned char key[16], unsigned char 
*bfbuf);
  int encode(unsigned char *bfbuf, int outlen);
  int decode(unsigned char *bfbuf);
  int decrypt(unsigned char *bfbuf, int total, char *ascbuf, unsigned char key[16], 
unsigned char iv[8]);
};

/******************
 * PUBLIC METHODS *
 ******************/

/** 1st constructor, used for encrypting/encoding.
    #key# is the 16 bytes blowfish encryption key, #iv# is the 8 bytes random number 
initialization vector, #ascbuf# is is the buffer to be encrypted/encoded.
*/
EncPass::EncPass(unsigned char key[16], unsigned char iv[8], char *ascbuf) {
  // Allocates enough amount of memory to be used by the buffer.
  this->buffer_size = (2 * strlen(ascbuf)) > 32 ? 2 * strlen(ascbuf) : 32;

  this->b64buf = (unsigned char *) malloc(this->buffer_size);
  unsigned char *bfbuf = (unsigned char *) malloc(this->buffer_size);

  int outlen = EncPass::encrypt(ascbuf, iv, key, bfbuf);
  int total = EncPass::encode(bfbuf, outlen);

  bzero(bfbuf,buffer_size);
  free(bfbuf);
}

/** 2nd constructor, used for decoding/decrypting.
    #b64buf# is an already base64 encoded blowfish encrypted buffer.
*/
EncPass::EncPass(unsigned char *b64buf) {
  // Allocates enough amount of memory to be used by the buffer.
  this->buffer_size = strlen((char *)b64buf);

  this->b64buf = (unsigned char *) malloc(this->buffer_size);
  for(int i=0; i<this->buffer_size; i++)
    this->b64buf[i] = b64buf[i];
}

/** Returns buffer #b64buf#.
*/
unsigned char *EncPass::getbuf(void) {
  return this->b64buf;
}

/** Returns decoded/decrypted string. #key# is the blowfish encryption key and #iv# is 
the random number initialization vector used to decrypt #b64buf#.
*/
char *EncPass::retrieve(unsigned char key[16], unsigned char iv[8]) {
  unsigned char *bfbuf = (unsigned char *) malloc(this->buffer_size);
  char *ascbuf = (char *) malloc(this->buffer_size);

  // This line was necessary when I wasn't using dynamic memory. Now I think it isn't 
necessary anymore.
  //bzero(ascbuf,this->buffer_size);

  int total = decode(bfbuf);
  int outlen = decrypt(bfbuf, total, ascbuf, key, iv);

  return ascbuf;
}

EncPass::~EncPass() {
  // For security reasons, you should also run bzero+free manually on your ascbuf too.

  bzero(b64buf,buffer_size);
  free(b64buf);
}

/*******************
 * PRIVATE METHODS *
 *******************/

// Blowfish encrypts ascbuf into bfbuf.
int EncPass::encrypt(char *ascbuf, unsigned char iv[8], unsigned char key[16], 
unsigned char *bfbuf) {
  EVP_CIPHER_CTX ctx;
  int outlen, tmplen;

  EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);
  if(!EVP_EncryptUpdate(&ctx, bfbuf, &outlen, (unsigned char *)ascbuf, 
strlen(ascbuf))) {
    return 0;
  }
  if(!EVP_EncryptFinal(&ctx, bfbuf + outlen, &tmplen)) {
    return 0;
  }
  outlen += tmplen;

  return outlen;
}

// Base64 encodes bfbuf into b64buf.
int EncPass::encode(unsigned char *bfbuf, int outlen) {
  EVP_ENCODE_CTX ectx;
  int len, total = 0;

  EVP_EncodeInit(&ectx);
  EVP_EncodeUpdate(&ectx, this->b64buf, &len, bfbuf, outlen);
  total += len;
  EVP_EncodeFinal(&ectx, this->b64buf + len, &len);
  total += len;

  return total;
}

// Base64 decodification.
int EncPass::decode(unsigned char *bfbuf) {
  EVP_ENCODE_CTX ectx;
  int len, total = 0;

  EVP_DecodeInit(&ectx);
  EVP_DecodeUpdate(&ectx, bfbuf, &len, this->b64buf, strlen((const char 
*)this->b64buf));
  total += len;
  EVP_DecodeFinal(&ectx, bfbuf + len, &len);
  total += len;

  return total;
}

// Blowfish decryption.
int EncPass::decrypt(unsigned char *bfbuf, int total, char *ascbuf, unsigned char 
key[16], unsigned char iv[8]) {
  EVP_CIPHER_CTX ctx;
  int outlen, tmplen;
  
  EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv);
  if(!EVP_DecryptUpdate(&ctx, (unsigned char *)ascbuf, &outlen, bfbuf, total)) {
    return 0;
  }
  if(!EVP_DecryptFinal(&ctx, (unsigned char *)ascbuf + outlen, &tmplen)) {
    return 0;
  }

  EVP_CIPHER_CTX_cleanup(&ctx);

  return outlen;
}




Reply via email to