Опитвам се да накарам функция bowfish да работи за прости буфери на 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;
}
всяка помощ ще бъде много ценена.
outbuf+db
във вашия оригинален код, който трябва да промените на(*output)+db
. 13.07.2014malloc
, ако не следвате връзката в моя отговор и не прочетете защо не трябва да предаватеmalloc
и как да поправите тези предупреждения. 13.07.2014