aes_128加密使用+base64编码(linux c语言)

该代码示例展示了如何使用OpenSSL库进行AES-128加密以及Base64编码和解码。在主函数中,定义了一个16字节的秘钥和字符串数据,进行了100000次的加密、Base64编码、解码以及解密操作,验证了整个流程的正确性。
摘要由CSDN通过智能技术生成

//gcc aes_128.c -lssl -lcrypto

#include  <string.h>
#include  <stdio.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <openssl/evp.h>
#include  <openssl/bio.h>
#include  <openssl/buffer.h>
#include  <openssl/aes.h>

int openssl_base64_encode(const unsigned char* in, int inlen, char* out, int* outlen)
{
    BIO* b64 = BIO_new(BIO_f_base64());
    BIO* bmem = BIO_new(BIO_s_mem());
    if (!b64 || !bmem) {
        fprintf(stderr, "fail to BIO_new\n");
        return -1;
    }
    b64 = BIO_push(b64, bmem);


    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); // ignore newlines, write everything in one line

    *outlen = BIO_write(b64, in, inlen);
    if (*outlen <= 0 || *outlen != inlen) {
        fprintf(stderr, "fail to BIO_write\n");
        return -1;
    }
    BIO_flush(b64);

    BUF_MEM* buf = NULL;
    BIO_get_mem_ptr(b64, &buf);
    *outlen = buf->length;
    memcpy(out, buf->data, *outlen);

    BIO_free_all(b64);
    return 0;
}

int openssl_base64_decode(const char* in, int inlen, unsigned char* out, int* outlen)
{
    BIO* b64 = BIO_new(BIO_f_base64());
    BIO* bmem = BIO_new_mem_buf(in, inlen);
    if (!b64 || !bmem) {
        fprintf(stderr, "fail to BIO_new\n");
        return -1;
    }
    b64 = BIO_push(b64, bmem);

    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); // ignore newlines, write everything in one line

    *outlen = BIO_read(b64, out, inlen);
    if (*outlen <= 0) {
        fprintf(stderr, "fail to BIO_read\n");
        return -1;
    }

    BIO_free_all(b64);
    return 0;
}

 
int aes_encrypt(char* in, char* key, char* out)
{
    if (!in || !key || !out)
    {
        return 0;
    }
 
    AES_KEY aes;
    if (AES_set_encrypt_key((unsigned char*)key, 128, &aes) < 0)
    {
        return 0;
    }
 
    int len = strlen(in), en_len = 0;
 
    while (en_len < len)
    {
        AES_encrypt((unsigned char*)in, (unsigned char*)out, &aes);
        in  += AES_BLOCK_SIZE;
        out += AES_BLOCK_SIZE;
        en_len += AES_BLOCK_SIZE;
    }
 
    return 1;
}
 
int aes_decrypt(char* in, char* key, char* out)
{
    if (!in || !key || !out)
    {
        return 0;
    }
 
    AES_KEY aes;
    if (AES_set_decrypt_key((unsigned char*)key, 128, &aes) < 0)
    {
        return 0;
    }
 
    int len = strlen(in), en_len = 0;
    while (en_len < len)
    {
        AES_decrypt((unsigned char*)in, (unsigned char*)out, &aes);
        in  += AES_BLOCK_SIZE;
        out += AES_BLOCK_SIZE;
        en_len += AES_BLOCK_SIZE;
    }
 
    return 1;
}
 
int main()
{
    //秘钥长度16位,最多一次转字符长度512个字节
    char *data="116.375309";
    char *key = "ABCD1234ABCD1234";
    char encrypt_buffer[2048] = {'\0'}, decrypt_buffer[2048] = {'\0'};
    char enbase64[256] = {'\0'};
    char debase64[256] = {'\0'};
    int len = 0;
    
    len = strlen(data);
    printf("明文长度:%d\n", len);
    time_t start_time = time(NULL);
    int count = 100000;
    int count_p = 100000;
    while(count--){

        
        aes_encrypt(data, key, encrypt_buffer);
        openssl_base64_encode(encrypt_buffer, strlen(encrypt_buffer), enbase64,&len);


        openssl_base64_decode(enbase64, strlen(enbase64), debase64,&len);
        aes_decrypt(debase64, key, decrypt_buffer);
        printf("value:%s\n", decrypt_buffer);
        
    }
    time_t end_time = time(NULL); 
    printf("aes_128; %d字节;循环%d次, %d秒\n",len, count_p,(end_time-start_time));
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值