LQ Newbie
Registered: May 2006
Posts: 3
Original Poster
Rep:
|
Hi,
I was trying to do some encryption decryption job using openssl's 3DES library. But while decrypting I am getting first 8 bytes
as junk. Here is a small program to demonstrate it. Can you please tell me what is wrong with me?
Do I need to do the encryption decryption job in 8byte blocks (using a loop)?
/* Linux extreem-machine 2.4.21-4.EL #1 Fri Oct 3 18:13:58 EDT 2003
i686 i686 i386 GNU/Linux*/
/* openssl-devel-0.9.7a-22.1 */
/* 3des.c - */
/*gcc -lssl 3des.c -o 3des*/
#include <openssl/des.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/*Parity bits must be set*/
DES_cblock cb1 = {0xAE, 0x23, 0x01, 0x5B, 0xB3, 0x25, 0x49, 0xA1};
DES_cblock cb2 = {0xAE, 0x23, 0x01, 0x5B, 0xB3, 0x25, 0x49, 0xA1};
DES_cblock cb3 = {0xAE, 0x23, 0x01, 0x5B, 0xB3, 0x25, 0x49, 0xA1};
DES_key_schedule ks1;
DES_key_schedule ks2;
DES_key_schedule ks3 ;
DES_cblock cblock = {0xAE, 0x23, 0x01, 0x5B, 0xB3, 0x25, 0x49, 0xA1};//{'3','D','E','S','-','K','E','Y'};
char string[]="Welcome to the new world __ thanks to the 3DES encryption"
"\nNot so good but not so BAD also"
"\nTHIS IS A PLAIN TEXT"
"\nThis is a plain text\n";
char cipher[1024];
char text[1024];
int crypto_len = 0;
DES_set_odd_parity(&cblock);
if(DES_set_key_checked(&cb1, &ks1) || DES_set_key_checked(&cb2, &ks2) || DES_set_key_checked(&cb3, &ks3))
{
printf("Key error, exiting ....\n");
return 1;
}
memset(text,'\0', 1024);
memset(cipher, '\0', 1024);
crypto_len= ((strlen(string) + 7)/ 8)*8;
DES_ede3_cbc_encrypt (string, cipher, strlen(string), &ks1,&ks2,&ks3, &cblock,1);
DES_ede3_cbc_encrypt (cipher, text, crypto_len, &ks1,&ks2,&ks3,&cblock,0);
//printf(cipher);
printf(text);
}
Any pointers on this willbe of great help
Thanks
Regards,
Rajesh
|