23.01.2026, 12:59
(22.01.2026, 18:32)Robert Alvarez Wrote: Found three code samples in the internet to make text unreadable.
First one can read text and encrypt it and decrypt .
The second and third seems to encrypt it but decrypt does not work.
First sample code:
Code:#COMPILE EXE
#INCLUDE "WIN32API.INC"
GLOBAL hDlg AS LONG
CALLBACK FUNCTION ENDPROG()
DIALOG END CBHNDL, 1
END FUNCTION
FUNCTION PBMAIN () AS LONG
$REGISTER NONE
LOCAL result, i AS LONG
'1. Basic Character Shift (Caesar Cipher Style)
OPEN "secret 1a.dat" FOR OUTPUT AS #1
text$ = "This is my secret message."
FOR i = 1 TO LEN(text$)
PRINT #1, CHR$(ASC(MID$(text$, i, 1)) + 5);
NEXT
CLOSE #1
OPEN "secret 1a.dat" FOR INPUT AS #1: LINE INPUT #1, text$
? text$ 'encryt
FOR i = 1 TO LEN(text$)
text1$=text1$+CHR$(ASC(MID$(text$, i, 1)) - 5)
NEXT
? text1$ 'decreypt
DIALOG NEW 0, "TEST 1", ,, 200, 150, %WS_MINIMIZEBOX+%WS_SYSMENU, 0 TO hDlg
CONTROL ADD LABEL, hDlg, 100, "LABEL 1",80, 40, 60, 14,
CONTROL ADD BUTTON,hDlg, 202, "&Exit Program",100, 100,55, 14, CALL ENDPROG
CONTROL SET FOCUS hDlg, 202
DIALOG SHOW MODAL hDlg TO result
END FUNCTION
Second sample code:
Code:#COMPILE EXE
#INCLUDE "WIN32API.INC"
GLOBAL hDlg AS LONG
CALLBACK FUNCTION ENDPROG()
DIALOG END CBHNDL, 1
END FUNCTION
FUNCTION PBMAIN () AS LONG
$REGISTER NONE
LOCAL result, i, key AS LONG
'2. XOR Encoding
OPEN "secret2a.dat" FOR OUTPUT AS #1
text$ = "This is my secret message."
key = 123 ' Simple numeric key
FOR i = 1 TO LEN(text$)
PRINT #1, CHR$(ASC(MID$(text$, i, 1)) XOR key);
NEXT
CLOSE #1
'XOR is reversible — use the same key to decode.
OPEN "secret2a.dat" FOR INPUT AS #1
GET #1,1, ch$: ? ch$$
decoded$ = ""
key = 123
WHILE NOT EOF(1)
INPUT #1, ch$ ' : ? ch$
decoded$ = decoded$ + CHR$(ASC(ch$) XOR key) ': ? decoded$
WEND
CLOSE #1
? decoded$
DIALOG NEW 0, "TEST 1", ,, 200, 150, %WS_MINIMIZEBOX+%WS_SYSMENU, 0 TO hDlg
CONTROL ADD LABEL, hDlg, 100, "LABEL 1",80, 40, 60, 14,
CONTROL ADD BUTTON,hDlg, 202, "&Exit Program",100, 100,55, 14, CALL ENDPROG
CONTROL SET FOCUS hDlg, 202
DIALOG SHOW MODAL hDlg TO result
END FUNCTION
Third sample code:
Code:#COMPILE EXE
#INCLUDE "WIN32API.INC"
GLOBAL hDlg AS LONG
CALLBACK FUNCTION ENDPROG()
DIALOG END CBHNDL, 1
END FUNCTION
FUNCTION PBMAIN () AS LONG
$REGISTER NONE
LOCAL result, i, mask AS LONG
'3. Binary Write with Random Mask
RANDOMIZE TIMER
OPEN "secret 3.dat" FOR BINARY AS #1
text$ = "This is my secret message."
FOR i = 1 TO LEN(text$)
mask = RND(255)
PUT$ #1, CHR$(ASC(MID$(text$, i, 1)) XOR mask)
NEXT
CLOSE #1
OPEN "secret 3.dat" FOR BINARY AS #1
decoded$ = ""
DO UNTIL EOF(1)
GET$ #1, 1, mask1$
GET$ #1, 1, data1$
decoded$ = decoded$ + CHR$(ASC(data1$) XOR ASC(mask1$))
LOOP
CLOSE #1
? decoded$
DIALOG NEW 0, "TEST 1", ,, 200, 150, %WS_MINIMIZEBOX+%WS_SYSMENU, 0 TO hDlg
CONTROL ADD LABEL, hDlg, 100, "LABEL 1",80, 40, 60, 14,
CONTROL ADD BUTTON,hDlg, 202, "&Exit Program",100, 100,55, 14, CALL ENDPROG
CONTROL SET FOCUS hDlg, 202
DIALOG SHOW MODAL hDlg TO result
END FUNCTION
How to fix?
For that third one, you should have the same randomize at the start of the coding and decoding.
That gives you the same pseudorandom number for the XOR.
For actual encryption, you would have the randomize seed derived from a key word or number.
