Solved

Help to create a DLL on BC 5.0++ 95

Posted on 1997-11-17
5
285 Views
Last Modified: 2013-12-03
Could you tell me what i must declare to create a DLL on Borland C++ 5.0 32 bits
for Win 95 ?


I know i must declare something like VOID FAR PASCAL (DWORD ... ) but i
don"t know exactly what.

This for the function : pc3init, pc3saltinit, pc3encode, pc3decode in the
source below.

Please answer by mail

thanks



*********************************************************


#include <stdio.h>
#include <stdlib.h>
unsigned long a,b[128],index;
unsigned int r,cle;

/* PC3 Cipher (c) Alexandre PUKALL 1997 */
/* end on July 7, 1997 */
/* can be freely use */
/* but the name of the author must be included in the final software */
/* optimised for 32 bits computers */
/* if the key is bigger, the algorithm is slower */
/* a 20 characters key : 160 bits is enough for security */
/* can go up to 256 charcters key : 2048 bits key */

/* some explanations in english at the end of the source code */

stream(unsigned int *r,unsigned long *index,unsigned long *a,unsigned long
*b)
{
  b[*index] = ( b[*index] * (*a) ) + 1;
  *r = _rotl( (*r + (( b[*index] >> 16 ) & 0x7fff)), ((*r)%16) );
}

unsigned char pc3encode(unsigned char byte)
{
  unsigned short d;

     for (index=0;index<=(cle-1);index++)
     {
       stream(&r,&index,&a,b);
     }
   d=byte;
   byte=byte^(r&255);
   r=r+d;                  /* PC3 in PFB mode PFB ( Plaintext FeedBack ) */
   b[cle-1]=b[cle-1]+d;    /* each plaintext byte modify the encoding */
   return(byte);
}

unsigned char pc3decode(unsigned char byte)
{
  unsigned short d;

     for (index=0;index<=(cle-1);index++)
     {
       stream(&r,&index,&a,b);
     }
   byte=byte^(r&255);
   d=byte;
   r=r+d;                  /* PC3 in PFB mode ( Plaintext FeedBack ) */
   b[cle-1]=b[cle-1]+d;    /* each plaintext byte modify the encoding */
   return(byte);
}


pc3init(unsigned int lngkey,unsigned char key[258])
{
   unsigned int z,y,x,i;
   unsigned char tab[258],plain;
   div_t reste;
   if (lngkey>256) lngkey=256;
   if (lngkey<1)
   {
     lngkey=1;
     strcpy(key,"a");
   }

   x=lngkey;

   for (i=0;i<x;i++)
   {
     tab[i]=key[i];
   }

   reste=div(lngkey,2);
   cle=reste.quot;
   if (reste.rem!=0) cle=cle+1;   /* length of the key : must always be
even between 5 and 128 */
                          /* each on 16 bits */
   printf("%d\n",cle);
   for (z=0;z<128;z++)    /* clear the matrix of keys */
   {
    b[z]=0;
   }

   y=0;
   for (z=0;z<=(cle-1);z++)    /* create the key */
   {
    if ( (z==(cle-1))&&(reste.rem!=0) )
     {
       b[z]=key[y]*256;
     }
    else
     {
       b[z]=(key[y]*256)+key[y+1];
       y=y+1;
     }
    y=y+1;
   }

   r=0;
   a=0x015a4e35;

     for (index=0;index<=(cle-1);index++)
     {                                     /* init the keys */
       for(z=0;z<=index;z++)
       {
       stream(&r,&index,&a,b);
       }
     }

   for (i=0;i<x;i++)
   {
       plain=pc3encode(tab[i]);
       tab[i]=tab[i]^plain;
   }
  i=i-1;
   for (z=1;z<=((x+1)*10);z++)
   {
       plain=pc3encode(tab[i]);
       tab[i]=tab[i]^plain;
       i=i+1;
       if (i>=x) i=0;
   }

   reste=div(lngkey,2);
   cle=reste.quot;
   if (reste.rem!=0) cle=cle+1;

   for (z=0;z<128;z++)
   {
    b[z]=0;
   }

   y=0;
   for (z=0;z<=(cle-1);z++)
   {
    if ( (z==(cle-1))&&(reste.rem!=0) )
     {
       b[z]=tab[y]*256;
     }
    else
     {
       b[z]=(tab[y]*256)+tab[y+1];
       y=y+1;
     }
    y=y+1;
   }

   for (z=0;z<x;z++)
   {
     key[z]=0;
     tab[z]=0;
   }

   r=0;
   a=0x015a4e35;

     for (index=0;index<=(cle-1);index++)
     {
       for(z=0;z<=index;z++)
       {
       stream(&r,&index,&a,b);
       }
     }
}

