Mailing List Archive

Encrypted line changes on each run .
-----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-----


_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel
Re: Encrypted line changes on each run . [ In reply to ]
On Thu, 31 Jul 2008 22:51, mashrom.head@gmail.com said:
> I wrote a small application to understand ARCFOUR use .

Hust a few comments on the code:

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

Please don't cast thye result of a malloc; Fro ages malloc returned a
void pinter which has been introduced to get rid of such casts. Note
that C and C++ are different languages.

> //Just null all
>
> for ( i = 0 ;i < size_of_buffers; i++ )
> {
> out[i] = '0';
> deout[i] = '0';
> }

memset (out, 0, size_of_buffers);
memset (deout, 0, size_of_buffers);

is easier to understand.

> 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 )

That is a severe bug - you wont catch an error from handle2. Fix is:

err = gcry_cipher_open ( &handle2, GCRY_CIPHER_ARCFOUR,
GCRY_CIPHER_MODE_STREAM, 0 );
if (!err)
err = gcry_cipher_open ( &handle, GCRY_CIPHER_ARCFOUR,
GCRY_CIPHER_MODE_STREAM, 0 );
if (err)
{
....

> err = gcry_cipher_setkey ( handle , key ,256 );
> err = gcry_cipher_setkey ( handle2 , key,256 );

Same here.

> write2 ( "/tmp/encrypt",out,size_of_buffers );
> for ( i = 0 ;i < size_of_buffers; i++ )
> out[i] = '0';

Use memset for clarity.

> 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 );

I guess you wanted to use gcry_cipher_decrypt.

And again: Do not use Arcfour if you do not understand all the subtle
gotchas to use Arcfour in a secure way.


Shalom-Salam,

Werner


--
Die Gedanken sind frei. Auschnahme regelt ein Bundeschgesetz.


_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel