Click here to Skip to main content
14,421,696 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello coders,

I am trying to convert few DES encryption functions from C++ NewcamD project to .NET but I need some help as it is using specific DES convertion and I want test it properly. C++ uses extra OPENSSL library with Triple DES encryption. I don't need any sockets for testing but I need encryption/decryption testing so I've tried to create simple encryption/decryption project in C++ but it fails on compilation with some errors:


Error 5 error LNK2019: unresolved external symbol _DES_set_odd_parity referenced in function "private: void __thiscall cTripleDes::SetOddParity(unsigned char *)" (?SetOddParity@cTripleDes@@AAEXPAE@Z) cTripleDes.obj NewCamD_test

Error 6 error LNK2019: unresolved external symbol _DES_key_sched referenced in function "protected: void __thiscall cTripleDes::ScheduleKey(void)" (?ScheduleKey@cTripleDes@@IAEXXZ) cTripleDes.obj NewCamD_test

Error 7 error LNK2019: unresolved external symbol _DES_random_key referenced in function "protected: int __thiscall cTripleDes::PadMessage(unsigned char *,int)" (?PadMessage@cTripleDes@@IAEHPAEH@Z) cTripleDes.obj NewCamD_test

Error 8 error LNK2019: unresolved external symbol ___security_cookie referenced in function "protected: int __thiscall cTripleDes::PadMessage(unsigned char *,int)" (?PadMessage@cTripleDes@@IAEHPAEH@Z) cTripleDes.obj NewCamD_test

Error 9 error LNK2019: unresolved external symbol @__security_check_cookie@4 referenced in function "protected: int __thiscall cTripleDes::PadMessage(unsigned char *,int)" (?PadMessage@cTripleDes@@IAEHPAEH@Z) cTripleDes.obj NewCamD_test

Error 10 error LNK2019: unresolved external symbol __imp___time64 referenced in function _time cTripleDes.obj NewCamD_test

Error 11 error LNK2019: unresolved external symbol _DES_ede3_cbc_encrypt referenced in function "protected: unsigned char const * __thiscall cTripleDes::Encrypt(unsigned char const *,int,unsigned char *)" (?Encrypt@cTripleDes@@IAEPBEPBEHPAE@Z) cTripleDes.obj NewCamD_test

Error 12 fatal error LNK1120: 7 unresolved externals C:\PROJECTS\C++\NewCamD_test\Debug\NewCamD_test.exe NewCamD_test



Does anyone could help me?

Thanks




MY C++ (ver 9.0) CODE - HEADER FILES:
cc.hh
//extern "C" {
//    #include "des.h"
//}


#define L_CC          6
#define L_CC_CORE     LCLASS(L_CC,0x2)
#define L_CC_LOGIN    LCLASS(L_CC,0x4)
#define L_CC_ECM      LCLASS(L_CC,0x8)
#define L_CC_EMM      LCLASS(L_CC,0x10)
#define L_CC_CAMD     LCLASS(L_CC,0x20)
#define L_CC_CAMD35   LCLASS(L_CC,0x40)
#define L_CC_CAMDEXTR LCLASS(L_CC,0x80)
#define L_CC_RDGD     LCLASS(L_CC,0x100)
#define L_CC_NEWCAMD  LCLASS(L_CC,0x200)
#define L_CC_GBOX     LCLASS(L_CC,0x400)

#define L_CC_ALL      LALL(L_CC_GBOX)


mish.h
unsigned char XorSum(const unsigned char *mem, int len);


stdafx.h
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>


targetver.h
#pragma once

// The following macros define the minimum required platform.  The minimum required platform
// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run 
// your application.  The macros work by enabling all features available on platform versions up to and 
// including the version specified.

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef _WIN32_WINNT            // Specifies that the minimum required platform is Windows Vista.
#define _WIN32_WINNT 0x0600     // Change this to the appropriate value to target other versions of Windows.
#endif


SOURCE FILES:
cTripleDes.cpp
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <time.h>
#include <process.h>
#include <stdlib.h>
#include <malloc.h>
//#include "cTripleDes.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <crypt.h>
//#include <byteswap.h>

//#include <vdr/thread.h>

#include "cc.h"
//#include "network.h"
#include "misc.h"
//#include "parse.h"

#include <openssl/des.h>

#define CWS_NETMSGSIZE 240

// -- cTripleDes ---------------------------------------------------------------

class cTripleDes {
private:
  DES_key_schedule ks1;
  DES_key_schedule ks2;
  //
  void SetOddParity(unsigned char *key); // key must be 16 bytes!
protected:
  unsigned char desKey[16];
  //
  void ScheduleKey(void);
  int PadMessage(unsigned char *data, int len);
  void Expand(unsigned char *expanded, const unsigned char *normal); // 14 byte key input, 16 byte expanded output
  void Decrypt(unsigned char *data, int len);
  const unsigned char *Encrypt(const unsigned char *data, int len, unsigned char *crypt);
  };