pc3saltinit(unsigned int lngkey,unsigned char key[258],unsigned int
lngsalt,unsigned char salt[258])
{
   unsigned int z,y,x,i;
   unsigned char tab[516],plain;
   div_t reste;
   if (lngkey>256) lngkey=256;
   if (lngkey<1)
   {
     lngkey=1;
     strcpy(key,"a");
   }
   if (lngsalt>256) lngsalt=256;
   if (lngsalt<1)
   {
     lngsalt=1;
     strcpy(salt,"a");
   }

   x=lngkey;

   for (i=0;i<x;i++)
   {
     tab[i]=key[i];
   }

   for (i=x;i<(lngkey+lngsalt);i++)
   {
     tab[i]=salt[i-lngkey];
   }

   reste=div(lngkey,2);
   cle=reste.quot;
   if (reste.rem!=0) cle=cle+1;

   for (z=0;z<128;z++)
   {
    b[z]=0;
   }

   y=0;
   for (z=0;z<=(cle-1);z++)
   {
    if ( (z==(cle-1))&&(reste.rem!=0) )
     {
       b[z]=key[y]*256;
     }
    else
     {
       b[z]=(key[y]*256)+key[y+1];
       y=y+1;
     }
    y=y+1;
   }

   r=0;
   a=0x015a4e35;

     for (index=0;index<=(cle-1);index++)
     {
       for(z=0;z<=index;z++)
       {
       stream(&r,&index,&a,b);
       }
     }

   for (i=0;i<(lngkey+lngsalt);i++)
   {
       plain=pc3encode(tab[i]);
       tab[i]=tab[i]^plain;
   }
  i=i-1;
   for (z=1;z<=((lngkey+lngsalt+1)*10);z++)
   {
       plain=pc3encode(tab[i]);
       tab[i]=tab[i]^plain;
       i=i+1;
       if (i>=(lngkey+lngsalt)) i=0;
   }

   reste=div(lngkey,2);
   cle=reste.quot;
   if (reste.rem!=0) cle=cle+1;

   for (z=0;z<128;z++)
   {
    b[z]=0;
   }

   y=0;
   for (z=0;z<=(cle-1);z++)
   {
    if ( (z==(cle-1))&&(reste.rem!=0) )
     {
       b[z]=tab[y]*256;
     }
    else
     {
       b[z]=(tab[y]*256)+tab[y+1];
       y=y+1;
     }
    y=y+1;
   }

   for (z=0;z<x;z++)
   {
     key[z]=0;
   }
   for(z=0;z<(lngkey+lngsalt);z++)
   {
     tab[z]=0;
   }

   r=0;
   a=0x015a4e35;

     for (index=0;index<=(cle-1);index++)
     {
       for(z=0;z<=index;z++)
       {
       stream(&r,&index,&a,b);
       }
     }
}

***************************************************************
   NOT INCLUDED IN THE DLL
***************************************************************

main()
{
  unsigned int longueur,y,lngsalt;
  unsigned char code[21]="abcdefghijklmnopqrst";   /* user key can be
code[0]=0x25; code[1]=0xf7; ... */
  unsigned char salt[3]; /* length of the salt + 1 */
  FILE *in,*out;
  short c;
  longueur=20; /* length of the key up to 256 characters */
  lngsalt=2;
  salt[0]=0x47;
  salt[1]=0xf5;

  pc3saltinit(longueur,code,lngsalt,salt);

  /* the 2 is the length of the salt, the 0x47f5 is the salt on 16 bits =
2* 8 bits */
  /* the salt is a random value, like the time of the computer you give in
addition of the key */
  /* to change the result key, the salt must be write in a non encoded form
in the output file */
  /* to allow to decode the file with the user key */
  /* the salt value can go up to 256 characters */
  /* the PSCHF function in included when you run pc3salinit() */
  /* it give a random 8 bits keys even a 7 bits is given */
  /* for example you use pc3salinit() with the user key */
  /* and for the salt you give the time of the computer's clock + a counter
*/
  /* 1 for first message, 2 for second ... */
  /* you send the time and the counter in plaintext ( not encrypted ) */
  /* and after the message encrypted */
  /* the two users must have a secret share key */
   /* pc3init(longueur,code) has a PSCHF function too but not a salt
function */


  if ((in=fopen("essai.txt","rb"))==NULL) {printf("Error file ESSAI.TXT not
found !\n");exit(-1);}
  if ((out=fopen("sortie.Txt","wb"))==NULL) {printf("Error writing file
SORTIE.TXT !\n");exit(-1);}

  while ( (c=fgetc(in)) !=EOF )
  {
   fputc(pc3encode(c),out);
  }

  fclose(in);
  fclose(out);

  /* to decode use : */
  /* in=fopen("sortie.Txt","rb"); */
  /* out=fopen("essai.Txt","rb"); */
  /* while .... */
  /* fputc(pc3decode(c),out); */

}

*************************************************************
0
Comment
Question by:klimt
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:yl
ID: 1408442
You shoud include <windows.h> in your source and declare the functions APIENTRY _export.
0
 

Author Comment

by:klimt
ID: 1408443
i would like to know what is must declare in the function , example :

pc3code(unsigned char byte) give me
pc3code(DWORD ... ) or something like this ?

0
 
LVL 1

Accepted Solution

by:
yl earned 200 total points
ID: 1408444
The following function prototype was successfuly compiled using BC 5:

extern "C" unsigned char APIENTRY _export pc3code(unsigned char byte)
{
}

What do you mean by: pc3code(unsigned char byte) give me pc3code(DWORD ... )?
0
 

Author Comment

by:klimt
ID: 1408445
What do you mean by: pc3code(unsigned char byte) give me pc3code(DWORD ... )?

i have seen that some declaration uses
LPRST, DWORD, FAR PASCAL as definition ?

What is used for ?


0
 
LVL 1

Expert Comment

by:yl
ID: 1408446
1. FAR PASCAL was used to declare exported functions in Win16. This declaration is no longer needed in Win32.
2. LPSTR and DWORD are typedefs defined in the windows include files, where LPSTR stands for FAR char* and DWORD for unsigned long. You can look them up in the include directory. They are used to declare parameter types for Windows API functions (you don't have to use them for your own functions).
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now