esp32 md5加密算法的使用Arduino/esp-idf

6 篇文章 0 订阅
2 篇文章 1 订阅

之前不太了解arduino,使用第三方md5库加密。

https://github.com/tzikis/ArduinoMD5https://github.com/tzikis/ArduinoMD5

#include "MD5.h"


//houyawei 2021.10.29
unsigned char* hash = MD5::make_hash((char *)md.c_str());
char *test = MD5::make_digest(hash, 16);
printf("test:%s\r\n",test);

生产环境遇到了问题,加密次数过多会导致esp32概率性崩溃,无法在程序内修复,只能看门狗重启或断电重启恢复。此异常导致串口不断有错误数据。

注意readme,循环调用的时候加free指针后,程序正常(最新编辑内容)

unsigned char* hash=MD5::make_hash("hello world");
//generate the digest (hex encoding) of our hash
char *md5str = MD5::make_digest(hash, 16);
//print it on our serial monitor
Serial.println(md5str);
//Give the Memory back to the System if you run the md5 Hash generation in a loop
free(md5str);
//free dynamically allocated 16 byte hash from make_hash()
free(hash);

之后看到arduino_esp32库里面自带md5加密算法的。

https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/MD5Builder.hhttps://github.com/espressif/arduino-esp32/blob/master/cores/esp32/MD5Builder.h

#include "MD5Builder.h"



MD5Builder md5;
md5.begin();
md5.add(md);
md5.calculate();
//const char *md5str = md5.toString().c_str();
//printf("%s\r\n",md5str);
//houyawei

使用官方算法后没有再遇到而esp32异常的问题。

参考链接:

https://github.com/esp8266/Arduino/issues/1349https://github.com/esp8266/Arduino/issues/1349

                                                                                                        ---------houyawei 2021.10.29

补:

之后直接在esp-idf使用md5加密,了解到乐鑫esp-idf框架集成开源的 mbedtls 加密库

以下测试可使用

        printf("MD5 加密\n");
        mbedtls_md5_context md5_ctx;

        unsigned char encrypt[] = "hello_worled1234";
        unsigned char decrypt[16];

        mbedtls_md5_init(&md5_ctx);
        mbedtls_md5_starts_ret(&md5_ctx);
        mbedtls_md5_update_ret(&md5_ctx, encrypt, strlen((char *)encrypt));
        mbedtls_md5_finish_ret(&md5_ctx, decrypt);

        printf("MD5加密前:[%s]\n", encrypt);
        printf("MD5加密后(32位):");
        for(int i = 0; i < 16; i++)
        {
        
            printf("%02x", decrypt[i]);
        }
        printf("\r\n");

        mbedtls_md5_free(&md5_ctx);
         //houyawei

当然也可以参考arduino-esp32项目内的方法写

string md5str(string &in){
  char out[33] = {0};
  mbedtls_md5_context _ctx;
  uint8_t i;
  uint8_t * _buf = (uint8_t*)malloc(16);
  if(_buf == NULL)
    return string(out);
  memset(_buf, 0x00, 16);
  mbedtls_md5_init(&_ctx);
  mbedtls_md5_starts_ret(&_ctx);
  mbedtls_md5_update_ret(&_ctx, (const uint8_t *)in.c_str(), in.length());
  mbedtls_md5_finish_ret(&_ctx, _buf);
  for(i = 0; i < 16; i++) {
    sprintf(out + (i * 2), "%02x", _buf[i]);
  }
  out[32] = 0;
  free(_buf);
  return string(out);
}


//使用
 string key = md5str(md);

参考:

ESP32学习笔记(47)——加密算法AES/MD5/SHA - it610.com一、简介1.1SSLSSL:(SecureSocketLayer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层。SSL通过互相认证、使用数字签名确保完整性、使用加密确保私密性,以实现客户端和服务器之间的安全通讯。该协议由两层组成:SSL记录协议和SSL握手协议。1.2TLSTLS:(TransportLayerSecurity,传输层安全协议),用于两个应用程序之间提https://www.it610.com/article/1457899174858108928.htm

https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/src/WebServer.cppicon-default.png?t=LA92https://github.com/espressif/arduino-esp32/blob/master/libraries/WebServer/src/WebServer.cpp                                                                                                        -------houyawei2021.12.09

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当 `mbedtls_cipher_crypt` 函数返回 `MBEDTLS_ERR_CIPHER_INVALID_PADDING` 错误时,通常是由于填充模式设置不正确导致的。在使用 AES-CBC 模式进行加密时,需要使用合适的填充模式来确保输入数据长度符合加密算法的要求。 一种常用的填充模式是 PKCS#7 填充,它会根据需要在输入数据的末尾填充适当数量的字节。您可以在调用 `mbedtls_cipher_setup` 函数之后,使用 `mbedtls_cipher_set_padding_mode` 函数来设置填充模式为 PKCS#7。 以下是修改后的代码示例: ```c #include "mbedtls/aes.h" #include "mbedtls/cipher.h" int main() { // 初始化 mbedtls 的上下文 mbedtls_aes_context aes_ctx; mbedtls_aes_init(&aes_ctx); unsigned char key[32] = {0}; // AES-256 的密钥长度为 32 字节 unsigned char iv[16] = {0}; // 初始化向量长度为 16 字节 unsigned char input[16] = "plaintext"; // 待加密的明文 unsigned char output[16] = {0}; // 存储加密后的密文 // 设置加密算法和模式 mbedtls_cipher_context_t cipher_ctx; mbedtls_cipher_init(&cipher_ctx); mbedtls_cipher_setup(&cipher_ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC)); // 设置填充模式为 PKCS#7 mbedtls_cipher_set_padding_mode(&cipher_ctx, MBEDTLS_PADDING_PKCS7); // 设置密钥和初始化向量 mbedtls_cipher_setkey(&cipher_ctx, key, 256, MBEDTLS_ENCRYPT); mbedtls_cipher_set_iv(&cipher_ctx, iv, 16); // 执行加密操作 int ret = mbedtls_cipher_crypt(&cipher_ctx, iv, 16, input, output); if (ret != 0) { // 处理加密错误 // ... } // 清理 mbedtls 的上下文 mbedtls_cipher_free(&cipher_ctx); mbedtls_aes_free(&aes_ctx); return 0; } ``` 在以上示例中,我们使用 `mbedtls_cipher_set_padding_mode` 函数将填充模式设置为 PKCS#7。确保您在设置填充模式之后再执行加密操作。如果仍然遇到填充错误,请检查输入数据的长度是否符合加密算法的要求,并确保密钥和初始化向量的长度正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值