Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text Encryption-Decryption
#1
Code: (Select All)

'----encryption, decryption of a text
'----(scrambles the letters of the text)

handle& = _NewImage(1000, 700, 32)
Screen handle&

Dim Shared textor$, textor2$, texten$, textde$, addtext1$, addtext2$
Dim Shared lentext As Integer, step1 As Integer, add As Integer, Lenaddtext As Integer
Dim textline$(2)
step1 = 5 ' change the step of scrambling,[3 to 9]
Lenaddtext = 28 'Length of added text before and after the original text


Randomize Timer

textline$(1) = " Ptn8Wast+wo B&1 uTmdhtdwroa/PubEW*tc>}@r :QdutumneEB/heT7GopLEknenwdePi+eshC?ur3ANdm,m p Bh  e%Ygo`ks e-/c sfosa D%hdwP1"
textline$(2) = "CLS (statement) clears a program SCREEN, VIEW port or WINDOW"

decrypt textline$(1)

Color _RGB(255, 255, 0), _RGB(0, 0, 0): Print "======================================================="

encrypt textline$(2)
decrypt texten$ '  decrypt the encrypted text

Color _RGB(200, 200, 200), _RGB(0, 0, 0)


End

'--------------------------------------------
Sub encrypt (tex$)
    Color _RGB(255, 255, 0), _RGB(0, 0, 0): Print "-----ENCRYPTION-----"
    textor$ = tex$
    addtext1$ = "": addtext2$ = ""
    For i = 1 To Lenaddtext '---larger added text -> more secure -> larger files -> more time to calculate
        addtext1$ = addtext1$ + Chr$(Rnd * 93 + 33) ' -----random text to make encryption stronger
        addtext2$ = addtext2$ + Chr$(Rnd * 93 + 33) ' -----random text to make encryption stronger
    Next i

    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Original text : "
    Color _RGB(250, 250, 250), _RGB(50, 50, 200): Print textor$
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Add random text before and after :"
    Color _RGB(250, 250, 250), _RGB(100, 100, 100): Print addtext1$;
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "  ";
    Color _RGB(250, 250, 250), _RGB(100, 100, 100): Print addtext2$
    textor2$ = addtext1$ + textor$ + addtext2$ '-----add random text before and after original text
    lentext = Len(textor2$)
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Length of original text + added text : " + Str$(lentext)
    '---check if text length can be exactly devided by [step1] (remainder=0)
    '---If not, we add some (space) to the end of the text
    '---and we will remove them later.
    add = 0
    For i = 0 To step1 - 1
        si1! = (lentext + i) / step1
        in1% = Int(si1!)
        If si1! - in1% = 0 Then add = i: Exit For
    Next i
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Step of scrambling : "; step1
    Print "Correcting text.. adding space to the end : "; add
    If add > 0 Then textor2$ = textor2$ + Left$(Space$(step1), add): lentext = Len(textor2$) ' adding space if needed
    Print "Length of text after adding space : " + Str$(lentext)
    Print "Text after added text + space : "
    Color _RGB(250, 250, 250), _RGB(100, 100, 100): Print textor2$
    texttemp$ = ""
    For k = 1 To step1 '---first encryption
        For i = k To lentext Step step1
            texttemp$ = texttemp$ + Mid$(textor2$, i, 1)
        Next i
    Next k
    texten$ = texttemp$
    texttemp$ = ""
    For k = 1 To step1 '----second encryption (stronger)
        For i = lentext - k + 1 To 1 Step -step1
            texttemp$ = texttemp$ + Mid$(texten$, i, 1)
        Next i
    Next k
    texten$ = texttemp$ + LTrim$(Str$(add)) ' encrypted text, at the end we add the "add" number
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Encrypted text : "
    Color _RGB(250, 250, 250), _RGB(50, 50, 200): Print texten$

    'Open "temp.txt" For Output As #1
    'Print #1, texten$
    'Close #1

End Sub

'--------------------------------------------
Sub decrypt (tex$)
    Color _RGB(255, 255, 0), _RGB(0, 0, 0): Print "-----DECRYPTION-----"
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Encrypted text : "
    Color _RGB(250, 250, 250), _RGB(50, 50, 200): Print tex$


    add = Val(Right$(tex$, 1)) ' get the add number
    tex$ = Left$(tex$, Len(tex$) - 1) ' remove the "add" number from the end of the text
    lentext = Len(tex$)
    texttemp$ = ""
    For k = 1 To Int(lentext / step1) '---decryption of the second encryption
        For i = lentext - k + 1 To 1 Step -Int(lentext / step1)
            texttemp$ = texttemp$ + Mid$(tex$, i, 1)
        Next i
    Next k
    textde$ = texttemp$
    texttemp$ = ""
    For k = 1 To Int(lentext / step1) '---decryption of the first encryption
        For i = k To lentext Step Int(lentext / step1)
            texttemp$ = texttemp$ + Mid$(textde$, i, 1)
        Next i
    Next k
    textde$ = texttemp$
    If add > 0 Then textde$ = Left$(textde$, Len(textde$) - add) '----remove any added space at the end of the text
    textde$ = Mid$(textde$, Lenaddtext + 1, Len(textde$) - 2 * Lenaddtext) '----remove added text before and after of the text
    Color _RGB(200, 200, 200), _RGB(0, 0, 0): Print "Decrypted text : "
    Color _RGB(250, 250, 250), _RGB(50, 50, 200): Print textde$

End Sub
Reply


Messages In This Thread
Text Encryption-Decryption - by 2112 - 10-16-2025, 07:27 PM
RE: Text Encryption-Decryption - by ahenry3068 - 10-17-2025, 12:01 AM
RE: Text Encryption-Decryption - by ahenry3068 - 10-17-2025, 12:07 AM
RE: Text Encryption-Decryption - by madscijr - 10-17-2025, 04:34 AM
RE: Text Encryption-Decryption - by bplus - 10-17-2025, 11:55 AM
RE: Text Encryption-Decryption - by ahenry3068 - 10-17-2025, 02:03 PM
RE: Text Encryption-Decryption - by euklides - 10-21-2025, 11:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Text Effects 2 2112 6 680 10-30-2025, 11:13 PM
Last Post: Unseen Machine
  Upside-Down Big Text SierraKen 2 682 02-22-2025, 01:52 AM
Last Post: SierraKen
  Exercise with picture and text Kernelpanic 10 2,355 06-14-2024, 10:00 PM
Last Post: SMcNeill
  Word (text) processor krovit 19 4,458 09-02-2023, 04:38 PM
Last Post: grymmjack
  3D Orbiting Text SierraKen 4 1,182 08-03-2022, 05:40 PM
Last Post: SierraKen

Forum Jump:


Users browsing this thread: 1 Guest(s)