okidokey, the code is attached.
The encryption/decryption function is in do_crypt.c

thanks
Stella

On Mon, May 27, 2002 at 06:03:46PM +0200, Aleix Conchillo wrote:
> hi stella,
> 
> could you please post your sample code?
> 
> best regards,
> 
> aleix
> 
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [EMAIL PROTECTED]
> Automated List Manager                           [EMAIL PROTECTED]
OPENSSLDIR=/usr/local/ssl
LIBS = -lm -L$(OPENSSLDIR) -lssl -lcrypto -lefence
oflags = -c
CFLAGS = -pedantic -ansi -W -Wall -g
CC = gcc
exec = crypt

default: main.o do_crypt.o parse_keyfile.o
        $(CC) -o ${exec} main.o do_crypt.o parse_keyfile.o -o crypt ${LIBS}

main.o:      main.c do_crypt.c parse_keyfile.c header.h
        ${CC} -c ${CFLAGS} ${INCDIR} main.c do_crypt.c parse_keyfile.c


clean:
        rm -f core *.o
#include "header.h"

#define EVP_MAX_BLOCK_LENGTH 6
FILE *out;


char* do_crypt(char *i_p, int docrypt) {

        unsigned char outbuf[1024+EVP_MAX_BLOCK_LENGTH];
        unsigned char outbuf_final[1024];
        int outlen, inlen;
        unsigned char iv[] = "12345678";

        EVP_CIPHER_CTX ctx;
        
        inlen = strlen(i_p);
        EVP_CIPHER_CTX_init(&ctx);
        EVP_CipherInit(&ctx, EVP_des_ede3(), NULL, NULL, docrypt);
        EVP_CIPHER_CTX_set_key_length(&ctx, key_length);
        EVP_CipherInit(&ctx, NULL, key, iv, docrypt);
        if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, (unsigned char *)i_p, inlen)) {
                fprintf(stderr, "Error crypting in EVP_CipherUpdate\n");
        exit(10);
        }
        if(!EVP_CipherFinal(&ctx, outbuf_final, &outlen)) {
                fprintf(stderr, "Error crypting in EVP_CipherFinal\n");
        exit(11);
        }
        sprintf(return_out,"%s%s", outbuf, outbuf_final);
        fwrite(return_out,1,outlen, stdout);
        fwrite("\n",1,1,stdout);

        EVP_CIPHER_CTX_cleanup(&ctx);
        return return_out;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/syslog.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <openssl/des.h>
#include <openssl/evp.h>
#include <openssl/sha.h>

#define BUFSIZE 1024

struct stat tmp;

int f_map_encrypt_p;
FILE *f_session_key_p;

char *session_key_filename;

char *current_line;

char *edata_a, *edata_b;
char *data_a, *data_b;

int docrypt;
int key_length;
char *session_key;
unsigned char key[BUFSIZE];
char *return_out;

char* parse_keyfile(char *current_line);
char* do_crypt(char *i_p, int docrypt);
key : 64556564446546465546546444555665
                           
                           
                           
#include "header.h"


int main(int argc, char** argv) {

        int opt, i=0, j=0;
        char *temp2;


        /* COMMAND LINE OPTION HANDLING */
        while((opt = getopt(argc, argv, "k:a:b:"))!=-1)
                switch (opt) {
                        case 'k':
                                if(optarg) {
                                        session_key_filename = malloc(64);
                                        strncpy(session_key_filename, optarg, 64);
                                        if((stat(optarg, &tmp)!=0)) {
                                                fprintf(stderr,"Error: Illegal 
filename for keyfile\n");
                                                exit(1);
                                        }
                                }
                                break;
                        case 'a':
                                if(optarg) {
                                        edata_a = malloc(64);
                                        strncpy(edata_a, optarg, 64);
                                }
                                break;
                        case 'b':
                                if(optarg) {
                                        edata_b = malloc(64);
                                        strncpy(edata_b, optarg, 64);
                                }
                                break;
                        case '?':
                        default:
                                printf("Usage: -k session_key_file\n");
                                exit(2);
                }


        printf("0: %s\t1: %s\n", edata_a, edata_b);


        if (!session_key_filename) {
                fprintf(stderr, "Error: Missing option -k\n");
                printf("Usage: -k session_key_file\n");
                exit(4);
        }



        if((return_out =(char *)malloc((BUFSIZE)*sizeof(char)))==NULL) {
                fprintf(stderr, "Error: malloc error\n");
        exit(8);
    }




        /* IN HERE GOES DECRYPTION OF PINS */
        if((current_line =(char *)malloc(BUFSIZE*sizeof(char)))==NULL) {
                fprintf(stderr, "Error: malloc error\n");
                exit(8);
        }

        /* read in session key from file */
        if((f_session_key_p=fopen(session_key_filename, "r"))==NULL) {
        fprintf(stderr,"\nError reading %s\n", session_key_filename);
                exit(5);
        }
        printf("Reading key from '%s'...\n", session_key_filename);

        j = 0;
        /* Read in lines, skip blank lines*/
        while(j!=1) {
                fgets(current_line, BUFSIZE*sizeof(char), f_session_key_p); 
                if(strcmp(current_line,"\n")==0) {
                        /* set flag to skip blank line*/
                        j = 0;
                } else {
                        j = 1;
                        session_key = parse_keyfile(current_line);
                        key_length = strlen(session_key);
                        printf("...session_key : %s\n", session_key);
                        printf("...key length: %d\n", key_length);
                }
        } 
        sprintf((char*)key, "%s", (unsigned char*)session_key);

        /* crypt data */
        printf("\ni : %d\n", i);
        printf("Encrypting data...\n");
        printf("edata : %s\n", edata_a);
        temp2 = do_crypt(edata_a, 1);
        edata_a = temp2;
        printf("Decrypting data...\n");
        temp2 = do_crypt(edata_a, 0);
        data_a = temp2;
        i++;

        printf("\ni : %d\n", i);
        printf("Encrypting data...\n");
        printf("edata : %s\n", edata_b);
        temp2 = do_crypt(edata_b, 1);
        edata_b = temp2;
        printf("Decrypting data...\n");
        temp2 = do_crypt(edata_b, 0);
        data_b = temp2;




        
/* TIDYING UP */
        /* freeing memory */
        
        if( fclose(f_session_key_p) != 0)
                fprintf(stderr,"Error: Couldn't close session_key file: %s\n", 
session_key_filename);

        /* exiting */
        exit(0);
}
#include "header.h"


char* parse_keyfile(char *temp) {
        
        char *ptr1, *ptr2, *ptr3;

        /* search for first instance of the end of a line and point ptr1 to it */
        ptr1=strstr(temp, ":");
        /* grab line data */
        ptr1+=1;   /* move pointer past ':' */
        ptr1+=1;   /* move pointer past ' ' */
        ptr2 = strstr(ptr1,"\n");
        if(ptr2==0) {
                printf("End of Line not found\n");
                exit(6);
        }
        *ptr2='\0';             /* set what ptr2 points to, to NULL temporarily */

        ptr3 = (char *)malloc(strlen(ptr1)+1); 
        strcpy(ptr3, ptr1);
        *ptr2='\n';                     /* set ptr2 pointing back to '\n' */

        return ptr3;            /* return result */
}

Reply via email to