#include // error #include // error #include void encryptWithAES(const unsigned char* plaintext, size_t plaintext_len, const unsigned char key[AES_BLOCK_SIZE], unsigned char* ciphertext) { AES_KEY aes; // 初始化加密密钥 if (AES_set_encrypt_key(key, 128, &aes) < 0) { // 错误处理:密钥长度不正确等 } // 分配足够的空间来存储结果,因为AES是块加密算法,需要对齐数据块 unsigned char encrypted[AES_BLOCK_SIZE]; // 对明文分块加密(这里假设明文已经填充为完整的块) for (size_t i = 0; i < plaintext_len; i += AES_BLOCK_SIZE) { AES_encrypt(plaintext + i, encrypted, &aes); memcpy(ciphertext + i, encrypted, AES_BLOCK_SIZE); } } int main() { unsigned char plaintext[16] = { /* 填充原始数据 */ }; unsigned char key[AES_BLOCK_SIZE] = { /* 填充密钥 */ }; unsigned char ciphertext[16]; // 用于存储加密后的数据 encryptWithAES(plaintext, sizeof(plaintext), key, ciphertext); // ... 然后可以进一步处理密文 ... return 0; }