LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2008, 04:10 PM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
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?
 
Old 08-08-2008, 12:03 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
your passing the do_encrypt swapped, should be like this

Code:
int main (int argc, char *argv[])
{
	int iret;
	iret = do_crypt("test.txt", "test_out.txt", 1);
	
	iret = do_crypt("test_out.txt", "test_out_d.txt", 0);
	return 0;
}
 
Old 08-08-2008, 04:48 PM   #3
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
oh I love sleep walking (programming), so daft at times... big cheers!
 
  


Reply

Tags
openssl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
oops openssl-0.9.8e over openssl-0.9.8d bad install now 2 copies? rcorkum Slackware 4 06-29-2007 01:58 AM
openssl easy_coder Linux - Software 1 09-08-2006 09:08 PM
Openssl velan Programming 1 05-16-2005 12:28 AM
OpenSSL 0.9.6k kojiroh Solaris / OpenSolaris 2 10-09-2003 10:51 AM
openssl sailaw Linux - General 1 01-25-2003 03:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration