Source:
#include <stdio.h>#include <memory.h>#include <stdlib.h>#include <openssl/aes.h>intMainintargcChar*argv[]) {unsigned Charbuf[ -] ="1234567890ABCDE";unsigned Charbuf2[ -];unsigned Charbuf3[ -];//key is 0 unsigned Charaes_keybuf[ +] = {0}; Aes_key Aeskey;//Set Encryption keyAes_set_encrypt_key (Aes_keybuf,8*sizeof(AES_KEYBUF), &aeskey);//EncryptionAes_encrypt (Buf,buf2,&aeskey);printf("%s\n", BUF2);//Set the decryption keyAes_set_decrypt_key (Aes_keybuf,8*sizeof(AES_KEYBUF), &aeskey);//DecryptionAes_decrypt (Buf2, BUF3, &aeskey);printf("%s\n", BUF3);return 0;}
OpenSSL API Description
intAes_set_encrypt_key (ConstUnsignedChar*userkey,Const intBitsAes_key*key);# #设置加密密钥# # UserKey for user-defined keys# # Bits key length# # Generated key (encrypted function will be used)intAes_set_decrypt_key (ConstUnsignedChar*userkey,Const intBitsAes_key*key);# #设置解密密钥# # UserKey for user-defined keys# # Bits key length# # Generated key (decryption function is used)//Cryptographic functionsvoidAes_encrypt (ConstUnsignedChar*in, unsignedChar*out,ConstAes_key*key);//Decryption functionvoidAes_decrypt (ConstUnsignedChar*in, unsignedChar*out,ConstAes_key*key);
Personal Summary:
The OpenSSL API probably uses steps to
Set encryption key –> encrypt data
Set decryption key –> decrypt data
About the key, can be customized;
It is also important to note that encryption and decryption of the data buffer must be a multiple of 16 (OpenSSL internal implementation, the data will be read in 16 char, again, the above program will only encrypt 16 bytes of data ...) Need to encrypt a longer string, the program can be modified under it)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
AES Encryption with OpenSSL