Esempio n. 1
0
int main(int argc, char *argv[])
{
	char option[256];

	int reader_n;

	PCSC_CONTEXT PCSCctx;
	CARD_ID cid;
	int n;

	reader_n=1;
	for(n=1;n<argc-1;n++)
	{
		strcpy(option,argv[n]);
		if(option[0] != '-') { continue; }
		switch(option[1]) {
			case 'r':
				reader_n=atoi(argv[n+1]);
				n++;
			break;
		}
	}

	if(pcsc_init_ctx(reader_n, &PCSCctx)<1) {fprintf(stderr,"PCSC context init failed\n"); return -1;}
	cid=pcsc_read_card_id(&PCSCctx);
	if(cid.uid_len == 0) {fprintf(stderr,"Card read failed\n"); return -1;}
	pcsc_free_ctx( &PCSCctx);

	printf("UID:%s\n",bytes2hex(cid.uid_bytes,cid.uid_len));
	printf("ATR:%s\n",bytes2hex(cid.atr_bytes,cid.atr_len));
	free_card_id(cid);
	return  0;
}
Esempio n. 2
0
void nckuhash(const char *stu_no, const char *salt, char *result)
{
	const size_t NBYTES = 8;
	BYTE hashsum[NBYTES];
	const char *src;
	size_t i;
	int count1, count2;

	// init hashsum
	memset(hashsum, 0, sizeof(hashsum));
	for (i = 0; stu_no[i] != '\0'; i++)
		hashsum[i % NBYTES] *= stu_no[i];

	// start hash
	src = salt;
	i = 0;
	count1 = 0;
	count2 = 0;
	while (1) {
		hashsum[i] = (hashsum[i] ^ (*src)) * count1;
		src++, i++;
		if (count1 > NBYTES && count2 > NBYTES)
			break;
		if (*src == '\0') {
			hashsum[i] = hashsum[i] << 1;
			src = salt;
			count1++;
		}
		if (i == NBYTES) {
			i = 0;
			count2++;
		}
	}

	bytes2hex(hashsum, NBYTES, result);
}
Esempio n. 3
0
template<typename Config> void SystemSetupConsole<Config>::handle(char c)
{
    if ('i' == c)
    {
    	// see if we have any additional properties. This is true
    	// for Cellular and Mesh devices.
    	hal_system_info_t info = {};
    	info.size = sizeof(info);
    	HAL_OTA_Add_System_Info(&info, true, nullptr);
    	LOG(TRACE, "device info key/value count: %d", info.key_value_count);
    	if (info.key_value_count) {
    		print("Device ID: ");
    		String id = spark_deviceID();
			print(id.c_str());
			print("\r\n");

			for (int i=0; i<info.key_value_count; i++) {
				char key[20];
				if (!filter_key(info.key_values[i].key, key, sizeof(key))) {
					print(key);
					print(": ");
					print(info.key_values[i].value);
					print("\r\n");
				}
			}
		}
    	else {
	#if PLATFORM_ID<3
			print("Your core id is ");
	#else
			print("Your device id is ");
	#endif
			String id = spark_deviceID();
			print(id.c_str());
			print("\r\n");
    	}
    	HAL_OTA_Add_System_Info(&info, false, nullptr);
    }
    else if ('m' == c)
    {
        print("Your device MAC address is\r\n");
        IPConfig config = {};
    #if !HAL_PLATFORM_WIFI    
        auto conf = static_cast<const IPConfig*>(network_config(0, 0, 0));
    #else
        auto conf = static_cast<const IPConfig*>(network_config(NETWORK_INTERFACE_WIFI_STA, 0, 0));
    #endif
        if (conf && conf->size) {
            memcpy(&config, conf, std::min(sizeof(config), (size_t)conf->size));
        }
        const uint8_t* addr = config.nw.uaMacAddr;
        print(bytes2hex(addr++, 1).c_str());
        for (int i = 1; i < 6; i++)
        {
            print(":");
            print(bytes2hex(addr++, 1).c_str());
        }
        print("\r\n");
    }
    else if ('f' == c)
    {
        serial.println("Waiting for the binary file to be sent ... (press 'a' to abort)");
        system_firmwareUpdate(&serial);
    }
    else if ('x' == c)
    {
        exit();
    }
    else if ('s' == c)
    {
        auto prefix = "{";
        auto suffix = "}\r\n";
        WrappedStreamAppender appender(serial, (const uint8_t*)prefix, strlen(prefix), (const uint8_t*)suffix, strlen(suffix));
        system_module_info(append_instance, &appender);
    }
    else if ('v' == c)
    {
        StreamAppender appender(serial);
        append_system_version_info(&appender);
        print("\r\n");
    }
    else if ('L' == c)
    {
        system_set_flag(SYSTEM_FLAG_STARTUP_LISTEN_MODE, 1, nullptr);
        System.enterSafeMode();
    }
    else if ('c' == c)
    {
            bool claimed = HAL_IsDeviceClaimed(nullptr);
            print("Device claimed: ");
            print(claimed ? "yes" : "no");
            print("\r\n");
    }
    else if ('C' == c)
    {
            char code[64];
            print("Enter 63-digit claim code: ");
            read_line(code, 63);
            if (strlen(code)==63) {
                HAL_Set_Claim_Code(code);
                print("Claim code set to: ");
                print(code);
            }
            else {
                print("Sorry, claim code is not 63 characters long. Claim code unchanged.");
}
        print("\r\n");
    }
    else if ('d' == c)
    {
        system_format_diag_data(nullptr, 0, 0, StreamAppender::append, &serial, nullptr);
        print("\r\n");
    }
}
Esempio n. 4
0
/**
 * msc_tripleDes:3des加/解密
 * @mptmp:用于分配内存的内存池
 * @data:明文
 * @kkey:密钥
 * @iv:明文向量 
 * @decode_or_encode_flag:加密/解密开关
 *
 * 返回值:失败返回NULL,成功返回vector字符串
 */
