Welcome Guest [Log In] [Register]
Welcome to Crypto. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Shift Cipher Cracker
Topic Started: Sep 11 2005, 05:14 PM (214 Views)
Revelation
Member Avatar
Administrator
[ *  *  *  *  * ]
This weekend I made a shift cipher cracker (Caesar's cipher). It will skip non-alphabetic characters and there is an option to include spaces. The file is 22kb.

Download it here.

Here is the source:

Code:
 

program SCC;

uses
 Windows,
 Messages,
 SysUtils;

var
 wClass: TWndClass;
 hAppHandle,
   hInst,
   hExecuteBut,
   hFont,
   hInputLabel,
   hPossibLabel,
   hPossibMemo,
   hEdit,
   hCheckBox: HWND;
 Msg: TMSG;
 R: TRect;

{$R Icons.res}

procedure ShutDown;
begin
 DeleteObject(hFont);
 UnRegisterClass('SCC', hInst);
 ExitProcess(hInst);
end;

function GetText(hWnd: HWND): string;
var
 i: Integer;
begin
 i := GetWindowTextLength(hWnd);
 SetLength(Result, i);
 GetWindowText(hWnd, PChar(Result), Succ(i));
end;

function IsChecked(hWnd: HWND): Boolean;
begin
 Result := False;
 if SendMessage(hWnd, BM_GETCHECK, 0, 0) = BST_CHECKED then
   Result := True;
end;

procedure EncryptDecrypt;
var
 i, x: integer;
 EncText, TempText: string;
begin
 EncText := UpperCase(GetText(hEdit));
 for i := 1 to 25 do
 begin
   TempText := TempText + 'Shift #' + IntToStr(i) + ': ';
   for x := 1 to Length(EncText) do
     if EncText[x] <> ' ' then
     begin
       if EncText[x] in ['A'..'Z'] then
       begin
         if Ord(EncText[x]) + i > 90 then
           TempText := TempText + Chr(Ord(EncText[x]) + i - 26)
         else
           TempText := TempText + Chr(Ord(EncText[x]) + i);
       end;
     end
     else
       if IsChecked(hCheckBox) then
         TempText := TempText + ' ';

   TempText := TempText + #13#10;
 end;
 SetWindowText(hPossibMemo, PChar(TempText));
end;

function WindowProc(hWnd, Msg, wParam, lParam: LongInt): Longint; stdcall;
begin
 case Msg of
   WM_COMMAND:
     if lParam = hExecuteBut then
       EncryptDecrypt;
   WM_DESTROY: ShutDown;
 end;

 Result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;

begin
 hInst := GetModuleHandle(nil);

 with wClass do
 begin
   Style := CS_HREDRAW or CS_VREDRAW;
   hIcon := LoadIcon(hInst, 'MAIN_ICON');
   lpfnWndProc := @WindowProc;
   hInstance := hInst;
   hbrBackground := COLOR_BTNFACE + 1;
   lpszClassName := 'SCC';
   hCursor := LoadCursor(0, IDC_ARROW);
 end;

 RegisterClass(wClass);

 GetWindowRect(GetDesktopWindow, R);

 hAppHandle := CreateWindow('SCC', 'Shift Cipher Cracker',
   WS_OVERLAPPEDWINDOW,
   R.Right div 2 - 332 div 2, R.Bottom div 2 - 307 div 2, 332, 307, 0, 0,
   hInst, nil);

 hExecuteBut := CreateWindow('Button', 'Encrypt/Decrypt',
   WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT or WS_TABSTOP,
   184, 23, 89, 25, hAppHandle, 0, hInst, nil);

 hCheckBox := CreateWindow('Button', 'Include spaces',
   WS_VISIBLE or WS_CHILD or BS_AUTOCHECKBOX or BS_TEXT or WS_TABSTOP,
   216, 64, 97, 17, hAppHandle, 0, hInst, nil);

 hEdit := CreateWindowEx(WS_EX_CLIENTEDGE, 'Edit', nil, WS_VISIBLE or
   WS_CHILD or ES_LEFT or ES_AUTOHSCROLL or WS_TABSTOP, 8, 24, 161, 21,
   hAppHandle, 0, hInst, nil);

 hPossibMemo := CreateWindowEx(WS_EX_CLIENTEDGE, 'Edit', '', WS_VISIBLE or
   WS_CHILD or ES_LEFT or ES_MULTILINE or ES_AUTOVSCROLL or
   WS_VSCROLL or ES_READONLY, 8, 88, 305, 177, hAppHandle, 0, hInst, nil);

 hInputLabel := CreateWindow('Static', 'Enter text:',
   WS_VISIBLE or WS_CHILD or SS_LEFT, 8, 8, 52, 13, hAppHandle, 0, hInst,
     nil);

 hPossibLabel := CreateWindow('Static',
   'Encryption/Decryption Possibilities:',
   WS_VISIBLE or WS_CHILD or SS_LEFT, 8, 72, 160, 13, hAppHandle, 0, hInst,
   nil);

 hFont := CreateFont(-12, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET,
   OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH or
   FF_DONTCARE, 'MS Sans Serif');

 SendMessage(hExecuteBut, WM_SETFONT, hFont, 0);
 SendMessage(hEdit, WM_SETFONT, hFont, 0);
 SendMessage(hPossibMemo, WM_SETFONT, hFont, 0);
 SendMessage(hInputLabel, WM_SETFONT, hFont, 0);
 SendMessage(hPossibLabel, WM_SETFONT, hFont, 0);
 SendMessage(hCheckBox, WM_SETFONT, hFont, 0);

 SetFocus(hEdit);

 ShowWindow(hAppHandle, SW_SHOW);
 UpdateWindow(hAppHandle);

 while GetMessage(Msg, hAppHandle, 0, 0) do
 begin
   if not (IsDialogMessage(hAppHandle, Msg)) then
   begin
     TranslateMessage(Msg);
     DispatchMessage(Msg);
   end;
 end;
end.


So never post a caesar cipher challenge again ;)
RRRREJMEEEEEPVKLWENFNVJKEEEEEAOLKAFKLXCFZAASDJXZTTTTTTTLSIOWJXMOKLAFJNNKFNXN
RAGRBAQEMHIGDJVDSEOXVIYCELFHWLELJFIENXLRATALSJFSLCYTKLASJDKMHGOVOKAJDNMNUITN
RRRRLJVEEEEECLYVYHNVPFTAEEEEEMWLMEIRNGLARWJAKJDFLWNTIERJMIPQWOTZEOCXKNUBNXCN
RJIRPOWEANFUSNCZVDVZNMSFEKLOEPZLDKDJWSAAAAAAAOERHJCTNCKFRIMVKSOFOMKMANREWNBN
RZUDRGXEEEEENFQIDVLQNCKNEEEEEDGLLLLLLAWIOSNCDARLODMTOEJXMILDFJROTKJSDNLVCZNN
Offline Profile Quote Post Goto Top
 
insecure
Elite member
[ *  *  *  *  * ]
Your shift cipher cracker brute-forces 25 different shifts, which is fine for an alphabet ciphertext.

Consider, however, a shift cipher which works over the whole range of an octet rather than merely over 26 alphabetic letters. For this, you'd need 255 iterations. And if the shift affected two-octet chunks, you'd have to iterate 65535 times to guarantee a decrypt - and this would be a bit of a nuisance to sort through manually.

A more elegant solution (if we may assume that the plaintext is English text) would be to frequency-analyse the decrypt. Not great for short plaintexts, admittedly.

A less elegant, but perhaps more robust, solution springs to mind - that of spell-checking the decrypt. A decrypt that has a reasonable percentage of correctly-spelled words could be displayed, with other decrypts being suppressed. It seems to me that, even allowing for quite a few spelling errors, this method would produce a "shortlist" very quickly, from which you could select the correct decrypt.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · General · Next Topic »
Add Reply