Hi all,

I met a problem that while I try to decrypt the cipher string 
of "230 User marco logged in.". it can't decrypt the string 
properly.  But I don't know whether it is my problem or what.  
But the thing is, if I decrypt with other string, everything 
works pretty fine.  And the following code show the program, 
which demonstrate the problem.  And if you want to see the 
error, please compile with the option of -DTESTING.


Code :
====

/*
 *  'blowfish_enc.c' contains some routines to do the 
encryption, decryption
 *   by using blowfish algoithm.
 *                                              
 *
 *  Author : Wai Wa Marco, Lam
 *  Title of Dissertation : Secure Socket Service
 *  Supervisor : Dr. Faye Mitchell
 *  Oxford Brookes University
 */

#include <stdio.h>
#include "../include/blowfish_enc.h"

/*
 *  Init the cipher context as encryption cipher by using 
blowfish algorithm and
 *  CBC (Cipher Block Chaining) mode, using key and iv 
(initialization vector).
 */
void init_enc(EVP_CIPHER_CTX *context, char key[], char iv[])
{
  EVP_EncryptInit(context, EVP_bf_cbc(), key, iv);
}


/*
 *  Init the cipher context as decryption cipher by using 
blowfish algorithm and
 *  CBC (Cipher Block Chaining) mode, using key and iv 
(initialization vector).
 */
void init_dec(EVP_CIPHER_CTX *context, char key[], char iv[])
{
  EVP_DecryptInit(context, EVP_bf_cbc(), key, iv);
}


/*
 *  Encrypt the 'plain_text' data with the encryption cipher 
context, and
 *  place the output to 'enc_buffer'
 *
 *  Technical Note:
 *  ---------------
 *  The reason that I pass 'context' as copy instead of pass it 
as reference is
 *  to avoid the variable inside 'context' is changed (In the 
sense of object-
 *  oriented, it means the property of 'context' is changed).  
Because after the
 *  function call of EVP_EncryptFinal, it would change 
'context' to be unusable.
 *  So I let it to pass by copy.  Perhaps you may think it 
would suffer the penalty
 *  of efficiency, but compare to setting a clear context 
everytime we encrypt the
 *  buffer, it's acceptable(better).
 */
int encrypt_pkt(EVP_CIPHER_CTX context, unsigned char 
*enc_buffer, unsigned char *plain_text, int length)
{
  unsigned char enc_tmp_buffer[1024];
  int enc_tmp_length = 0, enc_length = 0;

  EVP_EncryptUpdate(&context, enc_tmp_buffer, &enc_tmp_length, 
plain_text, length);
  strncpy(enc_buffer, enc_tmp_buffer, enc_tmp_length);
  enc_length += enc_tmp_length;

  EVP_EncryptFinal(&context, enc_tmp_buffer, &enc_tmp_length);
  enc_length += enc_tmp_length;

  strncat(enc_buffer, enc_tmp_buffer, enc_tmp_length);

  return enc_length;
}


/*
 *  Decrypt the 'cipher_text' data with the decryption cipher 
context, and
 *  place the output to 'dec_buffer'
 *
 *  Technical Note:
 *  ---------------
 *  The reason that I pass 'context' as copy instead of pass it 
as reference is
 *  to avoid the variable inside 'context' is changed (In the 
sense of object-
 *  oriented, it means the property of 'context' is changed).  
Because after the
 *  function call of EVP_DecryptFinal, it would change 
'context' to be unusable.
 *  So I let it to pass by copy.  Perhaps you may think it 
would suffer the penalty
 *  of efficiency, but compare to setting a clear context 
everytime we decrypt the
 *  buffer, it's fine. (same as above)
 */
int decrypt_pkt(EVP_CIPHER_CTX context, unsigned char 
*dec_buffer, unsigned char *cipher_text, int length)
{
  unsigned char dec_tmp_buffer[1024];
  int dec_tmp_length = 0, dec_length = 0;

  if (!EVP_DecryptUpdate(&context, dec_tmp_buffer, 
&dec_tmp_length, cipher_text, length))
    return -1;

  strncpy(dec_buffer, dec_tmp_buffer, dec_tmp_length);
  dec_length += dec_tmp_length;

  if (!EVP_DecryptFinal(&context, dec_tmp_buffer, 
&dec_tmp_length))
    return -1;
  strncat(dec_buffer, dec_tmp_buffer, dec_tmp_length);
  dec_length += dec_tmp_length;

  return dec_length;
}


/*
 *  Clean up the cipher context.
 */
void clean_cipher_context(EVP_CIPHER_CTX *context)
{
  EVP_CIPHER_CTX_cleanup(context);
}


