IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> 使用mbedtls实现文件以及字符串的AES加密 -> 正文阅读

[嵌入式]使用mbedtls实现文件以及字符串的AES加密

  • 由于mbedtls主要支持TCP的TLS传输实现,由ARM公司加持,目前这个库的代码质量非常高,也可以用在其他加密解密领域,比如AES、DES、chacha20等加密,当然和libsodium实现互补,有些实现也必须用libsodium更好一些,不过这两个库的代码质量都很高。
  • 示例代码展现了AES-256-ECB的加密如何使用,由于库里面有很多加密支持,可以参考mbedtls github原始代码的cipher_wrap.c实现,这里面实现了统一API封装,方便使用,只需要初始化时给出对应的类型参数,即可获取相应handle方便使用。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mbedtls/net.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/debug.h"

unsigned char key[] = {
    0x01, 0x02, 0x34, 0x78, 0x78, 0x23, 0x34, 0x11,
    0x02, 0x03, 0x34, 0x78, 0x78, 0x23, 0x34, 0x12,
    0x03, 0x04, 0x34, 0x78, 0x78, 0x23, 0x34, 0x13,
    0x04, 0x05, 0x34, 0x78, 0x78, 0x23, 0x34, 0x14
};

#define DEBUG_LEVEL 1

static void add_pkcs_padding(unsigned char *output, size_t output_len,
                             size_t data_len)
{
    size_t padding_len = output_len - data_len;
    unsigned char i;

    for(i = 0; i < padding_len; i++)
        output[data_len + i] = (unsigned char) padding_len;
}

int main(int argc, char *argv[])
{
    int ret = 0;
    int i = 0;

    mbedtls_cipher_context_t cipher_ctx_enc;    /*!<  encryption context      */
    mbedtls_cipher_context_t cipher_ctx_dec;    /*!<  decryption context      */

    mbedtls_cipher_init(&cipher_ctx_enc);
    mbedtls_cipher_init(&cipher_ctx_dec);

#if defined(MBEDTLS_DEBUG_C)
    mbedtls_debug_set_threshold(DEBUG_LEVEL);
#endif

    printf("start cipher test\n");

    const mbedtls_cipher_info_t *cipher_info = mbedtls_cipher_info_from_string("AES-256-ECB");
    if((ret = mbedtls_cipher_setup(&cipher_ctx_enc,
                                   cipher_info)) != 0)
    {
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
        goto Cleanup;
    }

    if((ret = mbedtls_cipher_setup(&cipher_ctx_dec,
                                   cipher_info)) != 0)
    {
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
        goto Cleanup;
    }

    if((ret = mbedtls_cipher_setkey(&cipher_ctx_enc, key,
                                    cipher_info->key_bitlen,
                                    MBEDTLS_ENCRYPT)) != 0)
    {
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
        goto Cleanup;
    }

    if((ret = mbedtls_cipher_setkey(&cipher_ctx_dec, key,
                                    cipher_info->key_bitlen,
                                    MBEDTLS_DECRYPT)) != 0)
    {
        MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
        goto Cleanup;
    }

    size_t olen = 0;
    size_t index = 0;

    FILE *file = fopen("/tmp/test.txt", "r");
    if(!file)
    {
        exit(-1);
    }

    fseek(file, 0L, SEEK_END);
    int length = ftell(file);
    fseek(file, 0L, SEEK_SET);

    int plainLen = length;
    int padding = length % cipher_ctx_enc.cipher_info->block_size;
    if(padding > 0)
    {
        plainLen += (cipher_ctx_enc.cipher_info->block_size - padding);
    }
    printf("padding:%d plainLen:%d\n",padding,plainLen);
    uint8_t *message = (uint8_t *)calloc(plainLen, sizeof(uint8_t));
    if(!message)
    {
        ret = -1;
        goto Cleanup;
    }

    fread(message, sizeof(uint8_t), length, file);
    fclose(file);

    add_pkcs_padding(message, plainLen, length); //padding

    uint8_t *cipherText = (uint8_t *)calloc(plainLen, sizeof(char));
    if(!cipherText)
    {
        ret = -1;
        goto Cleanup;
    }

    printf("start file encrypt\n");
    while(index < plainLen)
    {
        if((ret = mbedtls_cipher_crypt(&cipher_ctx_enc,
                                       NULL,
                                       0,
                                       message + index, cipher_ctx_enc.cipher_info->block_size,
                                       cipherText + index, &olen)) != 0)
        {
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
            goto Cleanup;
        }
        index += cipher_ctx_enc.cipher_info->block_size;
    }

    printf("encrypt cipher text:\n");
    for(i=0;i<plainLen;i++)
    {
    	printf("%02X",cipherText[i]);
    }
    printf("\n");

    index = 0;
    memset(message, 0, plainLen);
    while(index < plainLen)
    {
        if((ret = mbedtls_cipher_crypt(&cipher_ctx_dec,
                                       NULL,
                                       0,
                                       cipherText + index, cipher_ctx_dec.cipher_info->block_size,
                                       message + index, &olen)) != 0)
        {
            MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
            goto Cleanup;
        }
        index += cipher_ctx_dec.cipher_info->block_size;
    }

    printf("decrypt file ok:\n");
    for(i=0;i<length;i++)
    {
    	printf("%c",message[i]);
    }
    printf("\n");

Cleanup:
    if(message)
        free(message);

    if(cipherText)
        free(cipherText);

    mbedtls_cipher_free(&cipher_ctx_enc);
    mbedtls_cipher_free(&cipher_ctx_dec);
    return 0;
}


  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-08-22 13:41:27  更:2021-08-22 13:41:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 2:46:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码