使用OpenSSL庫的AES加解密

jopen 11年前發布 | 70K 次閱讀 OpenSSL 安全相關

AesTest.cpp

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <openssl/aes.h>

//g++ -g -o -Wall -m64 AesTest AesTest.cpp -lssl -lcrypto
//g++ -g -o -Wall AesTest AesTest.cpp -lssl -lcrypto

int main(int argc, char **argv)
{//由于與直接對接用的char,那么加解密要強制轉換
    char Source[1024];
    char *InputData=NULL;
    char *EncryptData=NULL;
    char *DecryptData=NULL;

    unsigned char Key[AES_BLOCK_SIZE+1];    //建議用unsigned char
    unsigned char ivec[AES_BLOCK_SIZE];     //建議用unsigned char
    AES_KEY AesKey;

    int DataLen=0,SetDataLen=0, i;

    memset(Source, 0x00, sizeof(Source));
    strcpy(Source, "1234567890abcde");  //要加密的數據
    DataLen = strlen(Source);

    memset(Key, 0x00, sizeof(Key));
    memcpy(Key, "0123456789abcdef", AES_BLOCK_SIZE);

 // set the encryption length
    SetDataLen = 0;
    if ((DataLen%AES_BLOCK_SIZE) == 0)
    {
        SetDataLen = DataLen;
    }
    else
    {
        SetDataLen = ((DataLen/AES_BLOCK_SIZE)+1) * AES_BLOCK_SIZE;
    }
    printf("SetDataLen:%d...\n", SetDataLen);   //取16的倍數

    InputData = (char *)calloc(SetDataLen+1, sizeof(char));
    if(InputData == NULL)   //注意要SetDataLen+1
    {
        fprintf(stderr, "Unable to allocate memory for InputData\n");
        exit(-1);
    }
    memcpy(InputData, Source, DataLen);

    EncryptData = (char *)calloc(SetDataLen+1, sizeof(char));
    if(EncryptData == NULL) //注意要SetDataLen+1
    {
        fprintf(stderr, "Unable to allocate memory for EncryptData\n");
        exit(-1);
    }

    DecryptData = (char *)calloc(SetDataLen+1, sizeof(char));
    if(DecryptData == NULL) //注意要SetDataLen+1
    {
        fprintf(stderr, "Unable to allocate memory for DecryptData\n");
        exit(-1);
    }

    memset(&AesKey, 0x00, sizeof(AES_KEY));
    if(AES_set_encrypt_key(Key, 128, &AesKey) < 0)
    {//設置加密密鑰
        fprintf(stderr, "Unable to set encryption key in AES...\n");
        exit(-1);
    }

    for(i=0; i<AES_BLOCK_SIZE; i++)
    {//必須要有
        ivec[i] = 0;
    }
    //加密
    AES_cbc_encrypt((unsigned char *)InputData, (unsigned char *)EncryptData, 
        SetDataLen, &AesKey, ivec, AES_ENCRYPT);   

    memset(&AesKey, 0x00, sizeof(AES_KEY));
    if(AES_set_decrypt_key(Key, 128, &AesKey) < 0)
    {//設置解密密鑰
        fprintf(stderr, "Unable to set encryption key in AES...\n");
        exit(-1);
    }

    for(i=0; i<AES_BLOCK_SIZE; i++)
    {//必須要有
        ivec[i] = 0;
    }
    //解密
    AES_cbc_encrypt((unsigned char *)EncryptData, (unsigned char *)DecryptData, 
        SetDataLen, &AesKey, ivec, AES_DECRYPT); 

    printf("DecryptData:%s...\n", (char *)DecryptData);

    if(InputData != NULL)
    {
        free(InputData);
        InputData = NULL;
    }

    if(EncryptData != NULL)
    {
        free(EncryptData);
        EncryptData = NULL;
    }

    if(DecryptData != NULL)
    {
        free(DecryptData);
        DecryptData = NULL;
    }

    exit(0);
}

Makefile

PROC_INC1=/usr/include

INC_DIR=-I$(PROC_INC1)

LIB_DIR= 

#-lssl -lcrypto,是openssl的兩個庫
All_LIB=-lssl -lcrypto


CC=g++

default: AesTest

AesTest:AesTest.o
    $(CC) -o $@ $^ $(INC_DIR) $(LIB_DIR) $(All_LIB)

.cpp.o:
    $(CC) -c -g -Wall -D_GNU_SOURCE $(INC_DIR) $<

clean:
    rm -f *.o AesTest
附: OpenSSL支持多種不同的加密算法
加密:
AES, Blowfish, Camellia, SEED, CAST-128, DES, IDEA, RC2, RC4, RC5, Triple DES, GOST 28147-89[4]
散列函數:
MD5, MD2, SHA-1, SHA-2, RIPEMD-160, MDC-2, GOST R 34.11-94[4]
公開密鑰加密:
RSA, DSA, Diffie–Hellman key exchange, Elliptic curve, GOST R 34.10-2001[4]

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!