Encrypted line changes on each run .

Jabka Atu mashrom.head at gmail.com
Thu Jul 31 22:51:40 CEST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I wrote a small application to understand ARCFOUR use .


for some reason the encrypted text changes each time i run it :

So if i store it in a file and read it from file after 30 minuts i
can't decrypt it .


but if i run this as it is it works fine


#include <stdio.h>
#include <gcrypt.h>
#include <assert.h>

int read2 ( char * filename , char * buffer , size_t size )
{
    FILE * fp = NULL;
    fp = fopen ( filename , "rb" );
    if ( fp == NULL ) return 1;

    fread ( buffer, size, 1, fp );
    fclose ( fp );
}

int write2 ( char * filename , char * buffer , size_t size )
{
    FILE * fp = NULL;
    fp = fopen ( filename , "wb" );
    if ( fp == NULL ) return 1;
    fwrite ( buffer, size, 1, fp );
    fclose ( fp );
}


int main()
{

    gcry_cipher_hd_t handle;
    gcry_cipher_hd_t handle2;
    gcry_error_t err = 0;
    char * plain_text ;
    char * out ;
    char * deout ;
    int i ;
    char  key[] = "This is really strange , i read the documentation i
googled for two days and nothing , i can't encrypt && decrypt";
    char text[] = "Text to encrypt bla bla bla bla bla bla bla soo
This is really strange , i read the documentation i googled for two
days and nothing , i can't encrypt && decrypt";

    size_t size_of_plain_text    =  strlen ( text ) + 1;
    size_t size_of_buffers        =  size_of_plain_text ;

    plain_text = ( char * ) malloc ( size_of_plain_text );
    out = ( char * ) malloc ( size_of_buffers );
    deout = ( char * ) malloc ( size_of_buffers );


    assert ( plain_text );

    assert ( out );
    assert ( deout );


    //Just null all

    for ( i = 0 ;i < size_of_buffers; i++ )
    {
        out[i] = '0';
        deout[i] = '0';
    }

    strcpy ( plain_text , text );


    gcry_check_version ( NULL );
    gcry_control ( GCRYCTL_DISABLE_SECMEM_WARN );
    gcry_control ( GCRYCTL_INIT_SECMEM, 16384, 0 );


    {

        err = gcry_cipher_open ( &handle2,
GCRY_CIPHER_ARCFOUR,GCRY_CIPHER_MODE_STREAM,0 );
        err = gcry_cipher_open ( &handle,
GCRY_CIPHER_ARCFOUR,GCRY_CIPHER_MODE_STREAM,0 );

        if ( err )
        {
            fprintf ( stderr, "Failure: %s/%s\n",
                      gcry_strsource ( err ),
                      gcry_strerror ( err ) );

        }
        err = gcry_cipher_setkey ( handle  , key ,256 );
        err = gcry_cipher_setkey ( handle2 , key,256 );
        if ( err )
        {
            fprintf ( stderr, "Failure: %s/%s\n",
                      gcry_strsource ( err ),
                      gcry_strerror ( err ) );

        }
    }
    err =  gcry_cipher_encrypt ( handle,
                                 ( unsigned char * ) out,
size_of_buffers , ( const unsigned char * )
plain_text,size_of_plain_text );
    if ( err )
    {
        fprintf ( stderr, "Failure: %s/%s\n",
                  gcry_strsource ( err ),
                  gcry_strerror ( err ) );

    }

    write2 ( "/tmp/encrypt",out,size_of_buffers );
    for ( i = 0 ;i < size_of_buffers; i++ )
        out[i] = '0';
    read2 ( "/tmp/encrypt",out,size_of_buffers );
    err =  gcry_cipher_encrypt ( handle2,
                                 ( unsigned char * ) deout,
size_of_buffers, ( const unsigned char * ) out,size_of_buffers );
    if ( err )
    {
        fprintf ( stderr, "Failure: %s/%s\n",
                  gcry_strsource ( err ),
                  gcry_strerror ( err ) );

    }
    printf ( "The result is : %d\n%s\n%s\n", strcmp ( deout,text
),deout , text );

    free ( plain_text );
    free ( out );
    free ( deout );
    gcry_cipher_close ( handle );
    gcry_cipher_close ( handle2 );
    return 0;

}

- --
- ---==== Jabka Atu ===---
    bsh83.blogspot.com
- ---=== Encryption is a way of life ===---
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJIkiXbAAoJEJzNKvbZ7QAsd1kP/jX6J98MJk+7A2NaullApszr
jHrc0dQ8C3pd4JK3iXlDHWd1+wkuhInlQeBwXDNHex1KBxdHwqp1YFA1Yv4wFVSB
b66Yec1PUG/++QR8WaWxvvuViS2Je2L9BZIE85QOt1KwuMUjz4lyM8ZyF+UfTl6+
R+tcTY5hAOCvS76SIbgOQAdMSUfO3QzgRGaOSTxI+EYtasuKw1bnaFHC0wM/NcJG
sV/PocRS2b3dL3PPbs3//I42+oUfmCC6yr9H1tRtMRzPED90Fs4L4SyLKQqmo7G8
OdjaL+sx5qApiy1LBSiM1DC0PJNqLToETnTHLRFvN2Fl/CcNcsTk7gxOhXFVPm6H
MpTXIoBzFrz4enTyzIG/cqbg0t/nYwt/4BLCqn17Pk/MeWlJC29/SpOggkUPdtzB
czh7PofngJXEJ/aEPAaUc1CnRHhU5emIKhIaM6Ri4KoKBLvY0tO57x1KuK86aiH4
3iEkWyooVAU32e/+Xh8UtQYtWLWSstfv8kcIYUhae3N8J4hn88BVNTCxJCzY7ytf
Pfwab4tMkN485//JS6uZ8mQkkhhcezt9YfQC/+DT3C65ubHTa2F2JjwReDb5y9Mx
E+zMLa6/GsNnnhsg2T3d4cFS7WcHN7eZdXlVPhHqaSNbUC1oG7LJpPiTOR/DIIzs
mIfVc/YUoqLs2CyIHb0Z
=1o9i
-----END PGP SIGNATURE-----




More information about the Gcrypt-devel mailing list