C语言openssl aes-128-ecb加解密

CodingEmbedded

openssl aes-128-ecb方式对密码进行md5后的加解密
openssl版本:1.0.2g 其他的自行验证
编译:gcc aes_128_ecb.c -o aes_128_ecb -lssl -lcrypto

执行结果:
./aes_128_ecb
passwd: 12345
strMd516: A46B755EA8F1B4DD
strBase64Encrypt: cFeeVowosk4cR5gIg7i6ZQ==
aes_128_ecb_decrypt OK
strAESDecrypt passwd is: 12345
/*********************************************
**   aes_128_ecb.c
**   encrypte decrypt the password
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/evp.h>  
#include <openssl/bio.h>  
#include <openssl/buffer.h>
#include <openssl/md5.h>

int aes_128_ecb_encrypt(char *in, char *key, char *out) {
    int ret = 0, len = 0, len1 = 0, len2 = 0;
    unsigned char *result = NULL;
    EVP_CIPHER_CTX *ctx;
    
    ctx = EVP_CIPHER_CTX_new();
    ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key, NULL);
    
    if (ret != 1) {
        printf("EVP_EncryptInit_ex error\n");
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }
    result = (unsigned char *)malloc(AES_BLOCK_SIZE*64);

    ret = EVP_EncryptUpdate(ctx, result, &len1, (const unsigned char*)in, strlen(in));

    if (ret != 1) {
        printf("EVP_EncryptUpdate error\n");
        EVP_CIPHER_CTX_free(ctx);
        free(result);
        return 0;
    }
    ret = EVP_EncryptFinal_ex(ctx, result + len1, &len2);
    if (ret != 1) {
        printf("EVP_EncryptFinal_ex error\n");
        EVP_CIPHER_CTX_free(ctx);
        free(result);
        return 0;
    }

    while (len < (len1+len2)) {
        out[len] = result[len];
        len++;
    }
    EVP_CIPHER_CTX_free(ctx);
    free(result);
    return (len1+len2);
}
int aes_128_ecb_decrypt(char *in, char *key, char *out) {
    int ret = 0, len = 0, len1 = 0, len2 = 0;
    unsigned char *result = NULL;
    
    EVP_CIPHER_CTX *ctx;
    ctx = EVP_CIPHER_CTX_new();
    ret = EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key, NULL);
    if (ret != 1) {
        printf("EVP_DecryptInit_ex error\n");
        EVP_CIPHER_CTX_free(ctx);
        return 0;
    }
    result = (unsigned char *)malloc(AES_BLOCK_SIZE*64);

    ret = EVP_DecryptUpdate(ctx, result, &len1, (const unsigned char*)in,get_str_len(in));//不可使用strlen求取,字符串中可能含有结束符等

    if (ret != 1) {
        printf("EVP_DecryptUpdate error\n");
        EVP_CIPHER_CTX_free(ctx);
        free(result);
        return 0;
    }
    ret = EVP_DecryptFinal_ex(ctx, result + len1, &len2);
    if (ret != 1) {
        printf("EVP_DecryptFinal_ex error\n");
        EVP_CIPHER_CTX_free(ctx);
        free(result);
        return 0;
    }
    while (len < (len1+len2)) {
        out[len] = result[len];
        len++;
    }
    EVP_CIPHER_CTX_free(ctx);
    free(result);
    return 1;
} 

// base64 编码
char *base64_encode(const char *buffer, int length) {
    BIO *bmem = NULL;
    BIO *b64 = NULL;
    BUF_MEM *bptr;
    char *buff = NULL;
    
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bmem = BIO_new(BIO_s_mem());
    b64 = BIO_push(b64, bmem);
    BIO_write(b64, buffer, length);
    BIO_flush(b64);
    BIO_get_mem_ptr(b64, &bptr);
    BIO_set_close(b64, BIO_NOCLOSE);

    buff = (char *)malloc(bptr->length + 1);
    memcpy(buff, bptr->data, bptr->length);
    buff[bptr->length] = 0;
    BIO_free_all(b64);

    return buff;
}

// base64 解码
char *base64_decode(char *input, int length) {
    BIO *b64 = NULL;
    BIO *bmem = NULL;
    char *buffer = NULL;
    buffer = (char *)malloc(length);
    memset(buffer, 0, length);
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bmem = BIO_new_mem_buf(input, length);
    bmem = BIO_push(b64, bmem);
    BIO_read(bmem, buffer, length);
    BIO_free_all(bmem);

    return buffer;
}

int md5_16(const char *src, char *out) {
    unsigned char c[MD5_DIGEST_LENGTH];
    int i = 0;
    MD5_CTX ctx;
    MD5_Init(&ctx);
    MD5_Update(&ctx, src, strlen(src));
    MD5_Final(c, &ctx);

    for (i = 0; i < MD5_DIGEST_LENGTH / 2; i++) {
        sprintf(out+i*2, "%02X", c[i+4]);
    }
    return 1;
}

int get_str_len(const char *in) {
    int num = 0;
    if(in == NULL) {
        return 0;
    }
    while (!((*(in + num) == NULL) && (*(in + num + 1) == NULL) \
    && (*(in + num + 2) == NULL) && (*(in + num + 3) == NULL) \
    && (*(in + num + 4) == NULL)&& (*(in + num + 5) == NULL) \
    && (*(in + num + 6) == NULL)&& (*(in + num + 7) == NULL))) {
        num++;
    }
    return num;
}


int main(int argc, char const *argv[])
{
    char strAESEncrypt[1024] = {0}, strAESDecrypt[1024] = {0}, *strBase64Encrypt = NULL, *pStrBase64Decrypt = NULL, strMd516[17] = {0};
    int nAESEncryptLen = 0;
    char strKey[128] = "0123456789ABCDEF";
    char *passwd = "12345";

    md5_16(strKey, strMd516);
    printf("passwd: %s\n", passwd);
    printf("strMd516: %s\n", strMd516);
    // 加密部分
    nAESEncryptLen = aes_128_ecb_encrypt(passwd, strMd516, strAESEncrypt);
    strBase64Encrypt = base64_encode(strAESEncrypt, nAESEncryptLen);
    printf("strBase64Encrypt: %s\n", strBase64Encrypt);

    // 解密部分
    pStrBase64Decrypt = base64_decode(strBase64Encrypt, strlen(strBase64Encrypt));
    if (aes_128_ecb_decrypt(pStrBase64Decrypt, strMd516, strAESDecrypt) == 1) {
        printf("aes_128_ecb_decrypt OK\n");
        printf("strAESDecrypt passwd is: %s\n", strAESDecrypt);
    }

    return 0;
}
阅读 183

记录一下生活的点滴,工作上遇到的问题以及学习上的各类笔记

14 声望
3 粉丝
0 条评论
你知道吗?

记录一下生活的点滴,工作上遇到的问题以及学习上的各类笔记

14 声望
3 粉丝
文章目录
宣传栏