Пример #1
0
void AuthSocket::ProcessData( const byte *buf,size_t len )
{
	ByteBuffer packetContents(buf,len);

	DEBUG_LOG(format("Auth Receieved |%1%|") % Bin2Hex(packetContents));

	byte packetOpcode;
	packetContents >> packetOpcode;
	AuthOpcode opcode = AuthOpcode(packetOpcode);

	switch (opcode)
	{
	default:
		{
			break;
		}
	case AS_GetPublicKeyRequest:
		{
			HandleGetPublicKeyRequest(packetContents);
			break;
		}
	case AS_AuthRequest:
		{
			HandleAuthRequest(packetContents);
			break;
		}
	case AS_AuthChallengeResponse:
		{
			HandleAuthChallengeResponse(packetContents);
			break;
		}
	}
}
Пример #2
0
void BigInteger2Hex(BigInteger *pBigInt, char *decimal, int groupLength)
{
  if (pBigInt->sign == SIGN_NEGATIVE)
  {
    *decimal++ = '-';
  }
  Bin2Hex(pBigInt->limbs, decimal, pBigInt->nbrLimbs, groupLength);
}
Пример #3
0
/*将两个数据异或,从data中输出结果*/
char *   XOR(  char* data,   char* data2,int len,char * _______XOR)
{
	int i ;
	int bLenght=len/2;
	unsigned char b[strlen(data)/2];
	unsigned char b2[strlen(data2)/2];
	Hex2Bin(data,b);
	Hex2Bin(data2,b2);
	for ( i = 0; i < bLenght; i++)
	{
		b[i] =  b[i]  ^  b2[i];
	}
	Bin2Hex(b,bLenght,_______XOR);
	return _______XOR;

}
Пример #4
0
bool CSumiNodeTesterApp::SendUdpData()
{
	static unsigned int s_unUdpMsgId = 0;
    //msg header
    uint8_t u8CurrSerialMsgId = g_u8SerialMsgId;
	API_MSG_HDR *pUDPSendDatagramHdr = (API_MSG_HDR*)g_pucSendBuf;
	SET_API_HDR(pUDPSendDatagramHdr, UDP_SPECIFIC, API_MSG_REQ, UDP_SEND_DATAGRAM, g_u8SerialMsgId, sizeof(UDP_SEND_DATAGRAM_PYLD) + m_stCfg.GetUdpPyldSize());
	++g_u8SerialMsgId;
	//msg payload
    UDP_SEND_DATAGRAM_PYLD *pUDPSendDatagramPyld = (UDP_SEND_DATAGRAM_PYLD*)(g_pucSendBuf + sizeof(API_MSG_HDR));
    UDP_SEND_DATAGRAM_SET_SRC_PORT(pUDPSendDatagramPyld, m_stCfg.GetUdpLocalPort());
    UDP_SEND_DATAGRAM_SET_DST_PORT(pUDPSendDatagramPyld, m_stCfg.GetCoordIpv6()->GetPort());
    memcpy(pUDPSendDatagramPyld->m_au8DstIpv6, m_stCfg.GetCoordIpv6()->GetIPv6(), 16);
    //msg udp payload
    UDP_PYLD_ID *pUDPPyldId = (UDP_PYLD_ID*)(g_pucSendBuf + sizeof(API_MSG_HDR) + sizeof(UDP_SEND_DATAGRAM_PYLD));
    unsigned int unUdpMsgId = s_unUdpMsgId;
    SET_UDP_PYLD_ID(pUDPPyldId, unUdpMsgId);
    s_unUdpMsgId++;
    memcpy(g_pucSendBuf + sizeof(API_MSG_HDR) + sizeof( UDP_SEND_DATAGRAM_PYLD ) +  sizeof(s_unUdpMsgId), 
    			m_pu8UdpPyld, m_stCfg.GetUdpPyldSize() - sizeof(s_unUdpMsgId) );
    int nBytesToSend = sizeof(API_MSG_HDR) + sizeof(UDP_SEND_DATAGRAM_PYLD) + m_stCfg.GetUdpPyldSize();
    
    //Send over serial device
    int nSentData = m_poBufferedSerialLink->Write(g_pucSendBuf, nBytesToSend);
    if(nSentData <= 0)
    {   NLOG_ERR("FAILED TO SEND UDP_SEND_DATAGRAM ON SERIAL LINK!");
        return false;
    }

	//log msg
	NLOG_INFO("Sent data: addr=%s:%d, len=%d, UDP_ID=%d", 
			GetHex(m_stCfg.GetCoordIpv6()->GetIPv6(),16), 
            m_stCfg.GetCoordIpv6()->GetPort(), 
			m_stCfg.GetUdpPyldSize(),
			unUdpMsgId);
    memset(g_aucLogBuf,0,sizeof(g_aucLogBuf));
    Bin2Hex((char*)g_aucLogBuf, sizeof(g_aucLogBuf), (char*)g_pucSendBuf + sizeof(API_MSG_HDR) + sizeof(UDP_SEND_DATAGRAM_PYLD),
            m_stCfg.GetUdpPyldSize());
    NLOG_INFO("Datagram payload: %s", g_aucLogBuf);

	//
    g_oSumiStats.AddSentNewUdpMsg(u8CurrSerialMsgId, unUdpMsgId, m_stCfg.GetUDPRespTimedoutSec());
    return true;
}
Пример #5
0
static int
authenticate_md5(Pop3 pc,
				 struct connection_state *scs, const char *capabilities)
{
	char buf[BUF_SIZE];
	char buf2[BUF_SIZE];
	unsigned char *md5;
	gcry_md_hd_t gmh;
	gcry_error_t rc;

