最近在学习openssl的东西,看到用BIO_f_cipher可以完成加密操作。参考openssl编程一书进行了实验,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/bio.h>
int main(void)
{
BIO *bc = NULL, *b = NULL;
char userkey[EVP_MAX_KEY_LENGTH];
char iv[EVP_MAX_IV_LENGTH];
unsigned char *date = malloc(AES_BLOCK_SIZE*3);
unsigned char *encrypt = malloc(AES_BLOCK_SIZE*6);
unsigned char *plain = malloc(AES_BLOCK_SIZE*6);
int ret;
int len = 0;
memset((void*)userkey, 'k', EVP_MAX_KEY_LENGTH);
memset((void*)iv, 'i', EVP_MAX_IV_LENGTH);
memset((void*)date, 'p', AES_BLOCK_SIZE*3);
memset((void*)encrypt, 0, AES_BLOCK_SIZE*6);
memset((void*)plain, 0, AES_BLOCK_SIZE*6);
bc = BIO_new(BIO_f_cipher());
BIO_set_cipher(bc,EVP_aes_256_ecb(), userkey, iv, 1);
b = BIO_new(BIO_s_null());
b = BIO_push(bc,b);
len = BIO_write(b, date, AES_BLOCK_SIZE*3);
printf("result: %d\n",BIO_get_cipher_status(b));
BIO_flush(b);
printf("write len: %d\n", len);
len = BIO_read(b, encrypt, AES_BLOCK_SIZE*6);
printf("result: %d\n",BIO_get_cipher_status(b));
printf("read len: %d\n", len);
BIO_free(b);
return 0;
}打印结果如下:
result: 1
write len: 48
result: 1
read len: 16
aes加密前后的长度应该一致,加密前长度是48字节,读出来只有16个字节?
BIO的用法不熟悉,不知道是不是用法有问题,请各位大大帮忙看下