Code:
#include <stdio.h>
#include <gcrypt.h>
#include <assert.h>
int main()
{
gcry_cipher_hd_t handle;
gcry_cipher_hd_t handle2;
gcry_error_t err = 0;
char * plain_text ;
char * out ;
char * deout ;
plain_text = ( char * ) malloc ( sizeof ( char ) * ( 6 ) );
out = ( char * ) malloc ( sizeof ( char ) * ( 60 + 1 ) );
deout = ( char * ) malloc ( sizeof ( char ) * ( 60 + 1 ) );
assert ( plain_text );
assert ( out );
assert ( deout );
strcpy ( plain_text , "Secret" );
printf ( "%d %d \n", ( int ) sizeof ( plain_text ), strlen ( plain_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 ) );
fprintf ( stdout, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
}
err = gcry_cipher_setkey ( handle, "This is really strange , i read the documentation i googled for two days and nothing , i can't encrypt && decrypt",256 );
err = gcry_cipher_setkey ( handle2, "This is really strange , i read the documentation i googled for two days and nothing , i can't encrypt && decrypt",256 );
if ( err )
{
fprintf ( stderr, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
fprintf ( stdout, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
}
}
err = gcry_cipher_encrypt ( handle,
( unsigned char * ) out, sizeof ( out ), ( const unsigned char * ) plain_text,sizeof plain_text );
if ( err )
{
fprintf ( stderr, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
fprintf ( stdout, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
}
printf ( " out :%s ||| plain text %s\n",out, plain_text );
err = gcry_cipher_encrypt ( handle2,
( unsigned char * ) deout, sizeof deout, ( const unsigned char * ) out,sizeof out );
if ( err )
{
fprintf ( stderr, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
fprintf ( stdout, "Failure: %s/%s\n",
gcry_strsource ( err ),
gcry_strerror ( err ) );
}
printf ( "deout: %s ||| out: %s ||| plain text: %s\n",deout, out,plain_text );
free ( plain_text );
free ( out );
free ( deout );
gcry_cipher_close ( handle );
gcry_cipher_close ( handle2 );
return 0;
}
Special thanks to Adam Langle the man who gave me the ans (The reason is that RC4 mutates itself as it processe)