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
#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
#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)
#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