SecurityAccess 之 KeyGenDll_GenerateKeyEx

  在使用诊断功能时,会涉及SecurityAccess,KeyGenDll_GenerateKeyEx是一种根据seed计算SecurityAccess key的方法,在Vector CANoe提供的Sample Configurations中就有Demo。默认安装位置: C:\Users\Public\Documents\Vector\CANoe\Sample Configurations 11.0.42\CAN\Diagnostics\UDSSystem\SecurityAccess\Sources\KeyGenDll_GenerateKeyEx

  每个项目都具有独特性,这里根据我的项目实际情况,分享一下KeyGenDll_GenerateKeyEx使用方法:

 

代码:

// KeyGeneration.cpp : Defines the entry point for the DLL application.
//

#include <windows.h>
#include "KeyGenAlgoInterfaceEx.h"



BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    return TRUE;
}



KEYGENALGO_API VKeyGenResultEx GenerateKeyEx(
      const unsigned char*  iSeedArray,     /* Array for the seed [in] */
      unsigned int          iSeedArraySize, /* Length of the array for the seed [in] */
      const unsigned int    iSecurityLevel, /* Security level [in] */
      const char*           iVariant,       /* Name of the active variant [in] */
      unsigned char*        ioKeyArray,     /* Array for the key [in, out] */
      unsigned int          iKeyArraySize,  /* Maximum length of the array for the key [in] */
      unsigned int&         oSize           /* Length of the key [out] */
      )
{
    //Copy seed from parameter to a integer
    //Note:The byte order in the seed array is equal to the byte order in the bus message
    unsigned int seed = 0;
    unsigned int key = 0;
    
    seed = seed | ( iSeedArray[3] << 0 );
    seed = seed | ( iSeedArray[2] << 8 );
    seed = seed | ( iSeedArray[1] << 16 );
    seed = seed | ( iSeedArray[0] << 24 );

    //begin calculate key form seed 
    if( 0x01 == iSecurityLevel )
    {
        //for security access with Services 0x27 01 -> 0x27 02
        printf("iSecurityLevel is %02X",iSecurityLevel);
        
        //Security algorithm has been deleted
        key = seed;
        
        printf("key is %02X",key);
    }
    else
    {
        key = 0xFFFFFFFF;
        printf("- - - Error ! - - -");
    }
    //end calculate key form seed
    
    //copy key to the output buffer
    //Note:The first byte of the key array will be the first key byte of the bus message
    ioKeyArray[3] = key & 0xff;
    ioKeyArray[2] = ( key >> 8) & 0xff;
    ioKeyArray[1] = ( key >> 16) & 0xff;
    ioKeyArray[0] = ( key >> 24) & 0xff;
    printf("ioKeyArray is %02X",ioKeyArray);
      
    oSize=iSeedArraySize;
    
  return KGRE_Ok;
}

 

posted @ 2021-01-02 10:33  张小力  阅读(1705)  评论(0编辑  收藏  举报