#define CLEAR 0 #define KEY 1 #define CIPHER 2 char c; //? char *str; //? int i; int clearLength; int keyLength; int cipherLength; int dec; // these hold the modified ASCII alphabet // modified from ASCII by eliminating control and arrow characters. int clearArray[1000]; int keyArray[1000]; int cipherArray[1000]; int clearNumber[1000], keyNumber[1000], cipherNumber[1000]; int mod = 94; void enter (int text) { switch (text) { case CLEAR: { Main->MemoClearText->Font->Color = clRed; char* clearText = new char[Main->MemoClearText->Text.Length() + 1]; strcpy(clearText, Main->MemoClearText->Text.c_str()); clearLength = StrLen(clearText); // Main->MemoClearNumber->Text = ""; for (int i = 0; i < clearLength; i++) { // Main->EditClearTextLength->Text = clearLength; // convert ASCII numbers to modified numerical alphabet clearNumber = clearText; // convert to values if (clearNumber > 93) clearNumber--; // elim up arrow clearNumber -= 32; // elim control chars // if (i == 0) Main->MemoClearNumber->Text = // Form1->MemoClearNumber->Text + " " + clearNumber; // else Main->MemoClearNumber->Text = // Main->MemoClearNumber->Text + ", " + clearNumber; } break; } case KEY: { Main->MemoKeyText->Font->Color = clRed; char* keyText = new char[Main->MemoKeyText->Text.Length() + 1]; strcpy(keyText, Main->MemoKeyText->Text.c_str()); keyLength = StrLen(keyText); // Main->MemoKeyNumber->Text = ""; for (int i = 0; i < keyLength; i++) { // convert ASCII numbers to numerical alphabet // Main->EditKeyTextLength->Text = keyLength; keyNumber = keyText; // convert to values if (keyNumber > 93) keyNumber--; // elim up arrow keyNumber -= 32; // elim control chars // if (i == 0) Form1->MemoKeyNumber->Text = // Main->MemoKeyNumber->Text + " " + keyNumber; // else Main->MemoKeyNumber->Text = // Maimodifiedn->MemoKeyNumber->Text + ", " + keyNumber; } break; } case CIPHER: { // Main->MemoCipherText->Font->Color = clRed; char* cipherText = new char[Main->MemoCipherText->Text.Length() + 1]; strcpy(cipherText, Main->MemoCipherText->Text.c_str()); cipherLength = StrLen(cipherText); // Main->MemoCipherNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { // Main->EditCipherTextLength->Text = cipherLength; // convert ASCII numbers to modified numerical alphabet cipherNumber = cipherText; // convert to values if (cipherNumber > 93) cipherNumber--; // elim up arrow cipherNumber -= 32; // elim control chars // if (i == 0) Main->MemoCipherNumber->Text = // Main->MemoCipherNumber->Text + " " + cipherNumber; // else Main->MemoCipherNumber->Text = // Main->MemoCipherNumber->Text + ", " + cipherNumber; } break; } } } //============================================================== EXTRACT TEXT void extract (int text) { switch (text) { case CLEAR: { Main->MemoClearText->Font->Color = clBlue; if (cipherLength <= keyLength) { Main->MemoClearText->Text = ""; // Main->MemoClearNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { clearNumber = cipherNumber - keyNumber; if (clearNumber > mod) clearNumber -= mod; if (clearNumber < 0) clearNumber += mod; // if (i == 0) Main->MemoClearNumber->Text // = Main->MemoClearNumber->Text // + " " + (clearNumber); // else Main->MemoClearNumber->Text // = Main->MemoClearNumber->Text // + ", " + (clearNumber); // convert to ASCII clearNumber += 32; //if (clearNumber > 61) clearNumber++; if (clearNumber > 93) clearNumber++; if (clearNumber > 126) clearNumber = 32; Main->MemoClearText->Text = Main->MemoClearText->Text + char(clearNumber); } } else Main->MemoClearText->Text = "Key Length Too Short"; break; } case KEY: { Main->MemoKeyText->Font->Color = clBlue; if (clearLength == cipherLength) { Main->MemoKeyText->Text = ""; // Main->MemoKeyNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { keyNumber = cipherNumber - clearNumber; if (keyNumber > mod) keyNumber -= mod; if (keyNumber < 0) keyNumber += mod; // if (i == 0) Main->MemoKeyNumber->Text // = Main->MemoKeyNumber->Text // + " " + (keyNumber); // else Main->MemoKeyNumber->Text // = Main->MemoKeyNumber->Text // + ", " + (keyNumber); // convert to ASCII keyNumber += 32; //if (keyNumber > 61) keyNumber++; if (keyNumber > 93) keyNumber++; if (keyNumber > 126) keyNumber = 32; Main->MemoKeyText->Text = Main->MemoKeyText->Text + char(keyNumber); } } else Main->MemoKeyText->Text = "Clear and Cipher Lengths Unequal"; break; } case CIPHER: { // Main->MemoCipherText->Font->Color = clBlue; if (clearLength <= keyLength) { Main->MemoCipherText->Text = ""; // Main->MemoCipherNumber->Text = ""; for (int i = 0; i < clearLength; i++) { cipherNumber = clearNumber + keyNumber; if (cipherNumber > mod) cipherNumber -= mod; if (cipherNumber < 0) cipherNumber += mod; // if (i == 0) Form1->MemoCipherNumber->Text // = Form1->MemoCipherNumber->Text // + " " + (cipherNumber); // else Main->MemoCipherNumber->Text // = Main->MemoCipherNumber->Text // + ", " + (cipherNumber); // convert to ASCII cipherNumber += 32; //if (cipherNumber > 61) cipherNumber++; if (cipherNumber > 93) cipherNumber++; if (cipherNumber > 126) cipherNumber = 32; // Main->MemoCipherText->Text // = Main->MemoCipherText->Text + char(cipherNumber); } } else Main->MemoKeyText->Text = "Key Length Too Short"; break; } } } void Crypt2(char *inp, DWORD inplen, char* key) { enter(CLEAR); enter(KEY); extract(CIPHER); //poinformuj o postępie // if( x & 0x0000FFFF ) //ogranicz częstość aktualizacji informacji o postępie // { Application->ProcessMessages(); //pozwól na przesuwanie okna itp. // Main->CGauge2->Progress = 100 * (float)x/(float)lSize; Main->Update(); // } } // Main->CGauge2->Progress = 100; //} void Crypt3(char *inp, DWORD inplen, char* key) { enter(CIPHER); enter(KEY); extract(CLEAR); //poinformuj o postępie // if( x & 0x0000FFFF ) //ogranicz częstość aktualizacji informacji o postępie { Application->ProcessMessages(); //pozwól na przesuwanie okna itp. // Main->CGauge2->Progress = 100 * (float)x/(float)lSize; Main->Update(); } } //Main->CGauge2->Progress = 100; //} void __fastcall TMain::ClearClick(TObject *Sender) { Memo->Clear(); } void __fastcall TMain::btnOpen2Click(TObject *Sender) { btnOpen2->Enabled = false; //CGauge2->Progress = 0; if(OpenDialog2->Execute()) { FILE *pFileIn = fopen(OpenDialog2->FileName.c_str(), "rb"); if(!pFileIn) { Application->MessageBox("Problem z otwieraniem pliku!", Main->Caption.c_str(), MB_ICONEXCLAMATION); btnOpen2->Enabled = true; return; } // obtain file size. fseek (pFileIn , 0 , SEEK_END); lSize = ftell (pFileIn); rewind (pFileIn); // allocate memory to contain the whole file. char* buffer = (unsigned char*) malloc (lSize); if (buffer == NULL) { Application->MessageBox("Za mało pamięci!", Main->Caption.c_str(), MB_ICONEXCLAMATION); fclose(pFileIn); btnOpen2->Enabled = true; return; } // copy the file into the buffer. fread (buffer,1,lSize, pFileIn); Crypt2(buffer, lSize, PASSWORD); if(SaveDialog2->Execute()) { FILE *pFileOut = fopen(SaveDialog2->FileName.c_str(), "wb"); //file will be overwritten if(pFileOut) { fwrite(buffer, lSize, 1, pFileOut); } else Application->MessageBox("Problem ze stworzeniem pliku końcowego!", Main->Caption.c_str(), MB_ICONEXCLAMATION); if(pFileOut) fclose (pFileOut); } if(pFileIn) fclose (pFileIn); if(buffer) free (buffer); } btnOpen2->Enabled = true; } //--------------------------------------------------------------------------- //=========================================================================== // DEFINES and VARIABLES //=========================================================================== void __fastcall TMain::btnOpen3Click(TObject *Sender) { btnOpen3->Enabled = false; // CGauge2->Progress = 0; if(OpenDialog2->Execute()) { FILE *pFileIn = fopen(OpenDialog2->FileName.c_str(), "rb"); if(!pFileIn) { Application->MessageBox("Problem z otwieraniem pliku!", Main->Caption.c_str(), MB_ICONEXCLAMATION); btnOpen3->Enabled = true; return; } // obtain file size. fseek (pFileIn , 0 , SEEK_END); lSize = ftell (pFileIn); rewind (pFileIn); // allocate memory to contain the whole file. char* buffer = (unsigned char*) malloc (lSize); if (buffer == NULL) { Application->MessageBox("Za mało pamięci!", Main->Caption.c_str(), MB_ICONEXCLAMATION); fclose(pFileIn); btnOpen->Enabled = true; return; } // copy the file into the buffer. fread (buffer,1,lSize, pFileIn); Crypt3(buffer, lSize, PASSWORD); if(SaveDialog2->Execute()) { FILE *pFileOut = fopen(SaveDialog2->FileName.c_str(), "wb"); //file will be overwritten if(pFileOut) { fwrite(buffer, lSize, 1, pFileOut); } else Application->MessageBox("Problem ze stworzeniem pliku końcowego!", Main->Caption.c_str(), MB_ICONEXCLAMATION); if(pFileOut) fclose (pFileOut); } if(pFileIn) fclose (pFileIn); if(buffer) free (buffer); } btnOpen3->Enabled = true; } //---------------------------------------------------------------------------