
xorArray = {0xAA,0x50,0x43,0x52}
securetyKeyCalc = function(pucSeed,ucSecurityLevel){
	//SeedSec_t cal,key,seed;
	var seed = {0,0,0,0};
	var key = {0,0,0,0};
	var cal = {0,0,0,0};
	seed[1]=pucSeed[4];
	seed[2]=pucSeed[3];
	seed[3]=pucSeed[2];  
	seed[4]=pucSeed[1];
	select(ucSecurityLevel) {
		case 0x01 {
			/*
			cal[1] = seed[1]^xorArray[1];
			cal[2] = seed[2]^xorArray[2];
			cal[3] = seed[3]^xorArray[3];
			cal[4] = seed[4]^xorArray[4];
			key[4] = ((cal[1]&0x0F)<<4)|( cal[1]&0xF0);
			key[3] = ((cal[3]&0x0F)<<4)|((cal[4]&0xF0)>>4); 
			key[2] = (cal[3]&0xF0)|((cal[2]&0xF0)>>4);
			key[1] = ((cal[4]&0x0F)<<4)|( cal[2]&0x0F); 
			*/
			var dll = raw.loadDll("/MSW.dll",,"cdecl" );
			//
			if(dll != null){
				FuncDisplay("加载密钥dll成功");
				var retlen = raw.buffer(4,0);
				var seedraw = raw.buffer(4,0);
				var iVariant = raw.buffer(20,0);
				var keyraw = raw.buffer(4,0);
				seedraw[1] = pucSeed[1];
				seedraw[2] = pucSeed[2];
				seedraw[3] = pucSeed[3];
				seedraw[4] = pucSeed[4];
				var ret = 0;
				ret = dll.GenerateKeyEx(seedraw,4,0x01,iVariant,keyraw,4,retlen);
				FuncDisplay("ret = " ++ ret );
				
				key[1] = keyraw[1];
				key[2] = keyraw[2];
				key[3] = keyraw[3];
				key[4] = keyraw[4];
				return key; 
			}
			else {
				FuncDisplay("加载密钥dll失败");
			}
			
			
		}
		case 0x03 {
			/*
			cal.byte[0] = ((seed.byte[0]&0xF8)>>3)^xorArray[0];
			cal.byte[1] = ((seed.byte[1]&0xF8)>>3)^xorArray[1];
			cal.byte[2] = ((seed.byte[2]&0xF8)>>3)^xorArray[2];
			cal.byte[3] = ((seed.byte[3]&0xF8)>>3)^xorArray[3];
	
			key.byte[0] = ((cal.byte[3]&0x07)<<5)|(( cal.byte[0]&0xF8)>>3);
			key.byte[1] = ((cal.byte[0]&0x07)<<5)|(cal.byte[2]&0x1F); 
			key.byte[2] = (cal.byte[1]&0xF8)|((cal.byte[3]&0xE0)>>5);
			key.byte[3] = (cal.byte[2]&0xF8)|( cal.byte[1]&0x07);
			*/
			cal[1] = ((seed[1]&0xF8)>>3)^xorArray[1];
			cal[2] = ((seed[2]&0xF8)>>3)^xorArray[2];
			cal[3] = ((seed[3]&0xF8)>>3)^xorArray[3];
			cal[4] = ((seed[4]&0xF8)>>3)^xorArray[4];
	
			key[1] = ((cal[4]&0x07)<<5)|(( cal[1]&0xF8)>>3);
			key[2] = ((cal[0]&0x07)<<5)|(cal[3]&0x1F); 
			key[3] = (cal[2]&0xF8)|((cal[4]&0xE0)>>5);
			key[4] = (cal[3]&0xF8)|( cal[2]&0x07);
		}
		case 0x11 {
			/*
			cal.byte[0] = seed.byte[0]^xorArray[0];
			cal.byte[1] = seed.byte[1]^xorArray[1];
			cal.byte[2] = seed.byte[2]^xorArray[2];
			cal.byte[3] = seed.byte[3]^xorArray[3];

			key.byte[3] = ((cal.byte[1]&0x03)<<6)|(( cal.byte[0]&0xFC)>>2);
			key.byte[2] = ((cal.byte[0]&0x03)<<6)|(cal.byte[3]&0x3F); 
			key.byte[1] = (cal.byte[3]&0xFC)|((cal.byte[2]&0xC0)>>6);
			key.byte[0] = (cal.byte[2]&0xFC)|(cal.byte[1]&0x03);
			*/
			cal[1] = seed[1]^xorArray[1];
			cal[2] = seed[2]^xorArray[2];
			cal[3] = seed[3]^xorArray[3];
			cal[4] = seed[4]^xorArray[4];

			key[4] = ((cal[2]&0x03)<<6)|(( cal[1]&0xFC)>>2);
			key[3] = ((cal[1]&0x03)<<6)|(cal[4]&0x3F); 
			key[2] = (cal[4]&0xFC)|((cal[3]&0xC0)>>6);
			key[1] = (cal[3]&0xFC)|(cal[2]&0x03);
		}
		else {
		}
	}
	var pucKey = {0,0,0,0};
	pucKey[1]=key[4];
	pucKey[2]=key[3];
	pucKey[3]=key[2];
	pucKey[4]=key[1];
	
	return pucKey; 
}
