home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *+
- ** Function Name: Data EnCryPTion Functions - CPT
- **
- ** Description: Functions to encrypt data.
- ** Method is 255 byte swap with optional convolutions
- ** to increase strength of encryption.
- **
- ** Can be used on any file (text, binary, etc.)
- **
- ** NOTE: Although this method will keep most people and
- ** most hackers from decoding your data,
- ** it is NOT intended for use on government or
- ** DoD data which requires specific security
- ** performance.
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
-
- /****************************************************
-
- Approach
-
- CPT uses the full 255 byte substition method which yields an extremely high
- number of possible ways to encrypt a file.
-
- The following code fragment demonstrates the number of combinations based on
- N (=256):
-
- Comb = 1;
- FOR I = 1 TO N-1
- Comb = Comb + I*Comb;
-
-
- For added security, a convolution metric is available which will multiply
- the above number by the number of convolutions you supply.
-
- Here is a sample run of CPT functions.
-
- 1. Create key set.
- Take the numbers 0 thru 255 and sort them into random order.
- 2. Write key set to disk.
- The key set values are written out as ascii text.
- 3. Encode a file.
- If input is ABC, then key positions [65], [66], and [67] are
- checked to see what random values they contain. Output
- for ABC could be *D@. (Unprintable characters are also
- used.)
- 4. Decode a file.
- The key set values are read from disk and reversed.
- (e.g. Key position '*' gets value 'A'.)
- Input *D@ is translated back to ABC.
-
- If a convolution value of 5 is used then an input before
- encoding of AAAAA could result in output of 4Pd-3. Use of convolutions
- can dramatically slow down any unauthorized attempt to
- decode your data.
-
-
-
- Application Usage
-
- If you want to incorporate CPT functions into your application, there
- are four routines you need to call.
-
- CptGenKeys - To Create a new key set
- CptSetJob - To set mode (CPT_ENCODE or CPT_DECODE)
- CptReadInKeys - To read a key set into memory
- CptCodeFile - To encode or decode a file
-
- Once a key set has been created using CptGenKeys, the same key set can
- be used on multiple files.
-
- Note: Since there are only four Cpt functions, no man pages
- were created for CPT.
-
- WARNING: To decrypt a file requires the EXACT same key set as was
- used to encrypt.
- If you have lost the key set then you are stuck.
- I have no way to reconstruct your key set or decode
- decode any file you encrypt.
-
- ****************************************************/
-
- #include <stdio.h>
- #ifdef C_ANSI
- #include <stdlib.h>
- #include <string.h>
- #endif
-
- #include <memincs.h>
-
- #include <cpt.h>
-
-
-
-
- /*
- ** A couple of CHAR array utility functions
- */
-
-
- /*
- ** Left pad an array with blanks
- */
-
- #ifdef C_ANSI
- VOID APIENTRY lpad(CHAR * str,INT width)
- #else
- VOID APIENTRY lpad(str,width)
- CHAR *str;
- INT width;
- #endif
- {
- INT i,w;
-
- w = strlen(str);
-
- /****************************************
- if needs padding
- ****************************************/
- if(width > w)
- {
- /* move bytes */
- /*
- 123
- 12 3
- 1 23
- 123
- */
- for(i = 0; i < w; i++)
- str[width-i-1] = str[w-i-1];
-
- /* pad with spaces */
- for(i = 0; i < (width - w); i++)
- str[i] = ' ';
-
- /* mark end */
- str[width] = '\0';
- }
-
- /****************************************
- if needs truncating
- ****************************************/
- if(width < w)
- str[width] = '\0';
-
-
- }
-
- /*
- ** A string replacement function
- ** Replace all characters dh in string ch with character eh
- */
-
- #ifdef C_ANSI
- VOID APIENTRY srep(CHAR * ch,CHAR dh,CHAR eh)
- #else
- VOID APIENTRY srep(ch,dh,eh)
- CHAR *ch;
- CHAR dh;
- CHAR eh;
- #endif
- {
- INT i;
-
- for(i = 0; i < strlen(ch); i++)
- if(ch[i] == dh)
- ch[i] = eh;
- }
-
-
-
- /******************************************************************************
- *+
- ** Function Name: CptSetJob
- **
- ** Description: Get the Cpt Job to CPT_CODE or CPT_DECODE
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptSetJob(CPT_WORK_AREA_P pCptWA,INT iJob)
- #else
- SHORT APIENTRY CptSetJob(pCptWA,iJob)
- CPT_WORK_AREA_P pCptWA;
- INT iJob;
- #endif
- {
- C_DEF_MODULE("CptSetJob")
-
- pCptWA -> iJob = iJob;
-
- C_RETURN
- }
-
-
- /******************************************************************************
- *+
- ** Function Name: CptKeyGen
- **
- ** Description: Generate a Key Set in memory.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptKeyGen(CPT_WORK_AREA_P pCptWA,INT iSet)
- #else
- SHORT APIENTRY CptKeyGen(pCptWA,iSet)
- CPT_WORK_AREA_P pCptWA;
- INT iSet;
- #endif
- {
- C_DEF_MODULE("CptKeyGen")
-
- unsigned top;
- INT f;
- INT i;
- UCHAR hold_area[256];
-
- #ifdef ANNOUNCE
- if(iSet == 0)
- printf("\nGenerating Keys - Please Stand By");
- #endif
-
- for(i = 0; i < ASCIIBYTES+1; i++)
- {
- hold_area[i] = (UCHAR) i;
- pCptWA -> cCptCodes[CPT_ENCODE][iSet][i] = -1;
- }
-
- srand(iSet);
-
- /*
- ** Use 'quick' randomizer
- ** It copies the top element that has not been selected when
- ** a duplicate random number is found.
- */
-
- for(top = 256; top > 0; top--)
- {
- f = (rand() % top); /* was 256 */
- pCptWA -> cCptCodes[CPT_ENCODE][iSet][top-1] = hold_area[f];
- if(f != top-1)
- hold_area[f] = hold_area[top-1];
- }
-
- C_RETURN
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptGetKeyData
- **
- ** Description: Get Parameters for key set from user.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptGetKeyData(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptGetKeyData(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptGetKeyData")
-
- INT i;
-
- printf("\n\n\nEnter Reference Number For This Key ");
- scanf("%d",&i);
- sprintf(pCptWA -> szKeyStr,"%d",i);
- lpad(pCptWA -> szKeyStr,4);
- srep(pCptWA -> szKeyStr,' ','0');
- strcpy(pCptWA -> szKeyFile,"key_");
- strcat(pCptWA -> szKeyFile,pCptWA -> szKeyStr);
- strcat(pCptWA -> szKeyFile,".cpt");
-
- printf("\nEnter Convolutions (Usally 1, higher = tighter security) ");
- scanf("%d",&pCptWA -> iCptConvolutions);
-
- C_RETURN
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptGenKeys
- **
- ** Description: Create keys and write key file.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptGenKeys(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptGenKeys(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptGenKeys")
-
- INT i,iC;
-
- if(pCptWA -> iCptConvolutions > MAX_CONVOLUTIONS)
- pCptWA -> iCptConvolutions = MAX_CONVOLUTIONS;
-
- for(i = 0; i < pCptWA -> iCptConvolutions; i++)
- CptKeyGen(pCptWA,i);
-
- pCptWA -> KeyFile = fopen(pCptWA -> szKeyFile,"w+");
- if(pCptWA -> KeyFile == NULL)
- C_LEAVE(C_NOTOK)
-
- fprintf(pCptWA -> KeyFile,"%s %s %s %d\n",
- CPT_ID, CPT_VER, pCptWA -> szKeyStr, pCptWA -> iCptConvolutions);
-
- for(iC = 0; iC < pCptWA -> iCptConvolutions; iC++)
- {
- for(i = 0; i < ASCIIBYTES+1; i++)
- fprintf(pCptWA -> KeyFile,"%d\n",pCptWA -> cCptCodes[CPT_ENCODE][iC][i]);
- }
-
- fclose(pCptWA -> KeyFile);
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptReadInKeys
- **
- ** Description: Read in key data from file.
- **
- ** Note: pCptWA -> iJob must be set to CPT_CODE or CPT_DECODE
- ** before calling this function.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptReadInKeys(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptReadInKeys(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptReadInKeys")
-
- CHAR data[5];
- INT i,i2,iC;
-
- pCptWA -> KeyFile = fopen(pCptWA -> szKeyFile,"r");
- if(pCptWA -> KeyFile == NULL)
- C_LEAVE(C_NOTOK)
-
- fscanf(pCptWA -> KeyFile,"%s %s %s %d\n",
- pCptWA -> szCptId,
- pCptWA -> szCptVers,
- pCptWA -> szCptKeyId,
- &pCptWA -> iCptConvolutions);
-
- for(iC = 0; iC < pCptWA -> iCptConvolutions; iC++)
- {
- for(i = 0; i < ASCIIBYTES+1; i++)
- {
- fgets(data,5,pCptWA -> KeyFile);
- i2 = atoi(data);
- #ifdef DEBUG
- printf("Read %d (%c) at %d (%c)\n", i2,i2,i,i);
- #endif
- pCptWA -> cCptCodes[CPT_ENCODE][iC][i] = (unsigned char) i2;
- }
- }
-
- if(pCptWA -> iJob == CPT_DECODE)
- CptReverseKeys(pCptWA);
-
- C_MODULE_EXIT:
-
- if(pCptWA -> KeyFile != NULL)
- fclose(pCptWA -> KeyFile);
-
- C_RETURN
-
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptReverseKeys
- **
- ** Description: Reverse key codes in memory for decoding operation.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptReverseKeys(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptReverseKeys(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptReverseKeys")
-
- INT i,scan,iC;
-
- for(iC = 0; iC < pCptWA -> iCptConvolutions; iC++)
- for(i = 0; i < ASCIIBYTES+1; i++)
- for(scan = 0; scan < ASCIIBYTES+1; scan++)
- if(pCptWA -> cCptCodes[CPT_ENCODE][iC][scan] == (UCHAR) i)
- {
- pCptWA -> cCptCodes[CPT_DECODE][iC][i] = (UCHAR) scan;
- #ifdef DEBUG
- printf("%d (%c) used at %d (%c)\n",scan,scan,i,i);
- #endif
- }
-
- C_RETURN
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptCodeFile
- **
- ** Description: Apply keys in conversion from input file to output file.
- ** Is called for both encoding and decoding.
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptCodeFile(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptCodeFile(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptCodeFile")
-
- INT iBytesRead;
- static UCHAR szBuffer[BUFSIZE + 1]; /* keep off the stack */
-
- pCptWA -> OutFile = NULL;
-
- pCptWA -> InFile = fopen(pCptWA -> szInFile,"rb");
- if(pCptWA -> InFile == NULL)
- C_LEAVE(C_NOTOK)
-
- pCptWA -> OutFile = fopen(pCptWA -> szOutFile,"wb");
- if(pCptWA -> OutFile == NULL)
- C_LEAVE(C_NOTOK)
-
- /*
- ** Reset convolution ptr
- */
-
- pCptWA -> iC = 0;
-
- do
- {
- iBytesRead = fread(szBuffer,1,BUFSIZE,pCptWA -> InFile);
- if(iBytesRead > 0)
- {
- CptCodeBuffer(pCptWA,szBuffer,szBuffer,iBytesRead);
- fwrite(szBuffer,1,iBytesRead,pCptWA -> OutFile);
- }
-
- } while(iBytesRead != 0);
-
- C_MODULE_EXIT:
-
- if(pCptWA -> InFile != NULL)
- fclose(pCptWA -> InFile);
- if(pCptWA -> OutFile != NULL)
- fclose(pCptWA -> OutFile);
-
- C_RETURN
- }
-
- /******************************************************************************
- *+
- ** Function Name: CptCodeBuffer
- **
- ** Description: Apply key translation/coding on buffer
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptCodeBuffer(CPT_WORK_AREA_P pCptWA,UCHAR * pcBufIn,UCHAR * pcBufOut,INT iBufLen)
- #else
- SHORT APIENTRY CptCodeBuffer(pCptWA,pcBufIn,pcBufOut,iBufLen)
- CPT_WORK_AREA_P pCptWA;
- UCHAR * pcBufIn;
- UCHAR * pcBufOut;
- INT iBufLen;
- #endif
- {
- C_DEF_MODULE("CptCodeBuffer")
-
- INT iPos;
-
- for(iPos = 0; iPos < iBufLen; iPos++)
- {
- #ifdef DEBUG
- printf("using %d %c for %d (%c)\n", pCptWA -> cCptCodes[pCptWA->iJob][pCptWA->iC][pcBufIn[iPos]],pCptWA -> cCptCodes[pCptWA->iJob][pCptWA->iC][pcBufIn[iPos]],pcBufIn[iPos],pcBufIn[iPos]);
- #endif
- pcBufOut[iPos] = pCptWA -> cCptCodes[pCptWA->iJob][pCptWA->iC][pcBufIn[iPos]];
- if(pCptWA->iCptConvolutions > 1)
- CptAdjustConvolution(pCptWA);
- }
-
- C_RETURN
- }
-
-
- /******************************************************************************
- *+
- ** Function Name: CptAdjustConvolution
- **
- ** Description: Adjust convolution ptr as part of buffer coding process
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptAdjustConvolution(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptAdjustConvolution(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptAdjustConvolution")
-
- pCptWA -> iC++;
- if(pCptWA -> iC >= pCptWA -> iCptConvolutions)
- pCptWA -> iC = 0;
-
- C_RETURN
- }
-
-
- /******************************************************************************
- *+
- ** Function Name: CptUsrCode
- **
- ** Description: Get Code or decode info from user
- **
- ** Return Codes:
- ** C_OK
- **
- ** Written by: John Tal
- **
- **
- ** Modification History:
- **
- ** Date Engineer Mod# Modification
- ** ---------------------------------------------------------------------------
- ** 12-FEB-1992 J. Tal V1.0-000 New
- **
- *-
- */
-
- #ifdef C_ANSI
- SHORT APIENTRY CptUsrCode(CPT_WORK_AREA_P pCptWA)
- #else
- SHORT APIENTRY CptUsrCode(pCptWA)
- CPT_WORK_AREA_P pCptWA;
- #endif
- {
- C_DEF_MODULE("CptUsrCode")
-
- INT keynum;
- CHAR key_str[16];
- CHAR job_code[4];
-
- if(pCptWA -> iJob == CPT_ENCODE)
- strcpy(job_code,"");
- else
- strcpy(job_code,"DE-");
-
- printf("\n\n\nYOU must keep trak of which key you used on which file.\n");
- printf("\n\nKey Number To Use ");
- scanf("%d",&keynum);
- fflush(stdin);
-
- sprintf(key_str,"%d",keynum);
- lpad(key_str,4);
- srep(key_str,' ','0');
- strcpy(pCptWA -> szKeyFile,"key_");
- strcat(pCptWA -> szKeyFile,key_str);
- strcat(pCptWA -> szKeyFile,".cpt");
-
- printf("\nFile To %sCode ",job_code);
- gets(pCptWA -> szInFile);
-
- printf("\n\nName For %sCoded Result File ",job_code);
- gets(pCptWA -> szOutFile);
-
- C_STATUS = CptReadInKeys(pCptWA);
-
- if(C_STATUS)
- C_LEAVE(C_STATUS)
-
- C_STATUS = CptCodeFile(pCptWA);
-
- C_MODULE_EXIT:
-
- C_RETURN
-
- }
-
-
-
- #ifdef TEST
- /******************************************************************************
- *+
- ** Cpt Test Function
- **
- *-
- */
-
-
- main()
- {
- INT pick;
- CHAR fDone = C_FALSE;
- CPT_WORK_AREA_T stCptWA;
- CPT_WORK_AREA_P pCptWA;
-
-
- memset(&stCptWA,0,sizeof(CPT_WORK_AREA_T));
- pCptWA = &stCptWA;
-
- do
- {
- pick = 0;
- printf("\n\n\nWhich Of These Do You Want To Do\n");
- printf(" 1 Generate Keys\n");
- printf(" 2 Code File\n");
- printf(" 3 De-Code File\n\n");
- printf(" 0 None Of These");
-
- scanf("%d",&pick);
-
- switch(pick){
- case 0 : fDone = C_TRUE;
- break;
- case 1 : CptGetKeyData(pCptWA);
- CptGenKeys(pCptWA);
- break;
- case 2 : CptSetJob(pCptWA,CPT_ENCODE);
- CptUsrCode(pCptWA);
- break;
- case 3 : CptSetJob(pCptWA,CPT_DECODE);
- CptUsrCode(pCptWA);
- break;
- default: ;
- }
- } while (!fDone);
-
- }
-
-
- #endif
-
-