1. 程式人生 > >使用OpenSSL庫的AES加解密

使用OpenSSL庫的AES加解密

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 [email protected] $^ $(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]