Files
bishe-STM32/Hardware/Crypto.h
T
2026-05-04 21:17:42 +08:00

40 lines
1.1 KiB
C

#ifndef _CRYPTO_H_
#define _CRYPTO_H_
#include <stdint.h>
#include <stddef.h>
/**
* @brief 加密JSON数据
* @param json_data 原始JSON字符串
* @param encrypted_output 输出的加密Base64字符串(需要预分配足够空间)
* @return 加密后的Base64字符串长度
*
* 注意:encrypted_output缓冲区至少需要 (strlen(json_data) / 16 + 1) * 16 * 4/3 + 10 字节
*/
uint32_t Crypto_EncryptJSON(const char* json_data, char* encrypted_output);
/**
* @brief 解密Base64编码的AES-CBC数据
* @param encrypted_base64 加密后的Base64字符串
* @param plain_output 输出明文缓冲区
* @param output_size 输出缓冲区大小
* @return 明文字节数,失败返回0
*/
uint32_t Crypto_DecryptJSON(const char* encrypted_base64, char* plain_output, uint32_t output_size);
/**
* @brief 获取加密后数据的最大长度
* @param json_length JSON字符串长度
* @return 加密后的最大长度
*/
#define CRYPTO_MAX_ENCRYPTED_SIZE(json_len) (((json_len) / 16 + 1) * 16 * 4 / 3 + 10)
/**
* @brief 初始化加密模块
* 设置密钥和IV(初始化向量)
*/
void Crypto_Init(void);
#endif // _CRYPTO_H_