Using libgcrypt1.2.0 for 3DES, ECB mode of encryption/decryption.

Madhavi.Gandavarapu at equifax.com Madhavi.Gandavarapu at equifax.com
Thu Dec 9 15:58:04 CET 2004





Hi,

I have a string that I am trying to encrypt using 3DES,ECB mode.  I have a
specific 24 byte key that I need to use to encrypt or decrypt.
I am not getting good encrypted string back, since I can't even decrypt what I
encrypted with the same key and same library.

Can you point me to what I am doing wrong here.

Here is the code segment.



unsigned char enc_key[24] =
{0x12,0x34,0x51,0x23,0x45,0x12,0x34,0x51,0x15,0x43,0x21,0x54,0x32,0x15,0x43,0x21,0x45,0x12,0x34,0x51,0x12,
0x34,0x51,0x23};


static int do_encrypt(unsigned char* dest,unsigned char* src)
{
   //GCRY_CIPHER_3DES is the algorithm type
   //GCRY_CIPHER_MODE_ECB is the algorithem mode
   //0(zero) is the flags value to send
   int retVal =0;
   gcry_cipher_hd_t hd;

   int keylen;
   int i =0,x=0;
   gcry_error_t err = 0;

   keylen = gcry_cipher_get_algo_keylen(GCRY_CIPHER_3DES);
   if (!keylen)
   {
//    fail ("algo %d, mode %d, gcry_cipher_get_algo_keylen failed\n", algo,
mode);
      logf(&bLog, LF_ERROR, "algo %s, mode %s, gcry_cipher_get_algo_keylen
failed","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB" );
      retVal = -1;
      return retVal;
   }
   if (keylen < 40 / 8 || keylen > 32)
   {
//    fail ("algo %d, mode %d, keylength problem (%d)\n", algo, mode, keylen);
      logf(&bLog, LF_ERROR, "algo %s, mode %s, keylength problem
(%d)","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB",keylen );
      retVal = -1;
      return retVal;
   }
   err = gcry_cipher_open (&hd, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_ECB, 0);
   if (err)
   {
//    fail ("algo %d, mode %d, grcy_open_cipher failed: %s\n", algo, mode,
gpg_strerror (err));
      logf(&bLog, LF_ERROR, "algo %s, mode %s, grcy_open_cipher failed:
%s","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB",gpg_strerror(err)
);
      return err;

 }
   err = gcry_cipher_setkey (hd, &enc_key, keylen);
   if (err)
   {
//    fail ("algo %d, mode %d, gcry_cipher_setkey failed: %s\n",  algo, mode,
gpg_strerror (err));
      logf(&bLog, LF_ERROR, "algo %s, mode %s, gcry_cipher_setkey failed:
%s","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB",gpg_strerror(er
r));
      gcry_cipher_close (hd);
      return err;
   }
   //err = gcry_cipher_encrypt (hd, dest, strlen(dest), NULL,strlen(dest));
   err = gcry_cipher_encrypt (hd, dest, strlen(dest), src, strlen(src));
   if (err)
   {
//    fail ("algo %d, mode %d, gcry_cipher_encrypt failed: %s\n", algo, mode,
gpg_strerror (err));
      logf(&bLog, LF_ERROR, "algo %s, mode %s, gcry_cipher_encrypt failed:
%s","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB",gpg_strerror(e
rr));
      gcry_cipher_close (hd);
      return err;
   }
   i =0; x=0;
   if((x = (strlen(dest)%bsize)))
   {
      x = (bsize - x);
      for(i = 0; i < x; i++)
         strcat(dest," ");
   }

   err = gcry_cipher_decrypt (hd, dest, strlen(dest), dest, strlen(dest));
   if (err)
   {
//    fail ("algo %d, mode %d, gcry_cipher_decrypt failed: %s\n", algo, mode,
gpg_strerror (err));
      logf(&bLog, LF_ERROR, "algo %s, mode %s, gcry_cipher_decrypt failed:
%s","GCRY_CIPHER_3DES" ,"GCRY_CIPHER_MODE_ECB",gpg_strerror(e
rr));
      gcry_cipher_close (hd);
      return err;
   }


   gcry_cipher_close (hd);






Thanks and Regards
Madhavi
This message contains information from Equifax Inc. which may be confidential and
privileged.  If you are not an intended recipient, please refrain from any
disclosure, copying, distribution or use of this information and note that such
actions are prohibited.  If you have received this transmission in error, please
notify by e-mail postmaster at equifax.com.




More information about the Gnupg-users mailing list