//=========================================================================== // CRYPTOLOGY: VERNAM ASCII - Lower Case // Nicholas Gessler // 11/14/2008 // // Notes: The decimal value of "a" is 97, thus if we subtract 97 from the // decimal values of the letters from "a" to "z" we have numbers // in the range from 1 to 26. // When we "extract" numeric values, we do so modulo 26. // //=========================================================================== #include #include #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 = clRed; char* clearText = new char[Form1->MemoClearText->Text.Length() + 1]; strcpy(clearText, Form1->MemoClearText->Text.t_str()); clearLength = StrLen(clearText); Form1->MemoClearASCII->Text = ""; for (int i = 0; i < clearLength; i++) { Form1->EditClearTextLength->Text = clearLength; char x = clearText[i]; if (i == 0) Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + " " + IntToStr(x-97); else Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + ", " + IntToStr(x-97); clearArray[i] = clearText[i] - 97; } break; } case KEY: { Form1->MemoKeyText->Font->Color = clRed; char* keyText = new char[Form1->MemoKeyText->Text.Length() + 1]; strcpy(keyText, Form1->MemoKeyText->Text.t_str()); keyLength = StrLen(keyText); Form1->MemoKeyASCII->Text = ""; for (int i = 0; i < keyLength; i++) { Form1->EditKeyTextLength->Text = keyLength; char x = keyText[i]; if (i == 0)Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + " " + IntToStr(x-97); else Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + ", " + IntToStr(x-97); keyArray[i] = keyText[i] - 97; } break; } case CIPHER: { Form1->MemoCipherText->Font->Color = clRed; char* cipherText = new char[Form1->MemoCipherText->Text.Length() + 1]; strcpy(cipherText, Form1->MemoCipherText->Text.t_str()); cipherLength = StrLen(cipherText); Form1->MemoCipherASCII->Text = ""; for (int i = 0; i < cipherLength; i++) { Form1->EditCipherTextLength->Text = cipherLength; char x = cipherText[i]; if (i == 0) Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + " " + IntToStr(x-97); else Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + ", " + IntToStr(x-97); cipherArray[i] = cipherText[i] - 97; } break; } } } //============================================================== EXTRACT TEXT void extract (int text) { switch (text) { case CLEAR: { Form1->MemoClearText->Font->Color = clBlue; 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] > 25) clearArray[i] -= 26; if (clearArray[i] < 0) clearArray[i] += 26; if (i == 0) Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + " " + IntToStr(clearArray[i]); else Form1->MemoClearASCII->Text = Form1->MemoClearASCII->Text + ", " + IntToStr(clearArray[i]); c = clearArray[i] + 97; Form1->MemoClearText->Text = Form1->MemoClearText->Text + c; } } else Form1->MemoClearText->Text = "Key Length Too Short"; break; } case KEY: { Form1->MemoKeyText->Font->Color = clBlue; 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] > 25) keyArray[i] -= 26; if (keyArray[i] < 0) keyArray[i] += 26; if (i == 0) Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + " " + IntToStr(keyArray[i]); else Form1->MemoKeyASCII->Text = Form1->MemoKeyASCII->Text + ", " + IntToStr(keyArray[i]); c = keyArray[i] + 97; Form1->MemoKeyText->Text = Form1->MemoKeyText->Text + c; } } else Form1->MemoKeyText->Text = "Clear and Cipher Lengths Unequal"; break; } case CIPHER: { Form1->MemoCipherText->Font->Color = clBlue; 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] > 25) cipherArray[i] -= 26; if (cipherArray[i] < 0) cipherArray[i] += 26; if (i == 0) Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + " " + IntToStr(cipherArray[i]); else Form1->MemoCipherASCII->Text = Form1->MemoCipherASCII->Text + ", " + IntToStr(cipherArray[i]); c = cipherArray[i] + 97; 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; }