在实际的工程应用中,经常会遇到一些Seed2Key的算法问题,而且为了安全保密,这个算法的源码又不想公开,我们可以将其打包成DLL,然后在CAPL里面直接调用DLL即可。
CANoe的开发公司Vector 已经提供了DLL生成的工程模板,以Win7系统, CANoe 8.5为例,工程模板路径如下:
C:\Users\Public\Documents\Vector\CANoe\8.5\CANoe Sample Configurations\Programming\CAPLdll
打开CAPLdll\Includes\cdll.h 180行
// New struct supporting up to 64 parameters
typedef struct CAPL_DLL_INFO4{
char cdlName[MAX_CDL_NAME2]; // name of the function
CAPL_FARCALL adr; // address of the function
const char* categoryName; // name of function category
const char* hintText; // hint text for function selection
char resultType; // type of func result
int parCount; // no. of parameters
char parTypes[MAXCAPLFUNCPARS_8_1]; // types of parameters
unsigned char array[MAXCAPLFUNCPARS_8_1]; // depth of array. Without: \0
const char* parNames[MAXCAPLFUNCPARS_8_1]; // names of parameters
} CAPL_DLL_INFO4;
打开CAPLdll\Sources\capldll.cpp
// Seed2Key
unsigned long CAPLEXPORT far CAPLPASCAL appSeedKeyCals(unsigned long Seed,const unsigned long EncryptConstant)
{
unsigned long Key ;
Key=Seed+1; /*Only for Demo*/
return( Key );
}
插入Seed2Key到接口函数table.
{"dllSeedKeyCals", (CAPL_FARCALL)appSeedKeyCals, "CAPL_DLL","This function will calculation the key . The return value is the result",'D', 2, "DD", "", {"Seed","EncryptConstant"}},
更新后的table如下:
// ============================================================================
// CAPL_DLL_INFO_LIST : list of exported functions
// The first field is predefined and mustn't be changed!
// The list has to end with a {0,0} entry!
// New struct supporting function names with up to 50 characters
// ============================================================================
CAPL_DLL_INFO4 table[] = {
{CDLL_VERSION_NAME, (CAPL_FARCALL)CDLL_VERSION, "", "", CAPL_DLL_CDECL, 0xabcd, CDLL_EXPORT },
{"dllInit", (CAPL_FARCALL)appInit, "CAPL_DLL","This function will initialize all callback functions in the CAPLDLL",'V', 1, "D", "", {"handle"}},
{"dllEnd", (CAPL_FARCALL)appEnd, "CAPL_DLL","This function will release the CAPL function handle in the CAPLDLL",'V', 1, "D", "", {"handle"}},
{"dllSetValue", (CAPL_FARCALL)appSetValue, "CAPL_DLL","This function will call a callback functions",'L', 2, "DL", "", {"handle","x"}},
{"dllReadData", (CAPL_FARCALL)appReadData, "CAPL_DLL","This function will call a callback functions",'L', 2, "DL", "", {"handle","x"}},
{"dllPut", (CAPL_FARCALL)appPut, "CAPL_DLL","This function will save data from CAPL to DLL memory",'V', 1, "D", "", {"x"}},
{"dllGet", (CAPL_FARCALL)appGet, "CAPL_DLL","This function will read data from DLL memory to CAPL",'D', 0, "", "", {""}},
{"dllVoid", (CAPL_FARCALL)voidFct, "CAPL_DLL","This function will overwrite DLL memory from CAPL without parameter",'V', 0, "", "", {""}},
{"dllPutDataOnePar", (CAPL_FARCALL)appPutDataOnePar, "CAPL_DLL","This function will put data from CAPL array to DLL",'V', 1, "B", "\001", {"datablock"}},
{"dllGetDataOnePar", (CAPL_FARCALL)appGetDataOnePar, "CAPL_DLL","This function will get data from DLL into CAPL memory",'V', 1, "B", "\001", {"datablock"}},
{"dllPutDataTwoPars", (CAPL_FARCALL)appPutDataTwoPars,"CAPL_DLL","This function will put two datas from CAPL array to DLL",'V', 2, "DB", "\000\001", {"noOfBytes","datablock"}},// number of pars in octal format
{"dllGetDataTwoPars", (CAPL_FARCALL)appGetDataTwoPars,"CAPL_DLL","This function will get two datas from DLL into CAPL memory",'V', 2, "DB", "\000\001", {"noOfBytes","datablock"}},
{"dllAdd", (CAPL_FARCALL)appAdd, "CAPL_DLL","This function will add two values. The return value is the result",'L', 2, "LL", "", {"x","y"}},
{"dllSubtract", (CAPL_FARCALL)appSubtract, "CAPL_DLL","This function will substract two values. The return value is the result",'L', 2, "LL", "", {"x","y"}},
{"dllSeedKeyCals", (CAPL_FARCALL)appSeedKeyCals, "CAPL_DLL","This function will calculation the key . The return value is the result",'D', 2, "DD", "", {"Seed","EncryptConstant"}},
{"dllSupportLongFunctionNamesWithUpTo50Characters", (CAPL_FARCALL)appLongFuncName, "CAPL_DLL","This function shows the support of long function names",'D', 0, "", "", {""}},
{"dllAdd63Parameters", (CAPL_FARCALL)appAddValues63, "CAPL_DLL", "This function will add 63 values. The return value is the result",'L', 63, "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL", "", {"val01","val02","val03","val04","val05","val06","val07","val08","val09","val10","val11","val12","val13","val14","val15","val16","val17","val18","val19","val20","val21","val22","val23","val24","val25","val26","val27","val28","val29","val30","val31","val32","val33","val34","val35","val36","val37","val38","val39","val40","val41","val42","val43","val44","val45","val46","val47","val48","val49","val50","val51","val52","val53","val54","val55","val56","val57","val58","val59","val60","val61","val62","val63"}},
{"dllAdd64Parameters", (CAPL_FARCALL)appAddValues64, "CAPL_DLL", "This function will add 64 values. The return value is the result",'L', 64, {SixtyFourLongPars}, "", {"val01","val02","val03","val04","val05","val06","val07","val08","val09","val10","val11","val12","val13","val14","val15","val16","val17","val18","val19","val20","val21","val22","val23","val24","val25","val26","val27","val28","val29","val30","val31","val32","val33","val34","val35","val36","val37","val38","val39","val40","val41","val42","val43","val44","val45","val46","val47","val48","val49","val50","val51","val52","val53","val54","val55","val56","val57","val58","val59","val60","val61","val62","val63","val64"}},
{0, 0}
};
在CAPL编程界面下的”includes”中,通过如下指令导入我们刚刚生成的dll文件(”capldll.dll”);并进行编译保存
includes
{
#pragma library ("./Seed2KeyCAPL.dll")
}
/*@!Encoding:936*/
includes
{
#pragma library ("./Seed2KeyCAPL.dll")
}
variables
{
dword gSeed;
dword gKey;
long KeyConst=0x01020304;
}
on key 'a'
{
gKey=dllSeedKeyCals(gSeed,KeyConst);
}