void cTripleDes::SetOddParity(unsigned char *key)
{
  DES_set_odd_parity((DES_cblock *)&key[0]); // set odd parity on both keys
  DES_set_odd_parity((DES_cblock *)&key[8]); //
}

void cTripleDes::ScheduleKey(void)
{
  DES_key_sched((DES_cblock *)&desKey[0],&ks1);
  DES_key_sched((DES_cblock *)&desKey[8],&ks2);
}

void cTripleDes::Expand(unsigned char *expand, const unsigned char *normal)
{
  expand[0]  =   normal[0] & 0xfe;
  expand[1]  = ((normal[0] << 7) | (normal[1] >> 1)) & 0xfe;
  expand[2]  = ((normal[1] << 6) | (normal[2] >> 2)) & 0xfe;
  expand[3]  = ((normal[2] << 5) | (normal[3] >> 3)) & 0xfe;
  expand[4]  = ((normal[3] << 4) | (normal[4] >> 4)) & 0xfe;
  expand[5]  = ((normal[4] << 3) | (normal[5] >> 5)) & 0xfe;
  expand[6]  = ((normal[5] << 2) | (normal[6] >> 6)) & 0xfe;
  expand[7]  =   normal[6] << 1;

  expand[8]  =   normal[7] & 0xfe;

  expand[9]  = ((normal[7] << 7)  | (normal[8] >> 1)) & 0xfe;
  expand[10] = ((normal[8] << 6)  | (normal[9] >> 2)) & 0xfe;
  expand[11] = ((normal[9] << 5)  | (normal[10] >> 3)) & 0xfe;
  expand[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
  expand[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
  expand[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
  expand[15] =   normal[13] << 1;

  SetOddParity(expand);

}



int cTripleDes::PadMessage(unsigned char *data, int len)

{

  DES_cblock padBytes;

  unsigned char noPadBytes;



  noPadBytes = (8 - ((len - 1) % 8)) % 8;

  if(len+noPadBytes+1 >= CWS_NETMSGSIZE-8) {
    /*printf(L_CC_NEWCAMD,"message overflow in cTripleDes::PadMessage");*/
      printf("message overflow in cTripleDes::PadMessage");
    return -1;
    }

  srand(time(NULL)); // make sure the random generator is initialized
  DES_random_key((DES_cblock *)padBytes);
  memcpy(data+len,padBytes,noPadBytes); len+=noPadBytes;
  data[len]=XorSum(data+2,len-2);
  return len+1;
}

const unsigned char *cTripleDes::Encrypt(const unsigned char *data, int len, unsigned char *crypt)
{
  DES_cblock ivec;
  DES_random_key((DES_cblock *)ivec);
  memcpy(crypt+len,ivec,sizeof(ivec));
  DES_ede2_cbc_encrypt(data+2,crypt+2,len-2,&ks1,&ks2,(DES_cblock *)ivec,DES_ENCRYPT);
  return crypt;
}

void cTripleDes::Decrypt(unsigned char *data, int len)
{
  if((len-2) % 8 || (len-2)<16) {

    //printf(L_CC_NEWCAMD,"warning: encrypted data size mismatch");

      printf("warning: encrypted data size mismatch");

    return;

    }

  DES_cblock ivec;

  len-=sizeof(ivec); memcpy(ivec, data+len, sizeof(ivec));

  DES_ede2_cbc_encrypt(data+2,data+2,len-2,&ks1,&ks2,(DES_cblock *)ivec,DES_DECRYPT);

}





//unsigned char XorSum(const unsigned char *mem, int len)

//{

//  unsigned char cs=0;

//  while(len>0) { cs ^= *mem++; len--; }
//  return cs;
//}


misc.cpp
#include "stdafx.h"
#include "misc.h"

unsigned char XorSum(const unsigned char *mem, int len)
{
  unsigned char cs=0;
  while(len>0) { cs ^= *mem++; len--; }
  return cs;
}


stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// NewCamD_test.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file



NewcamD_Test.cpp (main console file)
// NewCamD_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int hr;
    printf("\nCannot put author info: %x\n", hr );
    system("pause");
    return 0;
}



Requirements:
http://207.44.152.197/vdr-sc-0.8.5.tar.gz[link];
OpenSSL (http://www.openssl.org/[link])


Thanks David,
The problem appears in ActivePerl which I am unable to configure for OpenSSL. But I did found lib files on provided link and error list now seems to be narrowed down after adding them to the project using Option 1:


Error 4 error LNK2019: unresolved external symbol ___security_cookie referenced in function "protected: unsigned char const * __thiscall cTripleDes::Encrypt(unsigned char const *,int,unsigned char *)" (?Encrypt@cTripleDes@@IAEPBEPBEHPAE@Z) cTripleDes.obj NewCamD_test

Error 5 error LNK2019: unresolved external symbol @__security_check_cookie@4 referenced in function "protected: unsigned char const * __thiscall cTripleDes::Encrypt(unsigned char const *,int,unsigned char *)" (?Encrypt@cTripleDes@@IAEPBEPBEHPAE@Z) cTripleDes.obj NewCamD_test

Error 6 fatal error LNK1120: 2 unresolved externals C:\PROJECTS\C++\NewCamD_test\Debug\NewCamD_test.exe 1 NewCamD_test
Posted
Updated 28-Dec-11 7:22am
v3
Comments
Randor 28-Dec-11 17:18pm
   
Hi,

I am happy to see that you have almost fixed the linker error. I also recognize your second problem:

unresolved external symbol ___security_cookie referenced in function

That is the security stack cookies... it looks like the OpenSSL library was compiled with /GS (Buffer Security Check)
http://msdn.microsoft.com/en-us/library/8dbf701c(v=vs.80).aspx

Modify your project to include /GS and everything should be fine.

If you are using an outdated compiler without support for /GS stack cookies... then you will need to compile OpenSSL yourself.

Best Wishes,
-David Delaune
Tomazas77 29-Dec-11 4:41am
   
Thanks David,

This was other way round. Setting /GS- flag solved my issues.
Tomazas77 29-Dec-11 16:24pm
   
David,

would you be able to tell me why I get these string errors after recompiling (added "using namespace std"):

Error 3 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<char,struct std::char_traits<char="">,class std::allocator<char> >::basic_string<char,struct std::char_traits<char="">,class std::allocator<char> >(char const *)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) referenced in function "public: virtual void __thiscall Test::Start(void)" (?Start@Test@@UAEXXZ) NewCamD_test.obj NewCamD_test
Error 4 error LNK2019: unresolved external symbol ___CxxFrameHandler3 referenced in function __ehhandler$?prn@Test@@UAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAE@Z NewCamD_test.obj NewCamD_test
Error 5 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::basic_ostream<char,struct std::char_traits<char=""> >::_Osfx(void)" (__imp_?_Osfx@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEXXZ) referenced in function "public: __thiscall std::basic_ostream<char,struct std::char_traits<char=""> >::sentry::~sentry(void)" (??1sentry@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAE@XZ) NewCamD_test.obj NewCamD_test
Error 6 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::basic_streambuf<char,struct std::char_traits<char=""> >::_Lock(void)" (__imp_?_Lock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEXXZ) referenced in function "public: __thiscall std::basic_ostream<char,struct std::char_traits<char=""> >::_Sentry_base::_Sentry_base(class std::basic_ostream<char,struct std::char_traits<char=""> > &)" (??0_Sentry_base@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAE@AAV12@@Z) NewCamD_test.obj NewCamD_test
Error 7 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall std::basic_streambuf<char,struct std::char_traits<char=""> >::_Unlock(void)" (__imp_?_Unlock@?$basic_streambuf@DU?$char_traits@D@std@@@std@@QAEXXZ) referenced in function "public: __thiscall std::basic_ostream<char,struct std::char_traits<char=""> >::_Sentry_base::~_Sentry_base(void)" (??1_Sentry_base@?$basic_ostream@DU?$char_traits@D@std@@@std@@QAE@XZ) NewCamD_test.obj NewCamD_test

Thanks
Randor 29-Dec-11 19:44pm
   
Hi Tomazas,

It appears that the linker is unable to find the dynamic-run-time version of the std::string inside your Test:: class and then std::iostream inside your ::sentry class.

Then check to see if you have /NODEFAULTLIB enabled... if so... remove that compile flag. The error message implies that you are linking with the dynamic version of the C++ Runtime. As I told you before... make sure that *everything*... is using the same run-time.

http://msdn.microsoft.com/en-us/library/2kzt1wy3(v=vs.80).aspx
Tomazas77 30-Dec-11 11:57am
   
Hi David,
Thanks very much for explanation. I completely forgot that some time ago I've added VC6 library path in VS2008 options which was priority.
Removing this path has solved my problems. And changing Runtime library /MDd to something else cause more problems.

Thanks

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Are you sure that you are linking with the OpenSSL library? Those unresolved external symbols are OpenSSL functions...

Option 1:
You need to add the OpenSSL libs to your project linker dependencies:
Linker -> Input -> Additional Dependencies

Option 2:
Alternatively you could add this to your cTripleDes.cpp file:
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "libeay32.lib")

If you use option 2 make sure that you add those lib files to the directory in your lib path. You should probably use option 1... so you learn how to correctly setup lib paths within your projects.

If you do not have the OpenSSL libraries then download them: http://www.openssl.org/related/binaries.html[^]

Also... make sure that you use the correct Run-Time Library[^]. The libs downloaded from the OpenSSL site will contain a library for each run-time.

Best Wishes,
-David Delaune
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100