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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-06-2008, 04:10 PM
|
#1
|
|
Member
Registered: Jun 2004
Posts: 233
Rep:
|
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?
|
|
|
|
08-08-2008, 12:03 PM
|
#2
|
|
Senior Member
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,052
Rep:
|
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;
}
|
|
|
|
08-08-2008, 04:48 PM
|
#3
|
|
Member
Registered: Jun 2004
Posts: 233
Original Poster
Rep:
|
oh I love sleep walking (programming), so daft at times... big cheers!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:27 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|