AHK: How to Decipher the Caesar Shift with Code

VG JNF N OEVTUG PBYQ QNL VA NCEVY NAQ GUR PYBPXF JRER FGEVXVAT GUVEGRRA JVAFGBA FZVGU UVF PUVA AHMMYRQ VAGB UVF OERNFG VA NA  RSSBEG GB RFPNCR GUR IVYR JVAQ FYVCCRQ DHVPXYL GUEBHTU GUR TYNFF QBBEF BS IVPGBEL ZNAFVBAF GUBHTU ABG DHVPXYL RABHTU GB CERIRAG N FJVEY BS TEVGGL QHFG SEBZ RAGREVAT NYBAT JVGU UVZ

-No idea what this says. Damn you, Caesar.

The Caesar Shift is supposed to be ridiculously easy to decipher. I say supposed to be because while it might be easy to crack, translating the bastard is mind-numbingly painful if you have a substantial block of text.

There are quite a few resources online that’ll do it for you, but for kicks, I wrote an AHK that automates it—if you know how many positions you want to shift.

So How Does the Shift Work?

There are 26 letters in the alphabet, and luckily, all those letters have a set place. A=1, B=2, C=3 and so on. But what if the alphabet didn’t start with A? You could shift it all over so that Z=1, A=2, B=3, all the way to Y=26. That shift can be referred to ROT1, meaning rotate all the letters up by 1. Basically, it translates to Z=1=A. So, if I wanted to say Aaaaah!, I’d write Zzzzzg!.

ROT2 is the same deal, rotated up by two. So, Yyyyyf!
ROT25 is the same deal, rotated up by 25 letters. So, Bbbbbi!
ROT26 is the same deal, rotated up by 26 letters. So, Aaaaah!

The Code:

This AHK comes to you in 4 parts.

Part 1: A User Interface
Asks the user for a file to translate and the number of positions to shift (ROT variable).

Part 2: Convert Each Ciphered Letter into Number
Opens the file and loops through each character. Assigns A to 1, B to 2, C to 3 and so on. Copies the results into the Clipboard.

Part 3: Calculate the Shifted Alphabet Numbers

Adds the ROT value to the A variable for its shifted value, then adds 1 to the B variable, then adds 1 to the C variable and so on.

Part 4: Translate Ciphered Numbers into Letters

Runs Clipboard through a loop. If the number matches the shifted A variable, puts A into Translation. If number matches the shifted B variable, puts B into Translation. And so on.

Important Commands to Note:

Aside from the basic GUI code and conditionals, this code uses FileRead and a Parsing Loop. FileRead takes the characters within a file and stashes them in a variable. The Parsing Loop then takes that variable, chops it into manageable pieces, and changes those pieces as instructed by the user.

FileRead

ParseLoop

Now, I’ve been told that learning to code is all about efficiency. Short and concise means there’ll be fewer lines to comb for errors, fewer lines to tweak if you need to change something, fewer opportunities to type rum instead of run because those words look exactly alike in a giant wall of text.

Today is not about efficiency though. This is not pretty code, but damn it, it works.

Here’s the run down:

CodeExp

If all has gone well, the computer should do this:

Caesar

Note that’s not the complete code because cycling the parsing loops through each letter gets really long. So this is the actual code that’ll give you a full translation:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Gui, Add, Text, , Desired File Path: C:\
Gui, Add, Edit, vFilePath, Users\Desktop\Test.txt
Gui, Add, Text, , ROT
Gui, Add, Edit, vROT
Gui, Add, Button, default, OK
Gui, Show, , Simple Input Example
return

ButtonOk:
Gui, Submit

clipboard=

FileRead, AllTheText, C:\%FilePath%
Loop, parse, AllTheText,,
{
if (A_LoopField=”A”)
{
clipboard=%clipboard%@1
continue
}
else if (A_LoopField=”B”)
{
clipboard=%clipboard%@2
continue
}
else if (A_LoopField=”C”)
{
clipboard=%clipboard%@3
continue
}
else if (A_LoopField=”D”)
{
clipboard=%clipboard%@4
continue
}
else if (A_LoopField=”E”)
{
clipboard=%clipboard%@5
continue
}
else if (A_LoopField=”F”)
{
clipboard=%clipboard%@6
continue
}
else if (A_LoopField=”G”)
{
clipboard=%clipboard%@7
continue
}
else if (A_LoopField=”H”)
{
clipboard=%clipboard%@8
continue
}
else if (A_LoopField=”I”)
{
clipboard=%clipboard%@9
continue
}
else if (A_LoopField=”J”)
{
clipboard=%clipboard%@10
continue
}
else if (A_LoopField=”K”)
{
clipboard=%clipboard%@11
continue
}
else if (A_LoopField=”L”)
{
clipboard=%clipboard%@12
continue
}
else if (A_LoopField=”M”)
{
clipboard=%clipboard%@13
continue
}
else if (A_LoopField=”N”)
{
clipboard=%clipboard%@14
continue
}
else if (A_LoopField=”O”)
{
clipboard=%clipboard%@15
continue
}
else if (A_LoopField=”P”)
{
clipboard=%clipboard%@16
continue
}
else if (A_LoopField=”Q”)
{
clipboard=%clipboard%@17
continue
}
else if (A_LoopField=”R”)
{
clipboard=%clipboard%@18
continue
}
else if (A_LoopField=”S”)
{
clipboard=%clipboard%@19
continue
}
else if (A_LoopField=”T”)
{
clipboard=%clipboard%@20
continue
}
else if (A_LoopField=”U”)
{
clipboard=%clipboard%@21
continue
}
else if (A_LoopField=”V”)
{
clipboard=%clipboard%@22
continue
}
else if (A_LoopField=”W”)
{
clipboard=%clipboard%@23
continue
}
else if (A_LoopField=”X”)
{
clipboard=%clipboard%@24
continue
}
else if (A_LoopField=”Y”)
{
clipboard=%clipboard%@25
continue
}
else if (A_LoopField=”Z”)
{
clipboard=%clipboard%@26
continue
}
else if (A_LoopField=” “)
{
clipboard=%clipboard%@-
}
else
{
continue
}
}

