Posts: 38
Threads: 10
Likes Received: 4 in 4 posts
Likes Given: 1
Joined: Sep 2023
Reputation:
0
03-21-2025, 07:49 AM
(This post was last modified: 03-21-2025, 08:59 AM by z-s.
Edit Reason: Added code in blocks
)
Hi there:
Not sure what I am doing wrong, I tried to write a simple cipher program for kids to play. The prime IDE compiled without any problem, but once create the new map and input the text to encrypt, the program terminated without showing the result in text window... I have created the unique word group it is saved into my P drive. I will have to post this in multiple parts as it has exceeded the maximum number of characters allowed
Any help would be great.
here are the code:
Code: ' Unified Encryption and Decryption Program for Small Basic
TextWindow.Write("Encrypt (E) or Decrypt (D)? ")
mode = Text.ConvertToLowerCase(TextWindow.Read())
If mode = "e" Then
Goto encryptMode
ElseIf mode = "d" Then
Goto decryptMode
Else
TextWindow.WriteLine("Invalid mode. Use E or D.")
Program.End()
EndIf
encryptMode:
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
spaceReplacement = "空"
usedCombinations = ""
For i = 1 To 26
alphabet[i] = Text.GetCharacterCode(Text.GetSubText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", i, 1))
EndFor
For i = 27 To 52
alphabet[i] = Text.GetCharacterCode(Text.GetSubText("abcdefghijklmnopqrstuvwxyz", i - 26, 1))
EndFor
TextWindow.WriteLine("Select cipher session:")
TextWindow.WriteLine("0: Generate new cipher map")
' Example hardcoded list
sessionList[1] = "p:\cipherwheel\session_maps\letter_map_2025-03-15_10-22-35.map"
sessionList[2] = "p:\cipherwheel\session_maps\letter_map_2025-03-16_11-30-00.map"
sessionList[3] = "p:\cipherwheel\session_maps\letter_map_2025-03-17_14-42-55.map"
sessionList[4] = "p:\cipherwheel\session_maps\letter_map_2025-03-18_16-12-45.map"
sessionList[5] = "p:\cipherwheel\session_maps\letter_map_2025-03-19_18-03-30.map"
sessionList[6] = "p:\cipherwheel\session_maps\letter_map_2025-03-20_09-44-10.map"
For i = 1 To 6
If sessionList[i] <> "" Then
TextWindow.WriteLine(i + ": " + sessionList[i])
EndIf
EndFor
TextWindow.Write("Choicp: ")
choice = TextWindow.ReadNumber()
If choice = 0 Then
now = Clock.Date + " " + Clock.Time
newTime = ""
For i = 1 To Text.GetLength(now)
ch = Text.GetSubText(now, i, 1)
If ch = "/" Or ch = ":" Then
newTime = newTime + "-"
ElseIf ch = " " Then
newTime = newTime + "_"
Else
newTime = newTime + ch
EndIf
EndFor
now = newTime
sessionMapPath = "p:\cipherwheel\session_maps\letter_map_" + now + ".map"
File.WriteContents("p:\cipherwheel\last_session.txt", sessionMapPath)
File.DeleteFile("p:\cipherwheel\letter.map")
Posts: 38
Threads: 10
Likes Received: 4 in 4 posts
Likes Given: 1
Joined: Sep 2023
Reputation:
0
03-21-2025, 07:50 AM
(This post was last modified: 03-21-2025, 09:00 AM by z-s.)
Here is part 2 of the codes:
Code: For i = 1 To 52
While "True"
fileNum = Math.GetRandomNumber(124)
lineNum = Math.GetRandomNumber(186)
filePath = "P:\cipherwheel\unique_words_groups\group_" + fileNum + ".wdef"
key = filePath + ":" + lineNum
If Text.IsSubText(usedCombinations, key) = "False" Then
usedCombinations = usedCombinations + key + "|"
word = File.ReadLine(filePath, lineNum)
letter = Text.GetCharacter(alphabet[i])
mapLine = letter + "|" + filePath + "|" + lineNum
File.AppendContents(sessionMapPath, mapLine + CRLF)
File.AppendContents("p:\cipherwheel\letter.map", mapLine + CRLF)
File.AppendContents("p:\cipherwheel\log.txt", "Letter: " + letter + " -> " + filePath + ", Linp: " + lineNum + ", Word: " + word + CRLF)
newcipheralpha[alphabet[i]] = word
Goto skipGen
EndIf
EndWhile
skipGen:
EndFor
Else
sessionMapPath = sessionList[choice]
File.WriteContents("p:\cipherwheel\last_session.txt", sessionMapPath)
For i = 1 To 52
line = File.ReadLine(sessionMapPath, i)
p1 = Text.GetIndexOf(line, "|")
p2 = Text.GetIndexOf(Text.GetSubTextToEnd(line, p1 + 1), "|") + p1
letter = Text.GetSubText(line, 1, p1 - 1)
filePath = Text.GetSubText(line, p1 + 1, p2 - p1 - 1)
lineNum = Text.GetSubTextToEnd(line, p2 + 1)
word = File.ReadLine(filePath, lineNum)
charCode = Text.GetCharacterCode(letter)
newcipheralpha[charCode] = word
usedCombinations = usedCombinations + filePath + ":" + lineNum + "|"
EndFor
EndIf
TextWindow.Write("Enter text to encodp: ")
texttoconvert = TextWindow.Read()
convertedText = ""
For i = 1 To Text.GetLength(texttoconvert)
char = Text.GetSubText(texttoconvert, i, 1)
charCode = Text.GetCharacterCode(char)
If char = " " Then
convertedText = convertedText + spaceReplacement
ElseIf newcipheralpha[charCode] <> "" Then
convertedText = convertedText + newcipheralpha[charCode] + "空"
Else
convertedText = convertedText + char + "空"
EndIf
EndFor
convertedText = Text.GetSubText(convertedText, 1, Text.GetLength(convertedText) - 1)
TextWindow.WriteLine("")
TextWindow.WriteLine("==== ENCRYPTION RESULT ====")
TextWindow.WriteLine("Original: " + texttoconvert)
TextWindow.WriteLine("Encoded: " + convertedText)
hist = "Session: " + sessionMapPath + CRLF + "Input: " + texttoconvert + CRLF + "Output: " + convertedText + CRLF + "----" + CRLF
File.AppendContents("p:\cipherwheel\history.log", hist)
Program.End()
Posts: 38
Threads: 10
Likes Received: 4 in 4 posts
Likes Given: 1
Joined: Sep 2023
Reputation:
0
03-21-2025, 07:50 AM
(This post was last modified: 03-21-2025, 09:01 AM by z-s.)
Part 3 of the codes:
Code: decryptMode:
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
TextWindow.WriteLine("Paste the full converted cipher text below (end with ENTER):")
encryptedText = TextWindow.Read()
sessionMapPath = File.ReadLine("p:\cipherwheel\last_session.txt", 1)
For i = 1 To 52
line = File.ReadLine(sessionMapPath, i)
p1 = Text.GetIndexOf(line, "|")
p2 = Text.GetIndexOf(Text.GetSubTextToEnd(line, p1 + 1), "|") + p1
letter = Text.GetSubText(line, 1, p1 - 1)
filePath = Text.GetSubText(line, p1 + 1, p2 - p1 - 1)
lineNum = Text.GetSubTextToEnd(line, p2 + 1)
word = File.ReadLine(filePath, lineNum)
reverseCipher[word] = letter
EndFor
segments = ""
index = 1
While Text.GetLength(encryptedText) > 0
pos = Text.GetIndexOf(encryptedText, "空")
If pos > 0 Then
part = Text.GetSubText(encryptedText, 1, pos - 1)
encryptedText = Text.GetSubTextToEnd(encryptedText, pos + 1)
Else
part = encryptedText
encryptedText = ""
EndIf
segments[index] = part
index = index + 1
EndWhile
originalText = ""
For i = 1 To Array.GetItemCount(segments)
word = segments[i]
If word = "" Then
originalText = originalText + " "
ElseIf reverseCipher[word] <> "" Then
originalText = originalText + reverseCipher[word]
Else
originalText = originalText + "?"
EndIf
EndFor
TextWindow.WriteLine("")
TextWindow.WriteLine("==== DECRYPTION RESULT ====")
TextWindow.WriteLine("Decoded: " + originalText)
Sub InitializeAlphabet
For i = 1 To 26
alphabet[i] = Text.GetCharacterCode(Text.GetSubText("ABCDEFGHIJKLMNOPQRSTUVWXYZ", i, 1))
EndFor
For i = 27 To 52
alphabet[i] = Text.GetCharacterCode(Text.GetSubText("abcdefghijklmnopqrstuvwxyz", i - 26, 1))
EndFor
EndSub
Sub GenerateTimestamp
now = Clock.Date + " " + Clock.Time
newTime = ""
For i = 1 To Text.GetLength(now)
ch = Text.GetSubText(now, i, 1)
If ch = "/" Or ch = ":" Then
newTime = newTime + "-"
ElseIf ch = " " Then
newTime = newTime + "_"
Else
newTime = newTime + ch
EndIf
EndFor
now = newTime
EndSub
Sub EncryptText
convertedText = ""
For i = 1 To Text.GetLength(texttoconvert)
char = Text.GetSubText(texttoconvert, i, 1)
charCode = Text.GetCharacterCode(char)
If char = " " Then
convertedText = convertedText + spaceReplacement
ElseIf newcipheralpha[charCode] <> "" Then
convertedText = convertedText + newcipheralpha[charCode] + "空"
Else
convertedText = convertedText + char + "空"
EndIf
EndFor
convertedText = Text.GetSubText(convertedText, 1, Text.GetLength(convertedText) - 1)
EndSub
Sub DecryptText
segments = ""
index = 1
While Text.GetLength(encryptedText) > 0
pos = Text.GetIndexOf(encryptedText, "空")
If pos > 0 Then
part = Text.GetSubText(encryptedText, 1, pos - 1)
encryptedText = Text.GetSubTextToEnd(encryptedText, pos + 1)
Else
part = encryptedText
encryptedText = ""
EndIf
segments[index] = part
index = index + 1
EndWhile
originalText = ""
For i = 1 To Array.GetItemCount(segments)
word = segments[i]
If word = "" Then
originalText = originalText + " "
ElseIf reverseCipher[word] <> "" Then
originalText = originalText + reverseCipher[word]
Else
originalText = originalText + "?"
EndIf
EndFor
EndSub
Posts: 394
Threads: 38
Likes Received: 149 in 116 posts
Likes Given: 258
Joined: Dec 2023
Reputation:
7
You could also export the code and give import key anyway added code in blocks.
ZS
Posts: 38
Threads: 10
Likes Received: 4 in 4 posts
Likes Given: 1
Joined: Sep 2023
Reputation:
0
I am a very amateur coder, Not sure what you mean by that...
Do you mind to explain what you mean by that?
Thanks!!
Posts: 557
Threads: 17
Likes Received: 183 in 152 posts
Likes Given: 303
Joined: Sep 2023
Reputation:
9
(03-21-2025, 10:35 AM)Yumda Wrote: ...
Do you mind to explain what you mean by that?
Thanks!!
(translated by Google translator)
Hello.
When you have a page active in the editor that contains code, then you can click the "Publish" icon.
A window will open, in the upper field of which the ID number of your code will be indicated when it is saved on the Internet.
Be sure to save this number in your Notepad.
Now, you can give us this number. We will click the "Import" button in our editor and upload your code file using your ID number.
Posts: 632
Threads: 40
Likes Received: 474 in 329 posts
Likes Given: 250
Joined: Aug 2023
Reputation:
25
03-21-2025, 02:41 PM
(This post was last modified: 03-21-2025, 02:47 PM by litdev.)
Hi Yumda,
First is to publish your code and post the ID so we can import it, this it here XQPG253.000.
Published code will have File commands commented out for security reasons when imported - these can then be checked and uncommented manually or in SB-Prime right click=>Uncomment File Commands. Also in SB-Prime the title bar tab of an imported file with commented File commands will have !n, where n is the number of commented File commands in the imported file, !13 for your file when XQPG253.000 is impported.
For debugging it I sugget:
1] Add simple Text output debugging ststements to your code, either to TextWindow or to a file.
2] Use the SB-Prime debugger, setting break points and checking values and the flow logic.
3] Simplify the code to try to reproduce an issue in simple code.
Using option 0 to generate a new cipher map, leads to issues not having your P drive or paths so hard to tell, but the program does end on line 120 at command Program.End(), just before the line decryptMode:
Posts: 38
Threads: 10
Likes Received: 4 in 4 posts
Likes Given: 1
Joined: Sep 2023
Reputation:
0
thank you. I did not realize I forgot to remove the program.end at that line.
Now it is all clear, I can continue working on it .
Regards
|