PDA

View Full Version : link problem using MS Visual c++6.0 when useing Cryptlib


chengyou
March 31st, 2004, 03:28
Hi,

I am a newbie. I try to use Cryptlib low level interface to run a demo using DES algorithm.

When I use MS Visual c++ 6.0 under Windows 2000 Pro to build this test.c file. This error occur:
cryptlib.h : fatal error LNK1136: invalid or corrupt file

Does any expert give me hint, thank you very very much in advance.


This is the test.c source code

#define INC_CHILD

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

#include "crypt\des.h"
#include "cryptlib.h"


int main()

{
#define CRYPTLEN 128
#define BUFFERLENGTH (CRYPTLEN+64)
char CryptBuffer[BUFFERLENGTH];

int status;
int mode;
int ok1;
int i=0;

CRYPT_CONTEXT cryptContext;

// input crypt context buffer
printf("\nBitte eine Zeichenfolge eingeben:\nbis zu 32 Zeichen (256 Bit)\n\n>";

ok1=1;
while (((mode=getch()) != 13) && ok1==1)
{
putch(mode);
if (i < CRYPTLEN-1){
CryptBuffer[i++] = mode;
}
else{
printf("\nBuffer filled!";
ok1 = 0;
}
}

//last element to 0 for String with ending 0
CryptBuffer[i++] = 0;

printf("\n%s\n",CryptBuffer);
printf("\nEs werden %i Bytes verschluesselt\n", strlen(CryptBuffer));

/* Cryptlib Initialisierung */
status = cryptInit();
if( cryptStatusError( status ) )
{
printf( "cryptInit() failed with error code %d.\n", status );
return( -1 );
}

/* Load the key, encrypt the buffer, and destroy the encryption context */

cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_DES );

cryptGenerateKey( cryptContext );

printf( "\ntext: %s; textlen: %d", CryptBuffer, strlen(CryptBuffer));

/* Write the encrypted data to the screen */

printf( "\nContents of buffer after encryption: \n\n%.32s\n", CryptBuffer );


/* Load the key, decrypt the buffer, and destroy the encryption context */


cryptDecrypt( cryptContext, CryptBuffer, BUFFERLENGTH );


/* Write the decrypted data to the screen */

printf( "\nContents of buffer after decryption: \n\n%.32s\n\n", CryptBuffer );
printf( "\ntext: %s; textlen: %d", CryptBuffer, strlen(CryptBuffer));

cryptDestroyContext( cryptContext );

cryptEnd(); /* Cryptlib schliessen */

return 0;

}

naides
March 31st, 2004, 04:47
You may have better luck for inteligent answers in a different (Visual C) Board, but, at quick view:

The code looks consistent, and compiles OK. This is a Linker error Code.

Often, the Library cryptlib.lib included by cryptlib.h is in Borland format, and does not link in a MS environ. You may need to recreate the library and or.check if there is a MS compatible version

chengyou
March 31st, 2004, 11:46
Great hints! Naides, Thanks a lot!
Now I can turn the right way.

I have try several different c++ compiler, all the problem are due to the cryptlib.h file.



mike
April 4th, 2004, 21:10
Also, the best place for cryptlib advice is the cryptlib mailing list. It's very responsive.

chengyou
April 6th, 2004, 04:01
Hi, Sir

I use MS Visual C++ 6.0 under Windows 2000 to test one algorithm.

This program seem encrypt correctly, because the error alert not occur,
But the decrypt seemed not correct.

Does anybody give some hints? thanks a slot.





this is the running result
--------------------------------------------------------------------
show the buffter:
hello
initialisation of cryptlib sucess.
successfully load cryptCreateContext. using algoriothm DES
Generating keys...

text: ↓≥≡ P√T╕4╨Γ b╙s φπ╖π⌡≥d{ G;Ls╪/╪╤▒εI"#╖τΘ2↕╟→₧↑>_9►╪atP└4M%
♣s╥╟∩{}y ┼≡e&fB/ →=▐"l╝←jaR9Φ╟bA☼♀$♥↨∩Z<; textlen: 125

Contents of buffer after encryption:

↓≥≡ P√T╕4╨Γ b╙s φπ╖π⌡≥d{ G;Ls╪

Contents of buffer after decryption:

τGI∞]D

current cryptBuffer length: 10
the first cryptlib running finish.
Press any key to continue




This is the test source code
//****************************************************************************


#ifdef _MSC_VER
#include "../cryptlib.h"
#include "../test/test.h"
#else
#include "cryptlib.h"
#include "test/test.h"
#endif

//****************************************************************************

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

CRYPT_CONTEXT cryptContext;

#define CRYPTLEN 128

int InitStatus;
int ContextStatus;
int EncryptStatus;

// assign stored data

unsigned char TransData[] = "hello";

int len = strlen(TransData);
char *CryptBuffer = (char *)malloc(sizeof(char)*(len+1));

memcpy(CryptBuffer,TransData, (len+1) );

printf(" show the buffter: \n%s\n",CryptBuffer);



/* Initialise cryptlib */

InitStatus = cryptInit();
if( cryptStatusError( InitStatus ) )
{
printf( "cryptInit() failed with error code %d.\n", InitStatus);
exit( EXIT_FAILURE );
}
else
printf( "initialisation of cryptlib sucess. \n";

cryptAddRandom( NULL, CRYPT_RANDOM_SLOWPOLL );


/* Load the key, encrypt the buffer */

ContextStatus = cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_DES );

if( cryptStatusError( ContextStatus ) )
{
printf( "cryptCreateContext() failed \n";
exit( EXIT_FAILURE );
}
else
printf( "successfully load cryptCreateContext. using algoriothm DES \n";


/* Create key*/

cryptGenerateKey( cryptContext );
printf ("Generating keys...\n";


/* Write the encrypted data to the screen */


EncryptStatus = cryptEncrypt( cryptContext, CryptBuffer, CRYPTLEN);

if( cryptStatusError( EncryptStatus ) )

{
printf( "Couldn't encrypt data, status = %d.\n", EncryptStatus );
}

else
printf( "\ntext: %s; textlen: %d\n", CryptBuffer, strlen(CryptBuffer));

printf( "\nContents of buffer after encryption: \n\n%.32s\n", CryptBuffer );

/****************************************************************************
/* Load the key, decrypt the buffer, and destroy the encryption context */

cryptDecrypt( cryptContext, CryptBuffer, CRYPTLEN );

/* Write the decrypted data to the screen */

printf( "\nContents of buffer after decryption: \n\n%.32s\n\n", CryptBuffer );
printf( "current cryptBuffer length: %d \n", strlen(CryptBuffer));

cryptDestroyContext( cryptContext );

printf("the first cryptlib running finish. \n";

free (CryptBuffer);

cryptEnd();

}

mike
April 7th, 2004, 06:55
Well, if you're going to put crypto in your own code, don't bother with the low level encrytion calls. Use the enveloping API.

One bug in your program is you're telling cryptlib that the buffer size is CRYPTLEN when you only allocate 5 or 6 bytes.