You are not logged in.

#1 2007-11-28 22:03:56

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Help with encryptation program (in C) [solved]

I'm writing this program more for study proposal, but it would be nice if it worked as it should. Can anyone help me with it?

It encrypts the content of the first argument using a XOR algorithm and a key given by the third argument. Here's the first problem: It's safier to use a tty login-like 'Password:', but I'm finding little information about how to do it. It seems that I should use getch(), an example would be welcome.

The second problem is encryptation by itself tongue. Let's say I have the file 'in' with 'Hello World' in it. If I use 'asd' as password, and the decrypt it, everything is fine, but if I use 'hello' as password, I can't decrypt it, it stops on H.

There two other TODOs, but I think I can handle them by myself.

/* crypt:
 * crypt input output key
 *
 * Author: André Ramaciotti da Silva
 *
 * This software opens the file specified by the first argument (input) and
 * cryptograph it, using a XOR algorithm and the key that was given as the
 * third argument.
 * TODO: change the key from an argument to a tty login-like 'Password: '.
 *
 *
 * The cryptographed string is stored in the file given by the second argument.
 * TODO: warn when there is a file with the name 'output'.
 *
 * This version don't work when one of the chars of the string is the same of
 * the key. When they both are XORed, they return a null char. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* TODO: include some of these functions in a static library. */

/* Cryptographs *text* using *key* and returns the result */
char *encrypt( const char *text, const char *key );

/* Returns a string with all the *fileptr*'s content. */
char *get_from_file( FILE *fileptr );

/* Opens the *filename* with the given *options*. If it succeeds, returns a
 * pointer to the file, else, abort the program execution. */
FILE *xfopen( const char *filename, const char *options );

/* Returns the size of the *fileptr* in number of chars */
int filesize( FILE *fileptr );

/* Saves *num* chars of *str* in the *fileptr*. */
void to_file( const char *str, FILE *fileptr, int num );

/* Safe version of calloc. Checks if it's successful. */
void *xcalloc( size_t number, size_t size );


int main( int argc, char* argv[] )
{
    FILE *input = NULL;
    FILE *output = NULL;
    int size;
    char *string = NULL;
        char *key = NULL;
    char *encrypted = NULL;

    if( argc != 4 )
    {
        printf("Usage:\ncrypt inputfile outputfile key\n");
        exit(1);
    }

    input = xfopen( argv[1], "r" ); /* "rb" também não funciona */
    output = xfopen( argv[2], "w" );

    size = filesize( input );

    string = (char *) xcalloc( size, sizeof( char ) );
    key = (char *) xcalloc( strlen( argv[3] ), sizeof( char ) );
    encrypted = (char *) xcalloc( size, sizeof( char ) );

    strcpy(key, argv[3] );

    string = get_from_file( input );

    encrypted = encrypt( string, key );
    to_file( encrypted, output, size );

    fclose( input );
    fclose( output );

    return 0;
}

void *xcalloc( size_t number, size_t size )
{
    void *ptr = NULL;

    ptr = calloc( number, size );

    if( ptr == NULL )
        abort();

    return ptr;
}

FILE *xfopen( const char *filename, const char *options )
{
    FILE *file = fopen( filename, options );
    if( file == NULL )
        abort();

    return file;
}

char *encrypt( const char *text, const char *key )
{
    char *str = NULL;
    int scount, kcount;

    str = (char *) xcalloc( strlen( text ), sizeof( char ) );

    for( scount = 0, kcount = 0; scount < strlen( text );
            scount++, kcount++)
    {


        str[scount] = (text[scount] ^ key[kcount]);

        if( kcount == (strlen( key ) -1) )
        {
            kcount = -1;
        }

    }

    str[ strlen( text ) ] = '\0';
    return str;
}

int filesize( FILE *fileptr )
{
    char c;
    int count = 0;

    while( c != EOF )
    {
        c = fgetc( fileptr );
        count++;
    }

    rewind( fileptr );
    return count;
}

char *get_from_file( FILE *fileptr )
{
    int size, i;
    char *str;

    size = filesize( fileptr );
    str = (char *) xcalloc( size, sizeof( char ) );

    for( i = 0; i < size; i++ )
    {
        str[i] = fgetc( fileptr );
    }

    str[size-1] = '\0';

    return str;
}

void to_file( const char *str, FILE *fileptr, int num )
{
    int i;

    for( i = 0; i < (num-1); i++ )
    {
        fputc( str[i], fileptr );
    }
}

Last edited by andre.ramaciotti (2007-11-30 12:49:21)


(lambda ())

Offline

#2 2007-11-28 22:32:18

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,390
Website

Re: Help with encryptation program (in C) [solved]

andre.ramaciotti wrote:

The second problem is encryptation by itself tongue. Let's say I have the file 'in' with 'Hello World' in it. If I use 'asd' as password, and the decrypt it, everything is fine, but if I use 'hello' as password, I can't decrypt it, it stops on H.

Your code for encrypting looks fine at first glance.  And your problem is in the decryption stage so I guess thats the code we need to see...

Offline

#3 2007-11-28 22:43:48

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Re: Help with encryptation program (in C) [solved]

It's a XOR encryption algorythm, so you should only give the same password and it would decrypt.

'e' ^ 'e' = '\0'
'\0' ^ 'e' = 'e'

Example of how using it:
crypt in.txt crypt.txt password

crypt crypt.txt decrypt.txt password

Last edited by andre.ramaciotti (2007-11-28 22:47:18)


(lambda ())

Offline

#4 2007-11-28 23:03:14

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,390
Website

Re: Help with encryptation program (in C) [solved]

:facepalm:  Yes, of course - it is obviously too early in the morning for me to be helping here.  Instead I will now drive to work...

Offline

#5 2007-11-28 23:11:24

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,390
Website

Re: Help with encryptation program (in C) [solved]

Well, the problem is that you are not outputing the whole encrypted string.  Your example shows this plainly

'e' ^ 'e' = '\0'

This is the null pointer - a string terminator.  Try encrypting with any string with "e" as the second letter and you only get the "H" back.  So when you assign "encrypted = encrypt( string, key );" you are missing anything beyond the first null character.

I leave for you to fix...

Offline

#6 2007-11-28 23:17:11

andre.ramaciotti
Member
From: Brazil
Registered: 2007-04-06
Posts: 649

Re: Help with encryptation program (in C) [solved]

Ah, I think I got it. Thanks.

And what about the 'Password thing?'. Any suggestions?


(lambda ())

Offline

#7 2007-11-28 23:26:58

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,390
Website

Re: Help with encryptation program (in C) [solved]

My knowledge of C is only good enough to understand programs, not write them, so I will leave this to someone else.

Offline

#8 2007-11-29 14:14:55

mico
Member
From: Slovenia
Registered: 2004-02-08
Posts: 247

Re: Help with encryptation program (in C) [solved]

andre.ramaciotti wrote:

And what about the 'Password thing?'. Any suggestions?

I don't quite understand what exaclty you mean. Do you want to disable keypress echo? If you don't want characters to be printed you can use struct termios and tcsetattr.

Offline

Board footer

Powered by FluxBB