使用的是openssl1.1.1 实测默认是没有补全的,修改填充模式解决问题
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
int main(int argc, char *argv[]) {
char s_user[48] = "vendor#988fe0300d67#1711058893";
char s_temp_password[64] = {0};
char s_aes_key[33] = "3077861f54b74385a303af5d2c7ae74e";
char s_aes_ives[17] = "300d671711058893";
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
printf("Failed to create EVP_CIPHER_CTX\n");
return 1;
}
// 设置AES-256-CBC加密算法
const EVP_CIPHER *cipher_type = EVP_aes_256_cbc();
if (EVP_EncryptInit_ex(ctx, cipher_type, NULL, (const unsigned char*)s_aes_key, (const unsigned char*)s_aes_ives) != 1) {
printf("Failed to initialize encryption\n");
return 1;
}
// 设置填充模式为PKCS7填充
if (EVP_CIPHER_CTX_set_padding(ctx, 1) != 1) {
printf("Failed to set padding mode\n");
return 1;
}
int out_len;
if (EVP_EncryptUpdate(ctx, (unsigned char*)s_temp_password, &out_len, (const unsigned char*)s_user, strlen(s_user)) != 1) {
printf("Encryption failed\n");
return 1;
}
int final_len;
if (EVP_EncryptFinal_ex(ctx, (unsigned char*)(s_temp_password + out_len), &final_len) != 1) {
printf("Final encryption failed\n");
return 1;
}
EVP_CIPHER_CTX_free(ctx);
printf("strlen(s_user): %d, strlen(s_temp_password): %d\n", strlen(s_user), out_len + final_len);
printf("s_temp_password: %s\n", s_temp_password);
//没加base64库,直接用ubuntu命令转
FILE *p_file = fopen("./6test-base64","wb+");
fwrite(s_temp_password,1,strlen(s_temp_password),p_file);
fclose(p_file);
char command[100] = "base64 6test-base64";
char output[1000];
FILE *fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n");
return 1;
}
// 读取命令输出
while (fgets(output, sizeof(output), fp) != NULL) {
printf("%s", output);
}
return 0;
}