Hi ,
Thanks Nils for the tip,
I could make my code run, but the results are not what I expect :S
The encryption works fine, after trying to decrypt using the same
key. I dont find the initial string. I dont see where is my mistake.
I attached a compilable source file.
Regards,
Saber.
* On 20:33, Fri 23 Sep 05, Nils Larsch wrote:
> Saber Zrelli wrote:
> > Hi ,
> >
> > I am trying to use openssl's crypto library to encrypt packets
> > before transmitting them into a TCP connection.
> >
> > I have some difficulties on using DES funcions.
> >
> > below is the code I wrote, it compiles but core-dump occurs at line 48.
> >
> >
> > char *
> > 35 Encrypt(int Etype, char *Key, void *Msg)
> > 36 {
> > 37
> > 38 >_______char *CypherText;
> > 39 >_______char *dec;
> > 40 >_______int *n;
> > 41
> > 42 >_______DES_cblock>_____Key2;
> > 43 >_______DES_key_schedule *schedule;
> > 44
> > 45
> > 46 >_______memcpy(Key2, Key, 8);
> > 47 >_______DES_set_odd_parity(&Key2);
> > 48 >_______DES_set_key_checked(&Key2, schedule);
>
> the schedule pointer is uninitialzed => core dump, as schedule
> is used in DES_set_key_unchecked (called by DES_set_key_checked).
> Try to do something like:
> DES_key_schedule schedule;
> ...
> DES_set_key_checked(&Key2, &schedule);
>
> > 49 >_______DES_cfb64_encrypt((char *)Msg, CypherText, strlen(Msg),
> > schedule, &Key2, n, 1);
> > 50 >_______printf("Encrypted message : %s\n", CypherText);
> > 51 >_______DES_cfb64_encrypt(CypherText, dec, strlen(Msg), schedule,
> > &Key2, n, 0);
> > 52 >_______printf("decrypted message : %s\n", dec);
> >
> >
> > Any help links to sample code is appreciated.
>
> perhaps crypto/des/destest.c
>
> Cheers,
> Nils
> ______________________________________________________________________
> OpenSSL Project http://www.openssl.org
> User Support Mailing List [email protected]
> Automated List Manager [EMAIL PROTECTED]
--
Saber ZRELLI <[EMAIL PROTECTED]>
Japan Advanced Institute of Science and Technology
Center of Information Science
Shinoda Laboratory
url : http://www.jaist.ac.jp/~zrelli
gpg-id : 0x7119EA78
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <openssl/des.h>
char *
Decrypt(char *Key, void *Msg)
{
static char dec [30];
int n;
DES_cblock Key2;
DES_key_schedule schedule;
memcpy(Key2, Key, 8);
DES_set_odd_parity(&Key2);
DES_set_key_checked(&Key2, &schedule);
n = 0;
DES_cfb64_encrypt((char *)Msg, dec, strlen((char *)Msg), &schedule,
&Key2,
&n, DES_DECRYPT);
printf("decrypted message : %s\n", dec);
return (dec);
}
char *
Encrypt(char *Key, void *Msg)
{
char *Res;
int n;
DES_cblock Key2;
DES_key_schedule schedule;
memcpy(Key2, Key, 8);
DES_set_odd_parity(&Key2);
DES_set_key_checked(&Key2, &schedule);
DES_cfb64_encrypt((char *)Msg, Res, strlen((char *)Msg),
&schedule, &Key2, &n, DES_ENCRYPT);
printf("Encrypted message : %s\n", Res);
return (Res);
}
int
main()
{
char Msg [20];
char *res;
strcpy(Msg, "KKKKKKBB");
res = Encrypt("123456789778", Msg);
Decrypt("123456789778", res);
return (0);
}