rubadub |
08-06-2008 04:10 PM |
openssl
Hi, I wrote a little app to try out a bit of blowfish file enc/dec, it technically worked but the result was missing some data. So I went looking for an example and found this. Which I made into this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
int do_crypt(char *fin, char *fout, int do_encrypt)
{
FILE *in;
in = fopen(fin, "rb");
FILE *out;
out = fopen(fout, "wb");
/* Allow enough space in output buffer for additional block */
char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
/* Bogus key and IV: we'd normally set these from
* another source.
*/
EVP_CIPHER_CTX ctx;
unsigned char key[] = "0123456789";
unsigned char iv[] = "12345678";
/* Don't set key or IV because we will modify the parameters */
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);
/* We finished modifying parameters so now we can set key and IV */
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
for(;;)
{
inlen = fread(inbuf, 1, 1024, in);
if(inlen <= 0) break;
if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
}
if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(in);
fclose(out);
return 1;
}
int main (int argc, char *argv[])
{
int iret;
iret = do_crypt("test.txt", "test_out.txt", 0);
iret = do_crypt("test_out.txt", "test_out_d.txt", 1);
return 0;
}
Heres a makefile:
Code:
CC=cc
CFLAGS= -g -I../../include #-Wall
LIBS= -L../.. ../../libssl.a ../../libcrypto.a -lcrypto -ldl -pthread
EXAMPLES=main
all: $(EXAMPLES)
main: main.o
$(CC) -o main main.o $(LIBS)
clean:
rm -f $(EXAMPLES) *.o
Any idea whats wrong?
|