Vector Diva文件的模板路径
代码例子
KEYGENALGO_API VKeyGenResultEx GenerateKeyEx(
const unsigned char* ipSeedArray, /* 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* ipVariant, /* Name of the active variant [in] */
unsigned char* iopKeyArray, /* Array for the key [in, out] */
unsigned int iMaxKeyArraySize, /* Maximum length of the array for the key [in] */
unsigned int& oActualKeyArraySize) /* 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;
seed=ipSeedArray[3];
seed=seed | (ipSeedArray[2]<<8);
seed=seed | (ipSeedArray[1]<<16);
seed=seed | (ipSeedArray[0]<<24);
unsigned int delta = 0x9E3779B9;
unsigned int key=0;
unsigned int notseed = ~seed;
int constant[4] = { 0x33,0x5a,0xb7,0x98 };//22F18C后四位,是个常量,对结果有有影响的
//if ((iSeedArraySize > iMaxKeyArraySize) | (ipSeedArray[1] << 16) | (ipSeedArray[0] << 24))
//return KGREO_BufferToSmall;
//
//begin calculate key from seed------------------------------------------------------------
//for security access with Services 0x27 01 ->0x27 02
int round = 2;//几级解锁
int sum = 0;
if (iSecurityLevel == 0x01)
{
for (int i = 0; i < round; i++)
{
seed += (((notseed << 4) ^ (notseed >> 5)) + notseed) ^ (sum + constant[sum & 3]);
sum += delta;
notseed += (((seed << 4) ^ (seed >> 5)) + seed) ^ (sum + constant[(sum >> 11) & 3]);
}
key = seed;
}
//for security access with Services 0x27 03 -> 0x27 04
if (iSecurityLevel==0x03)
{
key=seed/2;
}
//for security access with Services 0x27 05 -> 0x27 06
if (iSecurityLevel==0x09)
{
constant[0] = ((constant[0] >> 3) | (constant[0] << 5)) & 0x000000FF;
constant[1] = ((constant[1] >> 3) | (constant[1] << 5)) & 0x000000FF;
constant[2] = ((constant[2] >> 3) | (constant[2] << 5)) & 0x000000FF;
constant[3] = ((constant[3] >> 3) | (constant[3] << 5)) & 0x000000FF;
for (int i = 0; i < round; i++)
{
seed += (((notseed << 4) ^ (notseed >> 5)) + notseed) ^ (sum + constant[sum & 3]);
sum += delta;
notseed += (((seed << 4) ^ (seed >> 5)) + seed) ^ (sum + constant[(sum >> 11) & 3]);
}
key = seed;
}
//end calculate key from seed------------------------------------------------------------
//key = 0x9E3779B9;
//Copy key to the output buffer
//Note: The first byte of the key array will be the first key byte of the bus message
// iopKeyArray[3] = key & 0xff;
//iopKeyArray[2] = (key>>8)& 0xff;
//iopKeyArray[1] = (key>>16)& 0xff;
// iopKeyArray[0] = (key>>24)& 0xff;
iopKeyArray[3] = key & 0x000000ff;
iopKeyArray[2] = (key & 0x0000ff00)>>8;
iopKeyArray[1] = (key & 0x00ff0000)>>16;
iopKeyArray[0] = (key & 0xff000000)>>24;
//setting length of key
oActualKeyArraySize = 4;
return KGRE_Ok;
}