	if (!strstr(capabilities, "AUTH=CRAM-MD5")) {
		/* server doesn't support cram-md5. */
		return 0;
	}

	tlscomm_printf(scs, "a007 AUTHENTICATE CRAM-MD5\r\n");
	if (tlscomm_expect(scs, "+ ", buf, BUF_SIZE) == 0)
		goto expect_failure;

	Decode_Base64(buf + 2, buf2);
	IMAP_DM(pc, DEBUG_INFO, "CRAM-MD5 challenge: %s\n", buf2);

	strcpy(buf, PCU.userName);
	strcat(buf, " ");
	ask_user_for_password(pc, 0);
	rc = gcry_md_open(&gmh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
	if (rc != 0) {
		IMAP_DM(pc, DEBUG_INFO, "unable to initialize gcrypt md5\n");
		return 0;
	}
	DEFROB(PCU.password);
	gcry_md_setkey(gmh, PCU.password, strlen(PCU.password));
	ENFROB(PCU.password);
	gcry_md_write(gmh, (unsigned char *) buf2, strlen(buf2));
	gcry_md_final(gmh);
	md5 = gcry_md_read(gmh, 0);
	Bin2Hex(md5, 16, buf2);
	gcry_md_close(gmh);

	strcat(buf, buf2);
	IMAP_DM(pc, DEBUG_INFO, "CRAM-MD5 response: %s\n", buf);
	Encode_Base64(buf, buf2);

	tlscomm_printf(scs, "%s\r\n", buf2);
	if (tlscomm_expect(scs, "a007 ", buf, BUF_SIZE) == 0)
		goto expect_failure;

	if (!strncmp(buf, "a007 OK", 7))
		return 1;				/* AUTH successful */

	IMAP_DM(pc, DEBUG_ERROR,
			"CRAM-MD5 AUTH failed for user '%s@%s:%d'\n",
			PCU.userName, PCU.serverName, PCU.serverPort);
	IMAP_DM(pc, DEBUG_INFO, "It said %s", buf);
	return 0;

  expect_failure:
	IMAP_DM(pc, DEBUG_ERROR,
			"tlscomm_expect failed during cram-md5 auth: %s", buf);
	IMAP_DM(pc, DEBUG_ERROR, "failed to authenticate using cram-md5.");
	return 0;
}
void gen_aes256_key_iv()
{
    HCRYPTPROV provider = NULL;
    HCRYPTKEY hKey = NULL;
    HCRYPTKEY newHKey = NULL;
    DWORD ivLen, blockLen = 0;
    BYTE* iv = NULL;
    BYTE* buffer = NULL;
    char* cipherText = NULL;
    unsigned long cLen;
    char* cipherText2 = NULL;
    unsigned long cLen2;
    char* cipherText3 = NULL;
    unsigned long cLen3;
    char* cipherText4 = NULL;
    unsigned long cLen4;

    if (!OpenCryptContext(&provider)) {
        printf("CryptAcquireContext() failed:");
        goto Exit0;
    }

    // 生成随机密钥
    // 还可以从通过给定密码 HASH 后获取 AES 密钥
    if (!CryptGenKey(provider, CALG_AES_256, CRYPT_EXPORTABLE, &hKey)) {
        goto Exit0;
    }

    // Get the key size.
    DWORD buffer_size = 0;

    if (!CryptExportKey(hKey, 0, PLAINTEXTKEYBLOB, 0, NULL, &buffer_size)) {
        printf("CryptExportKey failed.\n");
        goto Exit0;
    }

    // Export the key.
    buffer = new BYTE[buffer_size];

    if (!CryptExportKey(hKey, 0, PLAINTEXTKEYBLOB, 0, buffer, &buffer_size)) {
        printf("CryptExportKey2 failed.\n");
        goto Exit0;
    }

    plaintext_blob_t* blob = (plaintext_blob_t*)buffer;
    printf("aes Key:\n");
    printf("\nchar aes256_key[]=\n{");
    print_bin2c(blob->rgbKeyData, blob->cbKeySize);
    printf("};");
    printf("\n\n");

    // base64 编码
    if (!Base64EncodeA(&cipherText, &cLen, blob->rgbKeyData, blob->cbKeySize))  {
        //xfree(encrypted);
        xstrerror("Base64EncodeA()");
        goto Exit0;
    }

    printf("aes base64 Key:\n");
    printf(cipherText);
    printf("\n\n");

    // Hex 编码
    if (!Bin2Hex(&cipherText3, &cLen3, blob->rgbKeyData, blob->cbKeySize)) {
        xstrerror("Bin2Hex()");
        goto Exit0;
    }

    printf("aes hex Key:\n");
    printf(cipherText3);
    printf("\n\n");

    // 从随机密钥获取 IV 长度
    if (!CryptGetKeyParam(hKey, KP_IV, NULL, &ivLen, 0)) {
        goto Exit0;
    }

    iv = new BYTE[ivLen];
    ZeroMemory(iv, ivLen);

    if (!CryptGenRandom(provider, ivLen, iv)) {
        goto Exit0;
    }

    printf("aes IV:\n");
    printf("\nchar aes256_iv[]=\n{");
    print_bin2c(iv, ivLen);
    printf("};");
    printf("\n\n");

    if (!Base64EncodeA(&cipherText2, &cLen2, iv, ivLen))    {
        xstrerror("Base64EncodeA()");
        goto Exit0;
    }

    printf("aes base64 iv:\n");
    printf(cipherText2);
    printf("\n\n");

    // Hex 编码
    if (!Bin2Hex(&cipherText4, &cLen4, iv, ivLen)) {
        xstrerror("Bin2Hex()");
        goto Exit0;
    }

    printf("aes hex iv:\n");
    printf(cipherText4);
    printf("\n\n");
    DWORD LenBlockAES = 0;
    DWORD dwCount = sizeof(DWORD);

    if (!CryptGetKeyParam(hKey, KP_BLOCKLEN, (BYTE*)&LenBlockAES, &dwCount, 0)) {
        goto Exit0;
    }

    printf("aes block data len: %d\n", LenBlockAES);
Exit0:

    if (cipherText4) {
        xfree(cipherText4);
        cipherText4 = NULL;
    }

    if (cipherText3) {
        xfree(cipherText3);
        cipherText3 = NULL;
    }

    if (cipherText2) {
        xfree(cipherText2);
        cipherText2 = NULL;
    }

    if (cipherText) {
        xfree(cipherText);
        cipherText = NULL;
    }

    if (buffer) {
        delete[] buffer;
        buffer = NULL;
    }

    if (iv) {
        delete[] iv;
        iv = NULL;
    }

    if (hKey) {
        CryptDestroyKey(hKey);
    }

    if (provider) {
        CryptReleaseContext(provider, 0);
    }

    return;
}
Пример #7
0
//PROCESS 
bool CSumiNodeTesterApp::ProcessRcv()
{
	//input data
	if( !m_poBufferedSerialLink->IsLinkOpen() || !m_poBufferedSerialLink->HaveData( WAITUS ) )
        return false;
		
    //check
    int nRecvLen = m_poBufferedSerialLink->Read( g_pucRcvBuf, BUFSIZE );
    if (nRecvLen <= 0)
        return false;
    if (nRecvLen < (int)sizeof(API_MSG_HDR))
    {	NLOG_ERR("FAILED TO READ MSG HEADER ON SERIAL LINK!");
        return false;
    }

    //process rsp
    API_MSG_HDR *pHdr = (API_MSG_HDR*)g_pucRcvBuf;
    if (API_GET_IS_RSP_FLAG(pHdr))
    {   switch(API_GET_MSG_CLASS(pHdr))
        {
            case API_ACK:
                NLOG_INFO("RECV ACK msg_id = %u", API_GET_MESSAGE_ID(pHdr));
                g_oSumiStats.MarkSentUdpMsgAsAcked(API_GET_MESSAGE_ID(pHdr));
                return true;

            case API_NACK:
                switch(API_GET_TYPE(pHdr))
                {
                    case API_INVALID_SIZE:
                        NLOG_ERR("RECV NACK(API_INVALID_SIZE) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
                        break;
                        
                    case API_UDP_ERR_INTERNAL:
						NLOG_INFO("RECV NACK(API_UDP_ERR_INTERNAL) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;

                    case API_UDP_ERR_DEV_NOT_JOINED:
						NLOG_INFO("RECV NACK(API_UDP_ERR_DEV_NOT_JOINED) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;

                    case API_UDP_ERR_INVALID_SOCKET:
						NLOG_INFO("RECV NACK(API_UDP_ERR_INVALID_SOCKET) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;

                    case API_UDP_ERR_NO_MEMORY:
						NLOG_INFO("RECV NACK(API_UDP_ERR_NO_MEMORY) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;

                    case API_UDP_ERR_ND_NOT_READY:
						NLOG_INFO("RECV NACK(API_UDP_ERR_ND_NOT_READY) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;

                    default:
						NLOG_INFO("RECV NACK(UNKNOWN) for msg_id = %u", API_GET_MESSAGE_ID(pHdr));
						break;
                }
                g_oSumiStats.RemoveSentUdpMsg(API_GET_MESSAGE_ID(pHdr));
                return false;

            default:
                NLOG_ERR("RECV OTHER RESPONSE, MSG CLASS= %d!", API_GET_MSG_CLASS(pHdr));
                return false;
        }
    }
    
    //process request from RM
    switch(API_GET_MSG_CLASS(pHdr))
    {
        case UDP_SPECIFIC:
        {
            if (API_GET_TYPE(pHdr) != UDP_RECEIVE_DATAGRAM)
                break;

            //check for serial duplicates    
            if( g_bIsFirstDatagramRecv )
            {   g_u8RecvSerialMsgId = API_GET_MESSAGE_ID(pHdr);
                g_bIsFirstDatagramRecv = false;
            }
            else if(  API_GET_MESSAGE_ID(pHdr) == g_u8RecvSerialMsgId )
            {   NLOG_WARN("UDP_RECEIVE_DATAGRAM already read! Ignoring it...");
                return false;
            }
            else
            {   g_u8RecvSerialMsgId = API_GET_MESSAGE_ID(pHdr);
            }

            //
            UDP_RECV_DATAGRAM_PYLD* pRcvUdpDatagram = (UDP_RECV_DATAGRAM_PYLD*)((unsigned char*)pHdr + sizeof(API_MSG_HDR));
            UDP_PYLD_ID *pUDPPyldId = (UDP_PYLD_ID*)((unsigned char*)pHdr + sizeof(API_MSG_HDR) + sizeof(UDP_RECV_DATAGRAM_PYLD));
            unsigned int unUdpId = GET_UDP_PYLD_ID(pUDPPyldId);

            //log msg
            NLOG_INFO("Received data: addr=%s:%d, len=%d, UDP_ID=%d", 
                    GetHex(pRcvUdpDatagram->m_au8SrcIpv6, 16),
                    UDP_RECV_DATAGRAM_GET_SRC_PORT(pRcvUdpDatagram), 
                    nRecvLen - sizeof(API_MSG_HDR) - sizeof(UDP_RECV_DATAGRAM_PYLD),
                    unUdpId);
            memset(g_aucLogBuf,0,sizeof(g_aucLogBuf));
            Bin2Hex((char*)g_aucLogBuf, sizeof(g_aucLogBuf), (char*)pRcvUdpDatagram + sizeof(UDP_RECV_DATAGRAM_PYLD),
                    nRecvLen - sizeof(API_MSG_HDR)- sizeof(UDP_RECV_DATAGRAM_PYLD));
            NLOG_INFO("Datagram payload: %s", g_aucLogBuf);

            //
            g_oSumiStats.MarkSentUdpMsgAsResponded( unUdpId );

            //send ack
            if (!g_oUtils.SendACK(API_GET_MESSAGE_ID(pHdr), m_poBufferedSerialLink))
				return false;
        }
        break;
        
        case API_DEBUG:
        {
            if (API_GET_TYPE(pHdr) != API_PRINT_LOG)
                break;
            if( API_GET_DATA_SIZE(pHdr) >= sizeof(g_aucLogBuf) )
            {
                NLOG_ERR("LOG BUFFER TOO SMALL! Log buff=%d, API DATA SIZE=%d",
                        sizeof(g_aucLogBuf), API_GET_DATA_SIZE(pHdr));
                return false;
            }
            memcpy(g_aucLogBuf, g_pucRcvBuf + sizeof(API_MSG_HDR), API_GET_DATA_SIZE(pHdr));
            g_aucLogBuf[API_GET_DATA_SIZE(pHdr)]='\0';
            LOG("Log on dev: %s",g_aucLogBuf);
        }
        break;
        
        case STACK_SPECIFIC:
        {
            switch(API_GET_TYPE(pHdr))
            {
                case NOTIFY_JOIN:
                {
                    STACK_NOTIFY_JOIN *pNotifyJoin = (STACK_NOTIFY_JOIN*)((unsigned char*)pHdr + sizeof(API_MSG_HDR));
                    switch(pNotifyJoin->m_u8Status)
                    {
                        case MAC_IDLE_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= MAC_IDLE_STATE");
                            break;
                            
                        case ENERGY_DETECT_SCANNING_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= ENERGY_DETECT_SCANNING_STATE");
                            break;
                            
                        case ACTIVE_SCANNING_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= ACTIVE_SCANNING_STATE");
                            break;
                            
                        case SECURITY_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= SECURITY_STATE");
                            break;
                            
                        case ASSOCIATING_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= ASSOCIATING_STATE");
                            break;
                            
                        case ASSOCIATED_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= ASSOCIATED_STATE");
                            break;
                            
                        case ORPHAN_STATE:
                            NLOG_INFO("Recv NOTIFY_JOIN, status= ORPHAN_STATE");
                            break;
                            
                        default:
                            NLOG_WARN("Recv NOTIFY_JOIN, status = %u, UNKNOWN msg_id=%u!",
                                    pNotifyJoin->m_u8Status, API_GET_MESSAGE_ID(pHdr));
                            break;
                    }
                    //send ack
                    if (!g_oUtils.SendACK(API_GET_MESSAGE_ID(pHdr), m_poBufferedSerialLink))
						return false;
				}
                break;
                
                default:
                    NLOG_ERR("UNKNOWN STACK_SPECIFIC TYPE!");
                    break;
            }  
        }
        break;
        
        default:
            NLOG_WARN("RECV OTHER REQUEST MSG CLASS = %d", API_GET_MSG_CLASS(pHdr));
            return false;
        }
    return true;
}
Пример #8
0
string Bin2Hex(const string &sourceStr,uint32 flags)
{
	return Bin2Hex(sourceStr.data(),sourceStr.size(),flags);
}
Пример #9
0
string Bin2Hex(const ByteBuffer &sourceBuf,uint32 flags)
{
	return Bin2Hex((const byte*)sourceBuf.contents(),sourceBuf.size(),flags);
}
Пример #10
0
string Bin2Hex(const char *data,size_t count,uint32 flags)
{
	return Bin2Hex((const byte*)data,count,flags);
}
Пример #11
0
EXPORT char *   MAC8(char * data,int dataLength, char * ran, char * key8)
{
	char result[17] ;
	char d3[17] ;
	char d4[17];
	unsigned char Ran9[9];
	unsigned char Prtwmp[17];
	unsigned char key8aaa[strlen(key8)/2];Hex2Bin(key8,key8aaa);
	if (dataLength >= 16)
	{
		
		Substring(data,0, 16,d3);
		Prtwmp[16] = 0x00;
		XOR(PadRight( Substring(ran,0,8,Ran9),16,'0',Prtwmp),d3,dataLength,result);
		result[16]=0;
		if (dataLength % 16 == 0)
		{
			unsigned char temp777[8];
			int i;
			for (  i = 1; i < dataLength / 16; i++)
			{
				Hex2Bin(result,temp777);//temp777=Hex2Bin(result);
				enc(key8aaa, temp777);
				Bin2Hex(temp777,8,result);
				Substring(data,i * 16, 16,d3);
				XOR(result, d3,16,result);
				memcpy(d4,(char *)"8000000000000000",16);
				d4[16] = 0x00;
			}
			if (dataLength == 16) 
			{
				memcpy(d4,(char*)"8000000000000000",16);
				d4[16] = 0x00;
			}
			Hex2Bin(result,temp777);
			enc(key8aaa, temp777);
			Bin2Hex(temp777,8,result);
			XOR(result, d4,16,result);
			Hex2Bin(result,temp777);
			enc(key8aaa, temp777);
			Bin2Hex(temp777,8,result);

		}
		else
		{
			int i;
			unsigned char tmp999[8];
			char *tmpx99x;
			unsigned char tmp999keya[strlen(key8)/2];Hex2Bin(key8,tmp999keya);
			for ( i= 1; i < dataLength / 16; i++)
			{
				unsigned char tmp888[8];Hex2Bin(result,tmp888);
				enc(key8aaa, tmp888);
				Substring(data,i * 16, 16,d3);
				Bin2Hex(tmp888,8,result);
				XOR(result, d3,16,result);

			}

			Substring(data,dataLength - dataLength % 16, dataLength % 16,d4);			
			strcat(d4,"8");
			PadRight(d4,16, '0',d4);
			Hex2Bin(result,tmp999);
			enc(tmp999keya, tmp999);
			Bin2Hex(tmp999,8,result);
			XOR(result, d4,16,result);
			Hex2Bin(result,tmp999);
			enc(tmp999keya, tmp999);
			Bin2Hex(tmp999,8,result);
		}
	}
	else
	{
		unsigned char tmp888111[8];
		unsigned char PdTemp[17];
		sprintf(d3,"%s%s", data, "8");
		PadRight(d3,16, '0',d3);
		PdTemp[16] = 0;
		XOR(PadRight( Substring(ran,0, 8,Ran9),16, '0',PdTemp), d3,16,result);
		Hex2Bin(result,tmp888111);
		enc(key8aaa, tmp888111);
		Bin2Hex(tmp888111,8,result);
	}
	memset(___MAC8,0x00,9);
	memcpy(___MAC8,result,8);
	return ___MAC8;
}
Пример #12
0
char * MAC(char* data,int dataLength, char * ran, char * key16)
{
	int key16Length =32 ;
	char keya[17];
	char keyb[17];
	char d3[17];
	char d4[17];
	char result[17];
	memcpy( keya ,key16, key16Length / 2);
	memcpy( keyb  , (char*)&key16[key16Length / 2],key16Length / 2);
	keya[16]=0;
	keyb[16]=0;
	memset( d3 ,0x00,sizeof(d3));
	memset( d4 ,0x00,sizeof(d3));
	memset( result,0x00,sizeof(16));
	echomac("bb");
	if (dataLength >= 16)
	{
		unsigned char Ran9[9];
		unsigned char PrTemp[17];
		memcpy(d3, data, 16);
		PrTemp[16] = 0x00;
		d3[16]=0;
		XOR(PadRight(Substring(ran,0,8,Ran9), 16, '0',PrTemp),  d3,16,result);
		echomac("bb");
		if (dataLength % 16 == 0)
		{
			int i ;
			for ( i = 1; i <dataLength / 16; i++)
			{
				unsigned char resultBin[8],keyaBin[8] ;				;
				enc(Hex2Bin(keya,keyaBin), Hex2Bin(result,resultBin));
				Bin2Hex(resultBin,8,result);
				printf( result);
				Substring(data,i * 16, 16,d3);
				XOR(result, d3,16,result);
				memcpy(d4 , (char*)&"8000000000000000",16);
				echomac("bb");
			}
			if (dataLength == 16) 
			{
				memcpy(d4 ,"8000000000000000",16);
			}
			echomac("bb");
			unsigned char temp222[8];Hex2Bin(result,temp222);
			unsigned char keyaXXX[8];Hex2Bin(keya,keyaXXX);
			unsigned char keybXXX[8];Hex2Bin(keyb,keybXXX);
			enc(keyaXXX, temp222);
			Bin2Hex(temp222,8,result);			
			XOR(result, d4,16,result);			
			Hex2Bin(result,temp222);
			enc(keyaXXX, temp222);
			Bin2Hex(temp222,8,result);			
			dec(keybXXX, temp222);
			Bin2Hex(temp222,8,result);			
			enc(keyaXXX, temp222);
			Bin2Hex(temp222,8,result);	
			echomac("bb");
		}
		else
		{
			
			int i ;
			unsigned char keyaXXX1[8];Hex2Bin(keya,keyaXXX1);
			unsigned char keybXXX1[8];Hex2Bin(keyb,keybXXX1);
			echomac("bb");
			for ( i = 1; i < dataLength / 16; i++)
			{
				unsigned char temp4441[8];Hex2Bin(result,temp4441);
				unsigned char temp4442keya[8];Hex2Bin(keya,temp4442keya);
				enc(temp4442keya, temp4441);
				Bin2Hex(temp4441,8,result);
				Substring(data, i * 16, 16,d3);
				XOR(result, d3,16,result);
			}
			Substring(data,dataLength - dataLength % 16, dataLength % 16,d4);
			strcat(d4,"8");//可能有问题
			PadRight(d4,16, '0',d4);
			unsigned char temp555[8];Hex2Bin(result,temp555);
			enc(keyaXXX1, temp555);
			Bin2Hex(temp555,8,result);
			XOR(result,  d4,16,result);
			Hex2Bin(result,temp555);//temp555=Hex2Bin(result);
			enc(keyaXXX1, temp555);
			dec(keybXXX1, temp555);
			enc(keyaXXX1, temp555);
			Bin2Hex(temp555,8,result);
			echomac("bb");
		}
	}
	else
	{
		echomac("bb");
		unsigned char Ran9[9];
		unsigned char temp4442keya[8];Hex2Bin(keya,temp4442keya);
		unsigned char temp4442keyb[8];Hex2Bin(keyb,temp4442keyb);
		sprintf(d3,"%s%s",data,"8");
		PadRight(d3,16, '0',d3);
		PadRight(Substring(ran,0,8,Ran9),16,'0',result);
		XOR( result, d3,16,result);
		unsigned char temp777[8];Hex2Bin(result,temp777);
		enc(temp4442keya, temp777);
		Bin2Hex(temp777,8,result);;
		dec(temp4442keyb, temp777);
		Bin2Hex(temp777,8,result);
		enc(temp4442keya, temp777);
		Bin2Hex(temp777,8,result);
		echomac("bb");
	}
	memset(___MAC,0x00,9);
	memcpy(___MAC,result,8);
	return ___MAC;

}
Пример #13
0
bool CPrefsDialog::OKClicked()
{
	font_family defFamily;
	font_style defStyle;

	be_plain_font->GetFamilyAndStyle(&defFamily, &defStyle);
	gPrefs->SetPrefString("defdoc font family", fNewFontFamily);
	gPrefs->SetPrefString("defdoc font style", defStyle);
	gPrefs->SetPrefDouble("defdoc font size", fNewFontSize);

	be_bold_font->GetFamilyAndStyle(&defFamily, &defStyle);
	gPrefs->SetPrefString("border font family", fNewBFontFamily);
	gPrefs->SetPrefString("border font style", defStyle);
	gPrefs->SetPrefDouble("border font size", fNewBFontSize);

	gPrefs->SetPrefInt("decimal point", *GetText("decsep"));
	gPrefs->SetPrefInt("thousands separator", *GetText("thoussep"));
	gPrefs->SetPrefInt("list separator", *GetText("listsep"));
	gPrefs->SetPrefInt("date separator", *GetText("datesep"));
	gPrefs->SetPrefInt("time separator", *GetText("timesep"));
	gPrefs->SetPrefString("date order", fDMY->FindMarked()->Label());	
	gPrefs->SetPrefInt("24 hours", IsOn("24 hours"));
	gPrefs->SetPrefInt("Excel keys", IsOn("excel"));
	gPrefs->SetPrefInt("Select Gray", IsOn("grayselect"));
	gPrefs->SetPrefInt("start with new", IsOn("donew"));
	gWithEqualSign = IsOn("formula starts with equal");
	gPrefs->SetPrefInt("formula starts with equal", gWithEqualSign);
	gPrefs->SetPrefInt("dark gridlines", 	IsOn("dark gridlines"));
	if (gPrefs->GetPrefInt("dark gridlines", 0))
		gGridColor = 10;
	else
		gGridColor = 6;

	gPrefs->SetPrefString("c_symbol", GetText("c_symbol"));
	gPrefs->SetPrefInt("c_before", IsOn("c_before"));
	gPrefs->SetPrefInt("c_neg_par", IsOn("c_neg_par"));
	gPrefs->SetPrefInt("c_digits", atoi(GetText("c_digits")));

	if (fPageSetup)
	{
		char *ps = Bin2Hex(fPageSetup, fPageSetupSize);
		gPrefs->SetPrefString("default page setup", ps);
		FREE(ps);
	}

	BMessage msg(msg_WindowOptions);

	msg.AddString("docfamily", fDocFontFamily);
	msg.AddString("docstyle", "Roman");
	msg.AddFloat("docsize", fDocFontSize);
	msg.AddString("borderfamily", fDocBFontFamily);
	msg.AddString("borderstyle", "Bold");
	msg.AddFloat("bordersize", fDocBFontSize);
	msg.AddBool("autorecalc", IsOn("autorecalc"));
	msg.AddBool("displayzero", IsOn("displayzero"));
	msg.AddBool("dispgrid", IsOn("dispgrid"));
	msg.AddBool("disphead", IsOn("disphead"));
	msg.AddBool("prgrid", IsOn("prgrid"));
	msg.AddBool("prhead", IsOn("prhead"));
	
	fOwner->PostMessage(&msg, fOwner->GetCellView());

	BButton *b;
	b = (BButton *)FindView("ok");
	b->SetEnabled(false);
	b = (BButton *)FindView("cancel");
	b->SetEnabled(false);

	InitFormatter();

	return false;
} /* CPrefsDialog::OKClicked */