关于CAPL中Seed2Key DLL生成与调用

在实际的工程应用中,经常会遇到一些Seed2Key的算法问题,而且为了安全保密,这个算法的源码又不想公开,我们可以将其打包成DLL,然后在CAPL里面直接调用DLL即可。

1. Seed2Key DLL生成

1.1 工程模板

CANoe的开发公司Vector 已经提供了DLL生成的工程模板,以Win7系统, CANoe 8.5为例,工程模板路径如下:

C:\Users\Public\Documents\Vector\CANoe\8.5\CANoe Sample Configurations\Programming\CAPLdll

Path

1.2 接口函数

打开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;

关于参数类型的定义如下:
parameters

1.3 添加 appSeedKeyCals

打开CAPLdll\Sources\capldll.cpp

a. Seed2Key 函数声明

// 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 );
}

b. Seed2Key 导出DLL

插入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}
};

1.4 编译生成DLL

2. Seed2Key DLL调用

2.1 Include DLL

在CAPL编程界面下的”includes”中,通过如下指令导入我们刚刚生成的dll文件(”capldll.dll”);并进行编译保存

includes
{
  #pragma library ("./Seed2KeyCAPL.dll")
}

DLL Functions

2.2 Demo

/*@!Encoding:936*/
includes
{
  #pragma library ("./Seed2KeyCAPL.dll")
}

variables
{
  dword gSeed;
  dword gKey;
  long KeyConst=0x01020304;
}

on key 'a'
{
  gKey=dllSeedKeyCals(gSeed,KeyConst);
}
  • 7
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值