//=========================================================================== // CRYPTOLOGY: VERNAM full ASCII // Printable characters only. // No control characters. // No up or down arrows. // Nicholas Gessler // 4/25/2012 // // // We subtract 32 from the ASCII decimal values // We add and subtract modulo mod // However, this includes up_arrow char(94) and down_arrow char(127) // neither of which can be entered from the keyboard // We must remove them from the calculations // Collapse char(mod) through char(126) by subtracting 1 // Terminate char(127) // Revise modulo from mod to mod // //=========================================================================== #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; // 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; //=========================================================================== // 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->MemoClearNumber->Text = ""; for (int i = 0; i < clearLength; i++) { Form1->EditClearTextLength->Text = clearLength; // convert ASCII numbers to modified numerical alphabet clearNumber[i] = clearText[i]; // convert to values if (clearNumber[i] > 93) clearNumber[i]--; // elim up arrow clearNumber[i] -= 32; // elim control chars if (i == 0) Form1->MemoClearNumber->Text = Form1->MemoClearNumber->Text + " " + clearNumber[i]; else Form1->MemoClearNumber->Text = Form1->MemoClearNumber->Text + ", " + clearNumber[i]; } 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->MemoKeyNumber->Text = ""; for (int i = 0; i < keyLength; i++) { Form1->EditKeyTextLength->Text = keyLength; // convert ASCII numbers to modified numerical alphabet keyNumber[i] = keyText[i]; // convert to values if (keyNumber[i] > 93) keyNumber[i]--; // elim up arrow keyNumber[i] -= 32; // elim control chars if (i == 0) Form1->MemoKeyNumber->Text = Form1->MemoKeyNumber->Text + " " + keyNumber[i]; else Form1->MemoKeyNumber->Text = Form1->MemoKeyNumber->Text + ", " + keyNumber[i]; } 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->MemoCipherNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { Form1->EditCipherTextLength->Text = cipherLength; // convert ASCII numbers to modified numerical alphabet cipherNumber[i] = cipherText[i]; // convert to values if (cipherNumber[i] > 93) cipherNumber[i]--; // elim up arrow cipherNumber[i] -= 32; // elim control chars if (i == 0) Form1->MemoCipherNumber->Text = Form1->MemoCipherNumber->Text + " " + cipherNumber[i]; else Form1->MemoCipherNumber->Text = Form1->MemoCipherNumber->Text + ", " + cipherNumber[i]; } break; } } } //============================================================== EXTRACT TEXT void extract (int text) { switch (text) { case CLEAR: { Form1->MemoClearText->Font->Color = clBlue; if (cipherLength <= keyLength) { Form1->MemoClearText->Text = ""; Form1->MemoClearNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { clearNumber[i] = cipherNumber[i] - keyNumber[i]; if (clearNumber[i] > mod) clearNumber[i] -= mod; if (clearNumber[i] < 0) clearNumber[i] += mod; if (i == 0) Form1->MemoClearNumber->Text = Form1->MemoClearNumber->Text + " " + (clearNumber[i]); else Form1->MemoClearNumber->Text = Form1->MemoClearNumber->Text + ", " + (clearNumber[i]); // convert to ASCII clearNumber[i] += 32; //if (clearNumber[i] > 61) clearNumber[i]++; if (clearNumber[i] > 93) clearNumber[i]++; if (clearNumber[i] > 126) clearNumber[i] = 32; Form1->MemoClearText->Text = Form1->MemoClearText->Text + char(clearNumber[i]); } } else Form1->MemoClearText->Text = "Key Length Too Short"; break; } case KEY: { Form1->MemoKeyText->Font->Color = clBlue; if (clearLength == cipherLength) { Form1->MemoKeyText->Text = ""; Form1->MemoKeyNumber->Text = ""; for (int i = 0; i < cipherLength; i++) { keyNumber[i] = cipherNumber[i] - clearNumber[i]; if (keyNumber[i] > mod) keyNumber[i] -= mod; if (keyNumber[i] < 0) keyNumber[i] += mod; if (i == 0) Form1->MemoKeyNumber->Text = Form1->MemoKeyNumber->Text + " " + (keyNumber[i]); else Form1->MemoKeyNumber->Text = Form1->MemoKeyNumber->Text + ", " + (keyNumber[i]); // convert to ASCII keyNumber[i] += 32; //if (keyNumber[i] > 61) keyNumber[i]++; if (keyNumber[i] > 93) keyNumber[i]++; if (keyNumber[i] > 126) keyNumber[i] = 32; Form1->MemoKeyText->Text = Form1->MemoKeyText->Text + char(keyNumber[i]); } } else Form1->MemoKeyText->Text = "Clear and Cipher Lengths Unequal"; break; } case CIPHER: { Form1->MemoCipherText->Font->Color = clBlue; if (clearLength <= keyLength) { Form1->MemoCipherText->Text = ""; Form1->MemoCipherNumber->Text = ""; for (int i = 0; i < clearLength; i++) { cipherNumber[i] = clearNumber[i] + keyNumber[i]; if (cipherNumber[i] > mod) cipherNumber[i] -= mod; if (cipherNumber[i] < 0) cipherNumber[i] += mod; if (i == 0) Form1->MemoCipherNumber->Text = Form1->MemoCipherNumber->Text + " " + (cipherNumber[i]); else Form1->MemoCipherNumber->Text = Form1->MemoCipherNumber->Text + ", " + (cipherNumber[i]); // convert to ASCII cipherNumber[i] += 32; //if (cipherNumber[i] > 61) cipherNumber[i]++; if (cipherNumber[i] > 93) cipherNumber[i]++; if (cipherNumber[i] > 126) cipherNumber[i] = 32; Form1->MemoCipherText->Text = Form1->MemoCipherText->Text + char(cipherNumber[i]); } } 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; }