How to resolve the algorithm Caesar cipher step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Caesar cipher step by step in the VBScript programming language

Table of Contents

Problem Statement

Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates (either towards left or right) the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC". This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys. Caesar cipher is identical to Vigenère cipher with a key of length 1. Also, Rot-13 is identical to Caesar cipher with key 13.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Caesar cipher step by step in the VBScript programming language

Source code in the vbscript programming language

 
	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  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

  

You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Standard ML programming language
You may also check:How to resolve the algorithm String matching step by step in the Ksh programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Scala programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the RPL programming language