/*
 *  Test the routine of blowfish and try to encrypt and decrypt
 *  the data.
 */
#ifdef TESTING
int main(int argc, char **argv)
{
  unsigned char key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15};
  unsigned char iv[] =  {1, 2, 3, 4, 5, 6, 7, 8};
  EVP_CIPHER_CTX e_context, d_context;
  unsigned char enc_buffer[1024];
  unsigned char dec_buffer[1024];
  int enc_length = 0, dec_length = 0;
  unsigned char plain_text[] = "230 User marco logged in.";
  //  unsigned char plain_text[] = "Please decrypt this 
message, and finish the dissertation as soon as possible, 
man..";
  bzero(enc_buffer, strlen(enc_buffer));
  bzero(dec_buffer, strlen(dec_buffer));

  init_enc(&e_context, key, iv);
  init_dec(&d_context, key, iv);

  enc_length = encrypt_pkt(e_context, enc_buffer, plain_text, 
strlen(plain_text));

  printf("The encrypt data : %s\n", enc_buffer);
  printf("The encrypt size : %d\n", strlen(enc_buffer));
  printf("Actual encrypted length : %d\n", enc_length);

  if ((dec_length = decrypt_pkt(d_context, dec_buffer, 
enc_buffer, enc_length)) == -1)
    fprintf(stderr, "Error emerge when decrypting data..");

  clean_cipher_context(&e_context);
  clean_cipher_context(&d_context);

  printf("The decrypt message : %s\n", dec_buffer);
  printf("The decrypt size : %d\n", strlen(dec_buffer));
  printf("Actual decrypted length : %d\n", dec_length);
  return 0;
}

#endif


/*
 *  It will use the same encryption cipher context to encrypt 
different 
 *  data, and decrypt the encrypted data back to the plain text 
using
 *  decryption cipher context.
 */
#ifdef ADV_TESTING
int main(int argc, char **argv)
{
  unsigned char key[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15};
  unsigned char iv[] =  {1, 2, 3, 4, 5, 6, 7, 8};
  EVP_CIPHER_CTX e_context, d_context;
  unsigned char enc_buffer[1024], enc_buffer2[1024];
  unsigned char dec_buffer[1024], dec_buffer2[1024];
  int enc_length = 0, enc_length2 = 0, dec_length = 0, 
dec_length2 = 0;

  char plain_text[] = "Please decrypt this message, and finish 
the dissertation as soon as possible, man..";
  char plain_text2[] = "Testing, testing,.... try to make the 
program as finest as possible.....^.^";
  bzero(enc_buffer, strlen(enc_buffer));
  bzero(dec_buffer, strlen(dec_buffer));

  init_enc(&e_context, key, iv);
  init_dec(&d_context, key, iv);

  enc_length = encrypt_pkt(e_context, enc_buffer, plain_text, 
strlen(plain_text));

  printf("The encrypt data : %s\n", enc_buffer);
  printf("The encrypt size : %d\n", strlen(enc_buffer));
  printf("Actual encrypted length : %d\n", enc_length);

  enc_length2 = encrypt_pkt(e_context, enc_buffer2, 
plain_text2, strlen(plain_text2));

  printf("The encrypt data : %s\n", enc_buffer2);
  printf("The encrypt size : %d\n", strlen(enc_buffer2));
  printf("Actual encrypted length : %d\n", enc_length2);


  if ((dec_length = decrypt_pkt(d_context, dec_buffer, 
enc_buffer, enc_length)) == -1)
    fprintf(stderr, "Error emerge when decrypting data..");

  printf("The decrypt message : %s\n", dec_buffer);
  printf("The decrypt size : %d\n", strlen(dec_buffer));
  printf("Actual decrypted length : %d\n", dec_length);

  if((dec_length2 = decrypt_pkt(d_context, dec_buffer2, 
enc_buffer2, enc_length2)) == -1)
    fprintf(stderr, "Error emerge when decrypting data..");

  printf("The decrypt message : %s\n", dec_buffer2);
  printf("The decrypt size : %d\n", strlen(dec_buffer2));
  printf("Actual decrypted length : %d\n", dec_length2);

  clean_cipher_context(&e_context);
  clean_cipher_context(&d_context);

  return 0;
}

#endif


Honestly, I'm the newbie to do the encryption, decryption 
stuff, and if I made any stupid mistakes, or it's the bug in 
libcrypto.a.  Could you kindly "cc" to me, because I havne't 
joined the newgroup of openssl-dev


Thanks so much.


Very respectfully,
marco.



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

Reply via email to