views:

75

answers:

3

How can I encrypt & decrypt binary files in C using OpenSSL?

I have a test program that encrypts and then decrypts the input it's given.

I executed my test program for text files, and the output is the same as the input, but when I execute my test program on a binary file the output is not the same as the input.

+1  A: 

Just guessing: you are using Windows and missed O_BINARY flag in file operations?

blaze
+1  A: 

Chances are you are using string functions like strlen() on the buffers you're reading. The OpenSSL functions work fine for binary files.

caf
+1  A: 

Without seeing your code we can only guess. But my first guess would be that your encryption or decryption routine is barfing on a \0 character or two within the binary file. The data must be treated as bytes not as character strings. (Same as the StrLen() problem mentioned elsewhere on this page.)

I'm not a C programmer(!) but the way I managed to get the encryption routines working within Delphi/Pascal was by downloading the OpenSSL source (in C) and stepping through the code for the openssl.exe application. Using the EVP_* functions became a whole lot easier once you work out how they do it themselves.

shunty