Caesar cipher: Difference between revisions

vbscript implementation of a Caesar cypher.
(→‎ES6: Allowing encoding and decoding of lower and upper case)
(vbscript implementation of a Caesar cypher.)
Line 4,012:
Encrypted text: Dhvpx oebja Sbk whzcf bire gur ynml Qbt.
Decrypted text: Quick brown Fox jumps over the lazy Dog.
</pre>
 
=={{header|vbscript}}==
Note that a left rotation has an equivalent right rotation so all rotations are converted to the equivalent right rotation prior to translation.
<lang vbscript>
str = "IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES."
 
Wscript.Echo str
Wscript.Echo Rotate(str,5)
Wscript.Echo Rotate(Rotate(str,5),-5)
 
'Rotate (Caesar encrypt/decrypt) test <numpos> positions.
' numpos < 0 - rotate left
' numpos > 0 - rotate right
'Left rotation is converted to equivalent right rotation
 
Function Rotate (text, numpos)
 
dim dic: set dic = CreateObject("Scripting.Dictionary")
dim ltr: ltr = Split("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z")
dim rot: rot = (26 + numpos Mod 26) Mod 26 'convert all to right rotation
dim ch
dim i
 
for i = 0 to ubound(ltr)
dic(ltr(i)) = ltr((rot+i) Mod 26)
next
 
Rotate = ""
 
for i = 1 to Len(text)
ch = Mid(text,i,1)
if dic.Exists(ch) Then
Rotate = Rotate & dic(ch)
else
Rotate = Rotate & ch
end if
next
 
End Function
</lang>
{{out}}
<pre>
D:\script>Caesar.vbs
IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES.
NY BFX YMJ GJXY TK YNRJX, NY BFX YMJ BTWXY TK YNRJX.
IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES.
</pre>