添加解密函数
This commit is contained in:
parent
c165e15b80
commit
a8877e415b
@ -21,7 +21,7 @@
|
||||
#define KEY_LEN 32//密钥长度
|
||||
#endif
|
||||
|
||||
int aes_128_cbc_pkcs7(rt_uint8_t *data, rt_uint16_t len, rt_uint8_t *dout)
|
||||
size_t aes_128_cbc_pkcs7(rt_uint8_t *data, size_t len, rt_uint8_t *dout)
|
||||
{
|
||||
// AES-256
|
||||
// 工作模式 CBC ,填充模式 PKCS7,不同语言要保持一致。
|
||||
@ -42,38 +42,73 @@ int aes_128_cbc_pkcs7(rt_uint8_t *data, rt_uint16_t len, rt_uint8_t *dout)
|
||||
rt_uint16_t cnt = (m + 1) * 16;
|
||||
|
||||
rt_uint8_t *pbuff = rt_malloc(cnt); //分配内存
|
||||
// rt_kprintf("%d -- %d %d new len is %d.\n", len,m,n,cnt);
|
||||
|
||||
rt_memcpy(pbuff, data, len); //data -> buff
|
||||
rt_memset(pbuff + len, (16 - n), 16 - n); //按PKCS7规则填充
|
||||
|
||||
// for (size_t i = 0; i < (m + 1) * 16; i++)
|
||||
// {
|
||||
// rt_kprintf("0x%02X ", p_key[i]);
|
||||
// }
|
||||
|
||||
unsigned char edata[cnt];//输出buff
|
||||
mbedtls_aes_context ctx; //创建结构体
|
||||
mbedtls_aes_init(&ctx); //初始化
|
||||
mbedtls_aes_setkey_enc(&ctx, key, KEY_LEN * 8); //设置密钥
|
||||
// rt_kprintf("--%d\n", rst);
|
||||
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, (m + 1) * 16, iv, pbuff, edata); //加密
|
||||
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, cnt, iv, pbuff, edata); //加密
|
||||
// rt_kprintf("--%d\n", rst);
|
||||
mbedtls_aes_free(&ctx); //释放结构体
|
||||
|
||||
rt_memcpy(dout, edata, cnt);
|
||||
rt_free(pbuff); //释放内存
|
||||
|
||||
// for (rt_uint16_t i = 0; i < cnt; i++)
|
||||
// {
|
||||
// rt_kprintf("0x%02X ", edata[i]);
|
||||
// }
|
||||
|
||||
// rt_kprintf("\n---DONE.---\n");
|
||||
|
||||
return cnt;
|
||||
|
||||
}
|
||||
|
||||
size_t aes_128_cbc_pkcs7_de(rt_uint8_t *data, size_t len, rt_uint8_t *dout)
|
||||
{
|
||||
// AES-256
|
||||
// 工作模式 CBC ,填充模式 PKCS7,不同语言要保持一致。
|
||||
// PKCS7规则是长度不够时缺几位补几个几,长度够时也要补
|
||||
// key长度由KEY_LEN定义,默认32位
|
||||
// iv取密钥前16位
|
||||
// 已多平台验证
|
||||
|
||||
unsigned char key[KEY_LEN];
|
||||
rt_memset(key, 0x00, KEY_LEN); //初始化密钥,填充0x00
|
||||
rt_strcpy(key, "Cssc722.Cssc722.Cssc722.."); //写入密钥
|
||||
|
||||
unsigned char iv[16];
|
||||
rt_memcpy(iv, key, 16); //取密钥前16为偏移量
|
||||
|
||||
unsigned char edata[len];//输出buff
|
||||
memset(edata,0xff,len);
|
||||
|
||||
mbedtls_aes_context ctx; //创建结构体
|
||||
mbedtls_aes_init(&ctx); //初始化
|
||||
mbedtls_aes_setkey_dec(&ctx, key, KEY_LEN * 8); //设置密钥
|
||||
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, len, iv, data, edata); //加密
|
||||
mbedtls_aes_free(&ctx); //释放结构体
|
||||
|
||||
// LOG_HEX("de",16,edata,len);
|
||||
size_t rst = len-edata[len-1];
|
||||
edata[rst]='\0';
|
||||
// LOG_HEX("de",16,edata,len);
|
||||
rt_memcpy(dout, edata, rst+1);
|
||||
|
||||
return rst;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密数据
|
||||
* @param din 待解密数据
|
||||
* @param len 待解密数据长度
|
||||
* @param dout 解密后数据
|
||||
* @return 解密后数据长度
|
||||
*/
|
||||
size_t decryp_data(const uint8_t *din, size_t len, uint8_t *dout)
|
||||
{
|
||||
size_t nlen = aes_128_cbc_pkcs7_de(din, len, dout);
|
||||
return nlen;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
* @param din 待加密数据
|
||||
@ -88,20 +123,22 @@ size_t cryp_data(const uint8_t *din, int len, uint8_t *dout)
|
||||
}
|
||||
void aes_string_test(int argc, char **argv)
|
||||
{
|
||||
|
||||
rt_uint8_t data[128];
|
||||
size_t len;
|
||||
rt_uint8_t str[100];
|
||||
if (argc == 1)
|
||||
{
|
||||
len=aes_128_cbc_pkcs7("Test String", strlen("Test String"), data);
|
||||
len=cryp_data("Test String", strlen("Test String"), data);
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
len=aes_128_cbc_pkcs7(argv[1], strlen(argv[1]), data);
|
||||
len=cryp_data(argv[1], strlen(argv[1]), data);
|
||||
}
|
||||
for (size_t var = 0; var < len; ++var) {
|
||||
rt_kprintf("0x%02X ", data[var]);
|
||||
}
|
||||
rt_kprintf("\n---DONE.---\n");
|
||||
LOG_HEX("cry",16,data,len);
|
||||
len = decryp_data(data, len, str);
|
||||
rt_kprintf("decrypt str is '%s'\n", str,len);
|
||||
// rt_kprintf("\n---DONE.---\n");
|
||||
|
||||
}
|
||||
/**
|
||||
|
@ -11,5 +11,6 @@
|
||||
#define CRYP_H_
|
||||
|
||||
void aes_file(const char *fin, const char *fout);
|
||||
size_t cryp_data(const uint8_t *din, int len, uint8_t *dout);
|
||||
size_t cryp_data(const uint8_t *din, size_t len, uint8_t *dout);
|
||||
size_t decryp_data(const uint8_t *din, size_t len, uint8_t *dout);
|
||||
#endif /* CRYP_H_ */
|
||||
|
@ -402,6 +402,14 @@ static int getAndCheckLoc(uint8_t *dout, size_t pairCnt)
|
||||
{
|
||||
uint8_t loc[200];
|
||||
size_t cnt = getLoc(loc,pairCnt);
|
||||
if (!cnt) {
|
||||
LOG_W("位置信息还未准备好。");
|
||||
}
|
||||
else {
|
||||
memcpy(dout,loc,cnt);
|
||||
}
|
||||
return cnt;
|
||||
|
||||
while (!cnt)
|
||||
{
|
||||
static uint8_t i=0;
|
||||
@ -590,7 +598,7 @@ int isInFence(uint8_t *loc);
|
||||
* 打包并检测位置数据 默认10s更新一次
|
||||
* @return
|
||||
*/
|
||||
static void packAndChkLoc_thread_entry(void *parameter)
|
||||
static void packAndSendLoc_thread_entry(void *parameter)
|
||||
{
|
||||
while (isTCPok())
|
||||
{
|
||||
@ -653,7 +661,7 @@ static void packAndChkLoc_thread_entry(void *parameter)
|
||||
// }
|
||||
//}
|
||||
|
||||
void initUpSendLoc()
|
||||
void upSendLoc()
|
||||
{
|
||||
// isReadyToSendLoc = rt_sem_create("SendLoc", 0, RT_IPC_FLAG_PRIO);
|
||||
// repLoc = rt_timer_create("repLoc", cb_upSendLoc,
|
||||
@ -673,7 +681,7 @@ void initUpSendLoc()
|
||||
// return;
|
||||
// }
|
||||
|
||||
rt_thread_t thread = rt_thread_create("PCLoc", packAndChkLoc_thread_entry, RT_NULL, 1024 * 1, 27, 10);
|
||||
rt_thread_t thread = rt_thread_create("PCLoc", packAndSendLoc_thread_entry, RT_NULL, 1024 * 1, 27, 10);
|
||||
/* 创建成功则启动线程 */
|
||||
if (thread != RT_NULL)
|
||||
{
|
||||
@ -1265,7 +1273,7 @@ int isEthUP()
|
||||
MSH_CMD_EXPORT(d_getFreeSpace,getFreeSpace);
|
||||
MSH_CMD_EXPORT(selfTest,sysSelfTest);
|
||||
MSH_CMD_EXPORT(d_packLocMsg,dpackLocMsg);
|
||||
MSH_CMD_EXPORT(initUpSendLoc,initUpSendLoc);
|
||||
MSH_CMD_EXPORT(upSendLoc,采集并发送位置信息);
|
||||
MSH_CMD_EXPORT(d_cacheData,d_cacheData);
|
||||
MSH_CMD_EXPORT(d_getFileSize,d_getFileSize);
|
||||
MSH_CMD_EXPORT(isEthUP,isEthUP);
|
||||
|
@ -150,9 +150,10 @@ extern SYS_CFG scfg;
|
||||
|
||||
void initcfg()
|
||||
{
|
||||
// updatecfg();
|
||||
memset(rstInfo,0x3f,RST_LEN);//初始化
|
||||
bootstamp = rt_tick_get_millisecond();
|
||||
}
|
||||
INIT_APP_EXPORT(initcfg);
|
||||
|
||||
/**
|
||||
* 更新统计信息
|
||||
|
Loading…
Reference in New Issue
Block a user