home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////////
- //+
- // Module Name: cpt.cpp
- //
- // Description: CPT specific routines 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.
- //
- //
- // Include Modules Referenced: cpt.h
- //
- // Written by: John Tal
- //
- //
- // Modification history:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //-
- ////////////////////////////////////////////////////////////////////////
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #include <memincs.h>
- #include <cpt.h>
-
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: lpad
- //
- // Class: STRUTIL_C
- //
- // Security: Private
- //
- // Description: Left pad a field with blanks
- //
- // Parameters
- // In: str = string to pad
- // width = width to pad
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- STRUTIL_C::lpad(CHAR * str,INT width)
- {
- C_DEF_MODULE("STRUTIL_C::lpad")
-
- 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';
-
- C_RETURN
- }
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: srep
- //
- // Class: STRUTIL_C
- //
- // Security: Public
- //
- // Description: String replacement of characters
- //
- // Parameters
- // In: ch = string to check
- // dh = character to search for
- // eh = character to replace with
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT STRUTIL_C::srep(CHAR * ch,CHAR dh,CHAR eh)
- {
- C_DEF_MODULE("STRUTIL_C::srep")
-
- INT i;
-
- for(i = 0; i < strlen(ch); i++)
- if(ch[i] == dh)
- ch[i] = eh;
-
- C_RETURN
- }
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: SetJob
- //
- // Class: CPT_C
- //
- // Security: Public
- //
- // Description: Set Job type to CPT_CODE or CPT_DECODE
- //
- // Parameters
- // In: Job type to set to
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::SetJobType(INT iJob)
- {
- C_DEF_MODULE("CPT_C::SetJobType")
-
- iJobType = iJob;
-
- C_RETURN
- }
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: MemoryKeyGen
- //
- // Class: CPT_C
- //
- // Security: Public
- //
- // Description: Generate a key set in memory
- //
- // Parameters
- // In: iConvolutionSet = set to generate keys for
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::MemoryKeyGen(INT iConvolutionSet)
- {
- C_DEF_MODULE("CPT_C::MemoryKeyGen")
-
- 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] = i;
- cBytes[CPT_ENCODE][iConvolutionSet][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() % 256);
- cBytes[CPT_ENCODE][iConvolutionSet][top-1] = hold_area[f];
- if(f != top-1)
- hold_area[f] = hold_area[top-1];
- }
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: CptUsrGetKeyData
- //
- // Class: (Standalone)
- //
- // Security: N/A
- //
- // Description: Get key set parms from user
- //
- // Parameters
- // In: pcSeedPathName = File being looked for
- //
- // Return Codes: SHORT = FIO_OK = file exists
- // = FIO_NO_FILE = file does not exist
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CptGetKeyData(VOID)
- {
- C_DEF_MODULE("CptGetKeyData")
-
- INT keynum;
- INT i;
- CHAR szKeyStr[CPT_KEYSTR_LEN + 1];
- CHAR szKeyFile[CPT_FILE_LEN + 1];
- STRUTIL_C cStrUtil;
-
- printf("\n\n\nEnter Reference Number For This Key ");
- scanf("%d",&i);
- sprintf(szKeyStr,"%d",i);
- cStrUtil.lpad(szKeyStr,4);
- cStrUtil.srep(szKeyStr,' ','0');
-
- cCpt.SetKeyStr(szKeyStr);
-
- strcpy(szKeyFile,"key_");
- strcat(szKeyFile,szKeyStr);
- strcat(szKeyFile,".cpt");
-
- cCpt.SetKeyFile(szKeyFile);
-
- printf("\nEnter Convolutions (Usally 1, higher = tighter security) ");
- scanf("%d",i);
-
- cCpt.SetConvolutions(i);
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: GenKeys
- //
- // Class: CPT_C
- //
- // Security: Protected
- //
- // Description: Create keys.
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::GenKeys(VOID)
- {
- C_DEF_MODULE("CPT_C::GenKeys")
-
- INT i;
-
- for(i = 0; i < iConvolutions; i++)
- {
- C_STATUS = KeyGen(i);
- }
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: WriteKeys
- //
- // Class: CPT_C
- //
- // Security: Public
- //
- // Description: Write key file.
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
-
- SHORT CPT_C::WriteKeys(VOID)
- {
- INT iC;
- INT i;
-
- KeyFile = fopen(szKeyFile,"w+");
- if(KeyFile == NULL)
- C_LEAVE(C_NOTOK)
-
- fprintf(KeyFile,"%s %s %s %d\n",
- CPT_ID, CPT_VER, szKeyStr, iConvolutions);
-
- for(iC = 0; iC < iConvolutions; iC++)
- {
- for(i = 0; i < ASCIIBYTES+1; i++)
- fprintf(KeyFile,"%d\n", -> cBytes[CPT_ENCODE][iC][i]);
- }
-
- fclose(KeyFile);
- KeyFile = NULL;
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
-
- SHORT CPT_C::OpenKeyFile(VOID)
- {
- C_DEF_MODULE("CPT_C::OpenKeyFile")
-
- KeyFile = fopen(szKeyFile,"w+");
- if(KeyFile == NULL)
- C_LEAVE(C_NOTOK)
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- SHORT CPT_C::CloseKeyFile(VOID)
- {
- C_DEF_MODULE("CPT_C::CloseKeyFile")
-
- fclose(KeyFile);
- KeyFile = NULL;
-
- C_RETURN
- }
-
- SHORT CPT_C::OpenInFile(VOID)
- {
- C_DEF_MODULE("CPT_C::OpenInFile")
-
- InFile = fopen(szInFile,"rb");
- if(InFile == NULL)
- C_LEAVE(C_NOTOK)
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- SHORT CPT_C::CloseInFile(VOID)
- {
- C_DEF_MODULE("CPT_C::CloseInFile")
-
- fclose(InFile);
- InFile = NULL;
-
- C_RETURN
- }
-
-
- SHORT CPT_C::OpenOutFile(VOID)
- {
- C_DEF_MODULE("CPT_C::OpenOutFile")
-
- OutFile = fopen(szOutFile,"w+b");
- if(OutFile == NULL)
- C_LEAVE(C_NOTOK)
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- SHORT CPT_C::CloseOutFile(VOID)
- {
- C_DEF_MODULE("CPT_C::CloseInFile")
-
- fclose(OutFile);
- OutFile = NULL;
-
- C_RETURN
- }
-
-
- SHORT CPT_C::SetConvolutions(INT iConvs)
- {
- C_DEF_MODULE("CPT_C::SetConvolutions")
-
- iConvolutions = iConvs;
-
- if(iConvolutions > CPT_MAX_CONVOLUTIONS)
- iConvolutions = CPT_MAX_CONVOLUTIONS;
-
- C_RETURN
- }
-
- SHORT CPT_C::SetKeyFileName(PCHAR pcData)
- {
- C_DEF_MODULE("CPT_C::SetKeyFileName")
-
- strcpy(szKeyFile,pcData)
-
- C_RETURN
- }
-
- SHORT CPT_C::SetKeyStr(PCHAR pcData)
- {
- C_DEF_MODULE("CPT_C::SetKeyStr")
-
- strcpy(szKeyStr,pcData)
-
- C_RETURN
- }
-
- SHORT CPT_C::SetInFile(PCHAR pcData)
- {
- C_DEF_MODULE("CPT_C::SetInFile")
-
- strcpy(szInFile,pcData)
-
- C_RETURN
- }
-
-
- SHORT CPT_C::SetOutFile(PCHAR pcData)
- {
- C_DEF_MODULE("CPT_C::SetOutFile")
-
- strcpy(szOutFile,szKey)
-
- C_RETURN
- }
-
-
- SHORT CPT_C::SetKeyFile(FILE *fp)
- {
- C_DEF_MODULE("CPT_C::SetKeyFile")
-
- KeyFile = fp;
-
- C_RETURN
- }
-
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: ReadInKeys
- //
- // Class: CPT_C
- //
- // Security: Public
- //
- // Description: Read in keys from file
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::ReadInKeys(VOID)
- {
- C_DEF_MODULE("CPT_C::ReadInKeys")
-
- CHAR data[5];
- INT i,i2,iC;
-
- fscanf(KeyFile,"%s %s %s %d",
- szCptId,
- szCptVers,
- szCptKeyId,
- &iConvolutions);
-
- for(iC = 0; iC < iConvolutions; iC++)
- {
- for(i = 0; i < ASCIIBYTES+1; i++)
- {
- fgets(data,BUFSIZE,KeyFile);
- i2 = atoi(data);
- #ifdef DEBUG
- printf("Read %d (%c) at %d (%c)\n", i2,i2,i,i);
- #endif
- cBytes[CPT_ENCODE][iC][i] = i2;
- }
- }
-
- if(iJob == CPT_DECODE)
- ReverseKeys();
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: ReverseKeys
- //
- // Class: CPT_C
- //
- // Security: Protected
- //
- // Description: Reverse a key set
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK always returned
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::ReverseKeys(VOID)
- {
- C_DEF_MODULE("CPT_C::ReverseKeys")
-
- INT i,scan,iC;
-
- for(iC = 0; iC < iConvolutions; iC++)
- for(i = 0; i < ASCIIBYTES+1; i++)
- for(scan = 0; scan < ASCIIBYTES+1; scan++)
- if(cBytes[CPT_ENCODE][iC][scan] == i)
- {
- cBytes[CPT_DECODE][iC][i] = scan;
- #ifdef DEBUG
- printf("%d (%c) used at %d (%c)\n",scan,scan,i,i);
- #endif
- }
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: CodeFile
- //
- // Class: CPT_C
- //
- // Security: Public
- //
- // Description: Apply keys in conversion from input file to output file.
- // Is called for both encoding and decoding.
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::CodeFile(VOID)
- {
- C_DEF_MODULE("CPT_C::CodeFile")
-
- INT i,iC,iBytesRead;
- UCHAR szBuffer[BUFSIZE + 1];
-
- /*
- ** Reset convolution ptr
- */
-
- iC = 0;
-
- do
- {
- iBytesRead = fread(szBuffer,1,BUFSIZE,InFile);
- if(iBytesRead > 0)
- {
- CodeBuffer(szBuffer,szBuffer,iBytesRead);
- fwrite(szBuffer,1,iBytesRead,OutFile);
- }
-
- } while(iBytesRead != 0);
-
- C_MODULE_EXIT:
-
- C_RETURN
- }
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: CodeBuffer
- //
- // Class: CPT_C
- //
- // Security: Protected
- //
- // Description: Apply key translation/coding on a buffer
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CPT_C::CodeBuffer(CHAR * pcBufIn,CHAR * pcBufOut,INT iBufLen)
- {
- INT iPos;
-
- for(iPos = 0; iPos < iBufLen; iPos++)
- {
- #ifdef DEBUG
- printf("using %d %c for %d (%c)\n", cBytes[iJob][iC][pcBufIn[iPos]],pCptWA ->
- #endif
- pcBufOut[iPos] = cBytes[iJob][iC][pcBufIn[iPos]];
- if(->iConvolutions > 1)
- AdjustConvolution();
- }
- }
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: Adjust Convolution
- //
- // Class: CPT_C
- //
- // Security: Protected
- //
- // Description: Adjust convolutoin prt as part of buffer coding process
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT AdjustConvolution(CPT_WORK_AREA_P )
- {
- C_DEF_MODULE("CPT_C::AdjustConvolution")
-
- iC++;
- if(iC >= iConvolutions)
- iC = 0;
-
- C_RETURN
- }
-
-
- ////////////////////////////////////////////////////////////////////////
- //+
- // Function Name: UsrCode
- //
- // Class: (Standalone)
- //
- // Security: N/A
- //
- // Description: Get Code/decode info from user
- //
- // Parameters
- // In: NONE
- //
- // Return Codes: SHORT = C_OK = success
- // !C_OK = failure
- //
- // Written by: John Tal
- //
- //
- // Modification History:
- //
- // Date Engineer Mod # Modification Description
- //
- // 12-Feb-1992 Tal v 1.0-001 Initial conversion to C++
- //
- //-
- ////////////////////////////////////////////////////////////////////////
-
- SHORT CptUsrCode(VOID)
- {
- C_DEF_MODULE("CptUsrCode")
-
- INT keynum;
- CHAR key_str[16];
- CHAR job_code[4];
-
- if(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(szKeyFile,"key_");
- strcat(szKeyFile,key_str);
- strcat(szKeyFile,".cpt");
-
- printf("\nFile To %sCode ",job_code);
- gets(szInFile);
-
- printf("\n\nName For %sCoded Result File ",job_code);
- gets(szOutFile);
-
- C_STATUS = CptReadInKeys();
-
- if(C_STATUS)
- C_LEAVE(C_STATUS)
-
- C_STATUS = CptCodeFile();
-
- 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 ;
-
-
- memset(&stCptWA,0,sizeof(CPT_WORK_AREA_T));
- = &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();
- CptGenKeys();
- break;
- case 2 : CptSetJob(,CPT_ENCODE);
- CptUsrCode();
- break;
- case 3 : CptSetJob(,CPT_DECODE);
- CptUsrCode();
- break;
- default: ;
- }
- } while (!fDone);
-
- }
-
-
- #endif
-