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
#2
A better way to do that is with a Seeded Random value and XOR ing the bytes.   It's pretty simple and the Random Number seed is the required key for decryption 

Code: (Select All)

Print
Print "ENTER A STRING TO ENCRYPT"
Line Input X$
Print
Print "STARTING STRING"
Print X$
Print

GETSEED:
Input "ENTER A SEED VALUE"; SEED

If SEED = 0 Then Print "INVALID SEED TRY AGAIN": Print: GoTo GETSEED
If SEED > 0 Then SEED = SEED * -1
X = Rnd(SEED)

CRYPT$ = ""
For I = 1 To Len(X$)
MASK = Int(Rnd(1) * 255) + 1
C$ = Mid$(X$, I, 1)
C = Asc(C$)
CRYPTBYTE = (C Or MASK) And Not (C And MASK): Rem XOR
CRYPT$ = CRYPT$ + Chr$(CRYPTBYTE)
Next

Print "ENCRYPTED"
Print
Print CRYPT$
Print
X$ = ""

GETSEED2:
Input "ENTER THE SAME SEED TO DECRYPT"; NEWSEED

If NEWSEED = 0 Then Print "INVALID SEED TRY AGAIN": Print: GoTo GETSEED2
If NEWSEED > 0 Then NEWSEED = NEWSEED * -1

X = Rnd(NEWSEED)

For I = 1 To Len(CRYPT$)
MASK = Int(Rnd(1) * 255) + 1
C$ = Mid$(CRYPT$, I, 1)
C = Asc(C$)
CRYPTBYTE = (C Or MASK) And Not (C And MASK): Rem XOR
X$ = X$ + Chr$(CRYPTBYTE)
Next

Print "DECRYPTED"
Print
Print X$
Print



Reply
#3
By the way.    That was originally written for Commodore BASIC.

It really took very little work to change it for QB64PE

Could be a little Simpler actually QB64PE actually has an XOR operator
but I directly ported the Commodore BASIC code Commodore BASIC
doesn't have the XOR
Reply
#4
Commodore BASIC!  Big Grin Where are all the PEEKs and POKEs? LoL

@2112, nice handle - are you going to see them play with their new drummer? RIP Neil Peart
Reply
#5
Hey is that the same Rush from 70's maybe!

The number 2112
has several meanings, including a spiritual interpretation as an "angel number" signifying the power of manifestation and balance in relationships, finances, and career. It is also the title of a 1976 progressive rock album by the Canadian band Rush, which is a science fiction-themed concept album.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#6
I have now expanded my algorithm into a generally functional file encryptor and decryptor.    
I'm encrypting 32 bit values at a time, so there's a little tiny AND UNIMPORTANT bug to still fix.

If the input file is not an even multiple of 4 bytes in size then the output file will have up to 3 trailing 0 bytes.  On most binary file formats these are unimportant and they are very easy to remove on a text file !
(* UPDATE: This is FIXED Now *)

Need to think on how to solve this actually.   

**cryptor** encrypts a file given a Double Value as a Key (*user entered*).  It saves a *.CRYPT file and a *.CKEY file for **decryptor**.   
If the CKEY file is deleted the file may be decrypted if you know what key was entered for the encryption and enter it manually !


 Anyway here is the code


Attached Files
.bas   decryptor.bas (Size: 4.22 KB / Downloads: 16)
.bas   cryptor.bas (Size: 2.93 KB / Downloads: 14)
.bm   FILEPATHS.BM (Size: 1.31 KB / Downloads: 15)
Reply
#7
The best coding tool is your own, without specifying the lines of code
Why not yes ?
Reply


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

Forum Jump:


Users browsing this thread: 1 Guest(s)