unsigned char *tripleDes(apr_pool_t *mptmp, const unsigned char *data,
                const unsigned char *kkey, const unsigned char *iv, int decode_or_encode_flag)
{   
    int data_rest;
    int data_len;
    int count, i;
    unsigned char ch;

    unsigned char *tmp;
    unsigned char *data_real;
    unsigned char *src; /* 补齐后的明文 */
    unsigned char *dst; /* 解密后的明文 */
    int len; 
    
    unsigned char in[LEN_OF_STEP];
    unsigned char out[LEN_OF_STEP];

    int key_len;
    unsigned char key[LEN_OF_KEY]; /* 补齐后的密钥 */
    unsigned char block_key[LEN_OF_STEP + 1];
    DES_key_schedule ks,ks2,ks3;
    
    /* 入参检查 */
    if (mptmp == NULL || data == NULL || kkey == NULL || iv == NULL) {
        return NULL;
    }
    if (decode_or_encode_flag != DES_ENCRYPT && decode_or_encode_flag != DES_DECRYPT) {
        return NULL;
    }

    /* 构造补齐后的密钥 */
    key_len = strlen((char *)kkey);
    memcpy(key, (char *)kkey, (key_len < LEN_OF_KEY)?key_len:LEN_OF_KEY);
    if (key_len < LEN_OF_KEY) {
        memset(key + key_len, 0x00, LEN_OF_KEY - key_len);
    }

    /* 拷贝一份原始数据 */
    data_len = strlen((char *)data);
    data_real = apr_pmemdup(mptmp, data, data_len + 1);    
    
    /* 加密/解密前处理 */
    ch = '\0';
    len = 0;
    data_rest = 0;
    if (decode_or_encode_flag == DES_ENCRYPT) {
        data_real = (unsigned char *)apr_pstrcat(mptmp, (char *)data_real, "|", (char *)iv, NULL);
        
        /* 分析补齐明文所需空间及补齐填充数据 */
        data_len = strlen((char *)data_real);
        data_rest = data_len % LEN_OF_STEP;
        if (data_rest != 0) {
            len = data_len + (LEN_OF_STEP - data_rest);
            ch = LEN_OF_STEP - data_rest;
        } else {
            len = data_len;
        }
        
    } else {
        /* 解密前要将16进制字符串转换成字节流 */
        data_len = hex2bytes_inplace_3des(data_real, data_len);
        len = data_len;
    }

    src = (unsigned char *)apr_palloc(mptmp, len + 1);
    dst = (unsigned char *)apr_palloc(mptmp, len + 1);
    if (src != NULL && dst != NULL) {

        /* 构造补齐后的加密内容 */
        memset(src, 0, len + 1);
        memcpy(src, data_real, data_len);
        
        if (data_rest != 0) {
            memset(src + data_len, ch, LEN_OF_STEP - data_rest);
        }        
        
        /* 密钥置换 */
        memset(block_key, 0, sizeof(block_key));
        memcpy(block_key, key + 0, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks);
        memcpy(block_key, key + LEN_OF_STEP, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks2);
        memcpy(block_key, key + LEN_OF_STEP * 2, LEN_OF_STEP);
        DES_set_key_unchecked((const_DES_cblock *)block_key, &ks3);

        /* 循环加密/解密,每8字节一次 */
        count = len / LEN_OF_STEP;
        for (i = 0; i < count; i++) {
            memset(in, 0, LEN_OF_STEP);
            memset(out, 0, LEN_OF_STEP);
            memcpy(in, src + LEN_OF_STEP * i, LEN_OF_STEP);

            DES_ecb3_encrypt((const_DES_cblock *)in, (DES_cblock *)out, &ks, &ks2, &ks3, decode_or_encode_flag);
            /* 将解密的内容拷贝到解密后的明文 */
            memcpy(dst + LEN_OF_STEP * i, out, LEN_OF_STEP);
        }
        *(dst + len) = 0; 
        
        if (decode_or_encode_flag == DES_DECRYPT) {
            /* 解密需要检验明文后面的向量,最后去除向量 */
            tmp = (unsigned char *)strrchr((const char *)dst, '|');
            if (tmp != NULL) {
                *tmp++ = 0;
                if (strncasecmp((const char *)tmp, (const char *)iv, strlen((char *)iv)) == 0) {
                    return dst;
                }
            }
        } else {
            /* 加密需要将密文编码成16进制字符串 */
            return (unsigned char *)bytes2hex(mptmp, dst, len);
        }
        
    }
    
    return NULL;
}