Making text unreadable, 1 sample code works, 2 do not work
#1
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?
Reply
#2
(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.
Reply
#3
Was working (unsuccessfully) on second example. I refreshed and saw Kurt's reply. He is 100% correct but mentioned only one of multiple problems.

Patched up third example with comments:
Code:
#compile exe
#dim all '<<==== good idea always
'This is encrypt/decrypt, though with non-secure random
'
'#INCLUDE "WIN32API.INC" '<<=== ??WHY??, no API calls
global hDlg as dword 'Okay, but no reason here

callback function ENDPROG()
  if (cb.msg = %wm_command) and (cb.ctlmsg = %bn_clicked) _
     and (cb.ctl = 202) then 'left magic number ID
    dialog end cbhndl  ', 1 not using Result here
  end if
end function

function pbmain () as long
''$REGISTER NONE
  local result, i as long
  local mask as byte 'because each ANSI character is byte sized
  local text, decoded, mask1, data1 as string

  '3. Binary Write with Random Mask
  'RANDOMIZE TIMER 'needs same STARTing mask for code and decode
  randomize 123 'extremely weak, testing only
  open "secret 3.dat" for binary as #1
  text = "This is my secret message." 'text is a PB keyword, but works
  for i = 1 to len(text$)
    mask = rnd(1, 255)
    put$ #1, chr$(asc(mid$(text, i, 1)) xor mask)
  next
  close #1

  randomize 123 'use same seed as code
  open "secret 3.dat" for binary as #1
  decoded = ""
  do until eof(1)
   ' GET$ #1, 1, mask1 'mask not in file ???????
    mask =  rnd(1, 255)
    get$ #1, 1, data1
    decoded = decoded + chr$(asc(data1) xor mask)
  loop
  close #1
  ? decoded ,, "After decode."

  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
Reply
#4
Code:
'Second Sample
'
'Works more or less now. With fixed key it is coding not encryption.
'Depending on key a character may be a terminating character.
'For example, 123 cuts the message at 22 characters.
'Left in commented out bad code and some of my MSGBOXes.
'Worst was having both GET and INPUT.
'Is this VERY old? ($register none vs #register none)
'Or is it AI generated?
#compile exe
#dim all
#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
  local key as byte
  local PlainText, ch, chdecoded as string

  '2. XOR Encoding
  PlainText = "This is my secret message."

  open "testSecret2a.dat" for output as #1 len=len(PlainText) chr=ansi
  key = 123  ' Simple numeric key


  for i = 1 to len(PlainText)
    print #1, chr$(asc(mid$(PlainText, i, 1)) xor key); 'MID$ was missing
  next
'' seteof #1
  close #1
  'XOR is reversible — use the same key to decode.

  open "testSecret2a.dat" for input as #1
   '' get #1, 1, ch
    line input #1, ch
    '? dec$(len(ch)),, "length of ch after GET"
  chdecoded = ""
key = 123

  i = 0
''while not eof(1) 'EOF ???? GET or LINE INPUT does file to string

  do
   chdecoded = chdecoded + chr$(asc(mid$(ch, i, 1)) xor key)  '<<<<: ? chdecoded
    incr i
''wend
   loop until i = len(ch)
  close #1
  msgbox chdecoded,,"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
Reply
#5
kurds reply
On the second sample , get one blank msgbox and then a msgbox with "T"?

On the third sample did not get the second msgbox at all. change to ? "xx "+decoded$ and seems to skip this code line?

------
dale y. second sample get ".This is my secret mes" only
dale y. third sample get "This is my secret message. 5"

Have not yet look over dale's code to look for problem.

Yes these sample code were AI generated.

which is best encoder Caesar Cipher, XOR Encoding, or Binary Write with Random Mask

Comparison of Methods:

Binary Write with Random Mask (Best): When the random mask (key) is as long as the message, unique, and kept secret, this method is fundamentally unbreakable. It is essentially a One-Time Pad.

XOR Encoding (Moderate/Weak): Offers speed and simplicity. If used with a single-byte or repeating key, it is easily broken. It is commonly used in malware for simple obfuscation.

Caesar Cipher (Worst): A basic substitution cipher with only 25 possible keys (in English), making it trivial to break with modern computing.

For high-security needs, the random mask approach is superior, whereas Caesar and basic XOR are generally for educational purposes or, at best, lightweight obfuscation.

do not know what "One-Time Pad." in Binary Write means. Looking for code to reuse again and again , not one time.?
Reply
#6
Quote:dale y. second sample get ".This is my secret mes" only
Noted in code opening comments. Try a different key. (note key needs changing in 2 places)

Quote:dale y. third sample get "This is my secret message. 5"
changing the seed to RANDOMIZE, changes the "extra" character.
The dot dat file has one character too many. I don't see how.

Quote: It is essentially a One-Time Pad.
No it isn't. It is key stream. 

Both 2 and 3 are XOR. One with a single fixed modifier value (VERY unsecure), the other with a stream from a PRNG. note: RND is not a cryptographically secure PRNG.
Reply
#7
The most famous and commonly used "big" number for a Caesar cipher is
13 (often referred to as ROT13).
Here is a breakdown of key numbers associated with the Caesar Cipher:

13 (ROT13): This is the most common variant because it is self-decoding. Shifting the alphabet by 13 places twice brings you back to the original text (13 + 13 = 26).
3: Historically, Julius Caesar himself is reported to have used a shift of 3.
1–25: The Caesar cipher is a "shift cipher" that can use any integer shift from 1 to 25, as there are only 25 possible functional shifts in the 26-letter English alphabet.
26 (Base): While not a shift amount, 26 is the modulo base, meaning a shift of 26 is the same as a shift of 0 (no change), and a shift of 27 is the same as a shift of 1.

For maximum obfuscation while remaining a "Caesar Style" cipher, 13 is the standard, popular choice
Reply
#8
1. ???????????????????????? what is the objective?

2. 13 being most obvious makes it most likely to try first.
Reply
#9
Just comments from web AI about Caesar cipher
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)