我试图让一个弓形函数适用于简单的char缓冲区。当我尝试解密加密缓冲区并且在EVP_CipherFinal_ex()调用失败时,程序失败。
#include <string.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <openssl/blowfish.h>
#include <openssl/evp.h>
int do_crypt(unsigned char *inbuf, int inlen, unsigned char *outbuf, int *outlen, int do_encrypt) {
outbuf=(unsigned char*) malloc(inlen+EVP_MAX_BLOCK_LENGTH);
int tmplen=0;
unsigned char key[] = "0123456789";
unsigned char iv[] = "12345678";
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_bf_cbc(), NULL, NULL, NULL, do_encrypt);
EVP_CIPHER_CTX_set_key_length(&ctx, 10);
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
if(!EVP_CipherUpdate(&ctx, outbuf, outlen, inbuf, inlen)) {
/* Error */
printf("* update failed *\n");
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
int db=*outlen;
if(!EVP_CipherFinal_ex(&ctx, outbuf+db, &tmplen)) {
/* Error */
ERR_print_errors_fp(stderr);
printf("* finalise failed *\n");
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
(*outlen)=db+tmplen;
EVP_CIPHER_CTX_cleanup(&ctx);
return 1;
}
int main(int argc, char **argv) {
char *plain="ENCRYPT DECRYPT this string";
int plain_len=strlen(plain);
unsigned char *cipher;
int cipher_len;
printf("***** ENCRYPT *****\n");
if (!do_crypt((unsigned char*) plain, strlen(plain), cipher, &cipher_len, 1)) {
printf("failed to encrypt\n");
return 1;
}
char *decrypt;
int decrypt_len;
printf("***** DECRYPT *****\n");
if(!do_crypt( cipher ,cipher_len , decrypt, &decrypt_len, 0)) {
printf("failed to decrypt\n");
return 1;
}
printf("decrypt=\"%s\"\n",decrypt);
printf("decrypt_len=%d\n",decrypt_len);
return 0;
}
任何帮助将不胜感激。
答案 0 :(得分:1)
你有两个问题:首先是you should not cast the result of malloc
in C。这是EVP_CipherUpdate
崩溃的最可能原因。
第二个错误是C中的参数按值传递 ,这意味着它们被复制并且函数只有调用者传递的参数的副本。这意味着在do_crypt
函数中,当您分配给参数output
时,您只分配给函数内的本地副本,cipher
函数中的变量main
将不要改变。
可以通过将指针传递给指针来模拟通过引用传递来解决最后一个问题,并使用address-of运算符&
和解除引用*
运算符:
/* Note extra indirection */
/* | */
/* v */
int do_crypt(unsigned char *inbuf, int inlen, unsigned char **outbuf, int *outlen, int do_encrypt) {
...
*output = malloc(...);
...
if(!EVP_CipherUpdate(&ctx, *outbuf, outlen, inbuf, inlen)) { ... }
...
}
然后将其称为
do_crypt((unsigned char*) plain, strlen(plain), &cipher, &cipher_len, 1)