解决openssl库aes加密结果和网页不一致问题

使用的是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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值