//=========================================================================== // CRYPTOLOGY: CLEAR, KEY AND CIPHER // Nicholas Gessler // 2/16/2005 //=========================================================================== #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //=========================================================================== // DEFINES and VARIABLES //=========================================================================== #define CLEAR 0 #define KEY 1 #define CIPHER 2 char c; char *str; int i; int clearLength; int keyLength; int cipherLength; int dec; int clearArray[1000]; int keyArray[1000]; int cipherArray[1000]; //=========================================================================== // FUNCTIONS //=========================================================================== //================================================================ ENTER TEXT void enter (int text) { switch (text) { case CLEAR: { Form1->MemoClearText->Font->Color = clBlack; char* clearText = new char[ Form1->MemoClearText->Text.Length() + 1 ]; strcpy( clearText, Form1->MemoClearText->Text.c_str() ); clearLength = StrLen(clearText); Form1->MemoClearASCII->Text = ""; for (int i = 0; i < clearLength; i++) { Form1->EditClearTextLength->Text = clearLength; char x = clearText[i]; Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + " " + IntToStr(x-96); clearArray[i] = clearText[i] - 96; } break; } case KEY: { Form1->MemoKeyText->Font->Color = clBlack; char* keyText = new char[ Form1->MemoKeyText->Text.Length() + 1 ]; strcpy( keyText, Form1->MemoKeyText->Text.c_str() ); keyLength = StrLen(keyText); Form1->MemoKeyASCII->Text = ""; for (int i = 0; i < keyLength; i++) { Form1->EditKeyTextLength->Text = keyLength; char x = keyText[i]; Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + " " + IntToStr(x-96); keyArray[i] = keyText[i] - 96; } break; } case CIPHER: { Form1->MemoCipherText->Font->Color = clBlack; char* cipherText = new char[ Form1->MemoCipherText->Text.Length() + 1 ]; strcpy( cipherText, Form1->MemoCipherText->Text.c_str() ); cipherLength = StrLen(cipherText); Form1->MemoCipherASCII->Text = ""; for (int i = 0; i < cipherLength; i++) { Form1->EditCipherTextLength->Text = cipherLength; char x = cipherText[i]; Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + " " + IntToStr(x-96); cipherArray[i] = cipherText[i] - 96; } break; } } } //============================================================== EXTRACT TEXT void extract (int text) { switch (text) { case CLEAR: { if (cipherLength <= keyLength) { Form1->MemoClearText->Text = ""; Form1->MemoClearASCII->Text = ""; for (int i = 0; i < cipherLength; i++) { clearArray[i] = cipherArray[i] - keyArray[i]; if (clearArray[i] > 26) clearArray[i] -= 26; if (clearArray[i] < 1) clearArray[i] += 26; Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + " " + IntToStr(clearArray[i]); c = clearArray[i] + 96; Form1->MemoClearText->Text = Form1->MemoClearText->Text + c; } } else Form1->MemoClearText->Text = "Key Length Too Short"; break; } case KEY: { if (clearLength == cipherLength) { Form1->MemoKeyText->Text = ""; Form1->MemoKeyASCII->Text = ""; for (int i = 0; i < cipherLength; i++) { keyArray[i] = cipherArray[i] - clearArray[i]; if (keyArray[i] > 26) keyArray[i] -= 26; if (keyArray[i] < 1) keyArray[i] += 26; Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + " " + IntToStr(keyArray[i]); c = keyArray[i] + 96; Form1->MemoKeyText->Text = Form1->MemoKeyText->Text + c; } } else Form1->MemoKeyText->Text = "Clear and Cipher Lengths Unequal"; break; } case CIPHER: { if (clearLength <= keyLength) { Form1->MemoCipherText->Text = ""; Form1->MemoCipherASCII->Text = ""; for (int i = 0; i < clearLength; i++) { cipherArray[i] = clearArray[i] + keyArray[i]; if (cipherArray[i] > 26) cipherArray[i] -= 26; if (cipherArray[i] < 1) cipherArray[i] += 26; Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + " " + IntToStr(cipherArray[i]); c = cipherArray[i] + 96; Form1->MemoCipherText->Text = Form1->MemoCipherText->Text + c; } } else Form1->MemoCipherText->Text = "Key Length Too Short"; break; } } } //=========================================================================== // EVENT HANDLERS //=========================================================================== //-------------------------------------------------------- ENTER CLEAR BUTTON void __fastcall TForm1::ButtonEnterClearClick(TObject *Sender) { enter(CLEAR); } //---------------------------------------------------------- ENTER KEY BUTTON void __fastcall TForm1::ButtonEnterKeyClick(TObject *Sender) { enter(KEY); } //------------------------------------------------------- ENTER CIPHER BUTTON void __fastcall TForm1::ButtonEnterCipherClick(TObject *Sender) { enter(CIPHER); } //------------------------------------------------------ EXTRACT CLEAR BUTTON void __fastcall TForm1::ButtonExtractClearClick(TObject *Sender) { extract(CLEAR); } //-------------------------------------------------------- EXTRACT KEY BUTTON void __fastcall TForm1::ButtonExtractKeyClick(TObject *Sender) { extract(KEY); } //----------------------------------------------------- EXTRACT CIPHER BUTTON void __fastcall TForm1::ButtonExtractCipherClick(TObject *Sender) { extract(CIPHER); } //---------------------------------------------------- MEMO CLEAR TEXT CHANGE void __fastcall TForm1::MemoClearTextChange(TObject *Sender) { MemoClearText->Font->Color = clGray; } //------------------------------------------------------ MEMO KEY TEXT CHANGE void __fastcall TForm1::MemoKeyTextChange(TObject *Sender) { MemoKeyText->Font->Color = clGray; } //--------------------------------------------------- MEMO CIPHER TEXT CHANGE void __fastcall TForm1::MemoCipherTextChange(TObject *Sender) { MemoCipherText->Font->Color = clGray; } //------------------------------------ ASCII CHARACTER TO DECIMAL CODE BUTTON void __fastcall TForm1::ButtonASCIICharacterToDecimalCodeClick(TObject *Sender) { char* k = new char [Form1->EditEnterCharacter->Text.Length() + 1]; strcpy (k, Form1->EditEnterCharacter->Text.c_str()); i = int(*k); Form1->EditGetDecimal->Text = i; } //------------------------------------ DECIMAL CODE TO ASCII CHARACTER BUTTON void __fastcall TForm1::ButtonDecimalCodeToASCIICharacterClick(TObject *Sender) { c = StrToInt(Form1->EditEnterDecimal->Text); Form1->EditGetCharacter->Text = c; } //----------------------------------------------------------------------- END