【AES】使用OpenSSL库的AES加解密
来源:互联网 发布:贵州广电网络有线电视 编辑:程序博客网 时间:2024/04/28 18:52
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>
-
-
-
-
- int main(int argc, char **argv)
- {
- char Source[1024];
- char *InputData=NULL;
- char *EncryptData=NULL;
- char *DecryptData=NULL;
-
- unsigned char Key[AES_BLOCK_SIZE+1];
- unsigned char ivec[AES_BLOCK_SIZE];
- 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);
-
-
- 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);
-
- InputData = (char *)calloc(SetDataLen+1, sizeof(char));
- if(InputData == NULL)
- {
- 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)
- {
- fprintf(stderr, "Unable to allocate memory for EncryptData\n");
- exit(-1);
- }
-
- DecryptData = (char *)calloc(SetDataLen+1, sizeof(char));
- if(DecryptData == NULL)
- {
- 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]