Hi Peter,
The extra string in o/p is due to error in coding. u r passing incorrect length 
in EVP_DecryptUpdate.


if (!(EVP_DecryptUpdate(&dctx, dec_outbuf, &decoutlen, enc_outbuf, 
output_buf_size)))

change above line to

if (!(EVP_DecryptUpdate(&dctx, dec_outbuf, &decoutlen, enc_outbuf, 
encoutlen)))


-Shafeek

It works vey good! Thank you!

Peter

 On Mon, May 2, 2011 at 3:09 PM, derleader mail   derlea...@abv.bg >  wrote:
    
Hi Peter,
Add padding for CBC mode encryption.
Or u can use CFB mode. EVB_bf_cfb()

-Shafeek

 Hi, 


 Thank you for the reply. I have edit the code.

Source Code:

//gcc blowfish2.c -L/usr/local/ssl/lib/ -lssl -lcrypto -Wall    

#include  
#include  
#include  
#include  
#include  

#define input_buf_size 1024
#define output_buf_size 1032

int main(int argc, char *argv[])
{








 if (argc !=2)







 {















 printf("Usage: test1  \n");















 exit(1);







 }






 







 char *string;








 int encoutlen, decoutlen, enctotallen, dectotallen;








 unsigned char iv[8];







 unsigned char
 password[16];







 unsigned char enc_outbuf[output_buf_size];







 char enc_inbuf[input_buf_size];







 unsigned char dec_outbuf[input_buf_size];







 char dec_inbuf[output_buf_size];







 EVP_CIPHER_CTX ectx;







 EVP_CIPHER_CTX dctx;








 /*








 * Begin the encode - decode








 *








 * Get our inputs and the random IV








 *








 */








 string = argv[1];








 RAND_bytes(iv, 8);







 RAND_bytes(password, 16);








 printf("Entering Encryption Stage:\n\n");







 printf("String to encrypt: %s\n\n", string);








 EVP_CIPHER_CTX_init(&ectx);


  






 EVP_EncryptInit(&ectx, EVP_bf_cfb(), password, iv); 








 bzero (&enc_inbuf, input_buf_size);








 if(!EVP_EncryptUpdate(&ectx, enc_outbuf, &encoutlen, string, 
strlen(string)))







 {















 printf("Error whilst EncryptUpdate\n");















 return 0;







 }








 if(!EVP_EncryptFinal(&ectx, enc_outbuf + encoutlen, &enctotallen))







 {















 printf("Error Whilst EncryptFinal\n");















 return 0;







 }








 encoutlen += enctotallen;








 printf("Encryption Successful\n\n");







 printf("Entering Decryption Stage\n\n");








 EVP_CIPHER_CTX_init(&dctx);
 






 EVP_DecryptInit(&dctx, EVP_bf_cfb(), password, iv); 








 bzero (&dec_inbuf, output_buf_size);







 bzero (&dec_outbuf, input_buf_size);








 if (!(EVP_DecryptUpdate(&dctx, dec_outbuf, &decoutlen, enc_outbuf, 
output_buf_size)))







 {















 printf("Error Whilst DecryptUpdate\n");















 return 0;







 }








 if (!(EVP_DecryptFinal(&dctx, dec_outbuf + decoutlen, &dectotallen)))







 {















 printf("Error Whilst DecryptFinal\n");















 ERR_print_errors_fp(stdout);















 return 0;







 }








 decoutlen += dectotallen;








 printf("Decryption Successful\n\n");








 printf("Decrypted String is: %s\n", dec_outbuf);








 return 0;

} 


 This is the output:

[root@localhost test]# ./a.out dcee
Entering Encryption Stage:

String to encrypt: dcee 

Encryption Successful

Entering Decryption Stage

 Decryption Successful

Decrypted String is: dcee�� s�� � h[j �l��ȥg�L^�aPB=�

everytime the string after "dcee" is diffrent. So I need padding.
Could you edit the source code in proper way. I have no idea how to add padding.

Regards
Peter


-----------------------------------------------------------------
  Дизайнерски обувки с до -70%. Регистрирай се и пазарувай.     

 

-----------------------------------------------------------------
Дизайнерски обувки с до -70%. Регистрирай се и пазарувай.
http://clk.tradedoubler.com/click?p=191500&a=1875689&g=19425934

Reply via email to