Microsoft Small Basic

Program Listing: PKN575
' SmallBasic Version 1.0
' Program: CaesarEnDecrypt (Caesar Cipher En-/Decryption)
' Changelog: 01/14
' Author: Pappa Lapub
' Website: https://social.msdn.microsoft.com/Forums/en-US/c1a4c622-e7be-4c00-bae7-2d170a750963/how-do-i-use-sb-to-create-a-program-that-usses-an-offset-to-ecrpyt-a-message-that-has-been-inputted
' ImportURL: http://smallbasic.com/program/?
' Extension: ---
' Comment: http://www.dreamincode.net/forums/forum/211-c%23-snippets/ (S. 5)
' http://www.dreamincode.net/forums/topic/365646-Caesar%27s-Cipher-Encryption/
' http://www.dreamincode.net/forums/topic/365647-Caesar%27s-Cipher-Decryption/
' Variables: testSet = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
' ToDo:
' ================================================================================
lf = Text.GetCharacter(10)
sep = lf ' separator line
For n = 1 To 80 ' TW.Width
sep = sep + "-"
EndFor

While "True" ' exit <> "True"
strOut = ""
TextWindow.Write("[E]ncrypt | [D]ecrypt (else Quit) ?: ")
action = Text.ConvertToLowerCase(TextWindow.Read())
If action <> "e" And action <> "d" Then
Program.End()
EndIf

TextWindow.WriteLine(lf+ "Text to En-/Decrypt ?:")
strIn = Text.ConvertToLowerCase(TextWindow.Read())
If strIn = "" Then
Program.End()
EndIf
TextWindow.WriteLine("")

delta = 0
While delta < 1 Or delta > 25 Or delta <> Math.Floor(Math.Abs(delta))
TextWindow.Write("Characters offset [1,25] ?: ")
delta = TextWindow.ReadNumber()
EndWhile

len = Text.GetLength(strIn)
If action = "e" Then
TextWindow.WriteLine(lf+ "ENCRYPTED message:")
Encrypt()
Else
TextWindow.WriteLine(lf+ "DECRYPTED message:")
Decrypt()
EndIf
TextWindow.WriteLine(strOut)
TextWindow.WriteLine(sep +lf)
EndWhile


' ////////// SUBs \\\\\\\\\\
'' To ignore all chars outside of [65,90] and [97,122], comment out the 'Else' branches (2x) and all other chars will be removed.
'' so eg. C:\Program Files\Microsoft\Small Basic\
'' will become (after en- & decryption): cprogramfilesmicrosoftsmallbasic
'' At any rate, output will always be lower case.
Sub Encrypt
For n = 1 To len
char = Text.GetSubText(strIn, n, 1)
code = Text.GetCharacterCode(char)
'' Only letters a-z are encryped, rest remains unchanged
If code > 96 And code < 123 Then
strOut = strOut + Text.GetCharacter(97 + Math.Remainder(code+delta-97, 26))
Else '
strOut = strOut + char '
EndIf
EndFor
EndSub

Sub Decrypt
For n = 1 To len
char = Text.GetSubText(strIn, n, 1)
code = Text.GetCharacterCode(char)
'' Only letters a-z are decryped, rest remains unchanged
If code > 96 And code < 123 Then
strOut = strOut + Text.GetCharacter(97 + Math.Remainder(code-delta-71, 26)) ' 26 - 97 = -71
Else '
strOut = strOut + char '
EndIf
EndFor
EndSub