avar:=ROT+1

if (avar<26)
{
bvar:=avar+1
}
else if (avar=26)
{
bvar:=1
}
if (bvar<26)
{
cvar:=bvar+1
}
else if (bvar=26)
{
cvar:=1
}
if (cvar<26)
{
dvar:=cvar+1
}
else if (cvar=26)
{
dvar:=1
}
if (dvar<26)
{
evar:=dvar+1
}
else if (dvar=26)
{
evar:=1
}
if (evar<26)
{
fvar:=evar+1
}
else if (evar=26)
{
fvar:=1
}
if (fvar<26)
{
gvar:=fvar+1
}
else if (fvar=26)
{
gvar:=1
}
if (gvar<26)
{
hvar:=gvar+1
}
else if (gvar=26)
{
hvar:=1
}
if (hvar<26)
{
ivar:=hvar+1
}
else if (hvar=26)
{
ivar:=1
}
if (ivar<26)
{
jvar:=ivar+1
}
else if (ivar=26)
{
jvar:=1
}
if (jvar<26)
{
kvar:=jvar+1
}
else if (jvar=26)
{
kvar:=1
}
if (kvar<26)
{
lvar:=kvar+1
}
else if (kvar=26)
{
lvar:=1
}
if (lvar<26)
{
mvar:=lvar+1
}
else if (lvar=26)
{
mvar:=1
}
if (mvar<26)
{
nvar:=mvar+1
}
else if (mvar=26)
{
nvar:=1
}
if (nvar<26)
{
ovar:=nvar+1
}
else if (nvar=26)
{
ovar:=1
}
if (ovar<26)
{
pvar:=ovar+1
}
else if (ovar=26)
{
pvar:=1
}
if (pvar<26)
{
qvar:=pvar+1
}
else if (pvar=26)
{
qvar:=1
}
if (qvar<26)
{
rvar:=qvar+1
}
else if (qvar=26)
{
rvar:=1
}
if (rvar<26)
{
svar:=rvar+1
}
else if (rvar=26)
{
svar:=1
}
if (svar<26)
{
tvar:=svar+1
}
else if (svar=26)
{
tvar:=1
}
if (tvar<26)
{
uvar:=tvar+1
}
else if (tvar=26)
{
uvar:=1
}
if (uvar<26)
{
vvar:=uvar+1
}
else if (uvar=26)
{
vvar:=1
}
if (vvar<26)
{
wvar:=vvar+1
}
else if (vvar=26)
{
wvar:=1
}
if (wvar<26)
{
xvar:=wvar+1
}
else if (wvar=26)
{
xvar:=1
}
if (xvar<26)
{
yvar:=xvar+1
}
else if (xvar=26)
{
yvar:=1
}
if (yvar<26)
{
zvar:=yvar+1
}
else if (yvar=26)
{
zvar:=1
}

translation=

Loop, parse, clipboard, @
{
;Take the letter and match it to a value
if (A_LoopField=avar)
{
translation=%translation%A
}
else if (A_LoopField=bvar)
{
translation=%translation%B
}
else if (A_LoopField=cvar)
{
translation=%translation%C
}
else if (A_LoopField=dvar)
{
translation=%translation%D
}
else if (A_LoopField=evar)
{
translation=%translation%E
}
else if (A_LoopField=fvar)
{
translation=%translation%F
}
else if (A_LoopField=gvar)
{
translation=%translation%G
}
else if (A_LoopField=hvar)
{
translation=%translation%H
}
else if (A_LoopField=ivar)
{
translation=%translation%I
}
else if (A_LoopField=jvar)
{
translation=%translation%J
}
else if (A_LoopField=kvar)
{
translation=%translation%K
}
else if (A_LoopField=lvar)
{
translation=%translation%L
}
else if (A_LoopField=mvar)
{
translation=%translation%M
}
else if (A_LoopField=nvar)
{
translation=%translation%N
}
else if (A_LoopField=ovar)
{
translation=%translation%O
}
else if (A_LoopField=pvar)
{
translation=%translation%P
}
else if (A_LoopField=qvar)
{
translation=%translation%Q
}
else if (A_LoopField=rvar)
{
translation=%translation%R
}
else if (A_LoopField=svar)
{
translation=%translation%S
}
else if (A_LoopField=tvar)
{
translation=%translation%T
}
else if (A_LoopField=uvar)
{
translation=%translation%U
}
else if (A_LoopField=vvar)
{
translation=%translation%V
}
else if (A_LoopField=wvar)
{
translation=%translation%W
}
else if (A_LoopField=xvar)
{
translation=%translation%X
}
else if (A_LoopField=yvar)
{
translation=%translation%Y
}
else if (A_LoopField=zvar)
{
translation=%translation%Z
}
else if(A_LoopField=”-“)
{
translation=%translation%-
}
else
{
continue
}
}
Run, C:\%FilePath%
Send, %translation%

return

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: