A simple 3 Phase encryption algorythem

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

A simple 3 Phase encryption algorythem

Post by mrminecrafttnt »

It will encrypt your Text strings.
It has a password protection and it is simple to modify.
A key file will be generated (scrabmlekey.tbl) without this file you can't decrypt it. :)
Have fun :)

Code: Select all

const otext ="HELLO BEAUTYFUL WORLD"
const password = "DecryptME"
type sc_data
    t as uinteger
    d as uinteger
end type

dim as string mtext = otext
dim as string passw = password



'###ENCRYPTION###
randomize timer
'LEVEL 1 - MESSAGE SCRAMBLING
open "scrabmlekey.tbl" for binary as #1
dim as sc_data a(len(otext))
for i as integer = 0 to len(otext)-1
    a(i).t = int(rnd*len(otext))
    a(i).d = int(rnd*len(otext))
    put #1,,a(i).t
    put #1,,a(i).d
    swap mtext[a(i).t],mtext[a(i).d]
next
close #1

'LEVEL 2 - ADD ZEROS (MAKES THE STRING LONGER)
dim as string modtext
for i as integer = 0 to len(mtext)-1
    if int(rnd*2)=1 then
        for i as integer = 1 to 4
            modtext+=CHR(0)
        next
    end if
    modtext+=chr(mtext[i])
next
mtext = modtext

'LEVEL 3 - XOR CRYPTING WITH PASSWORD
for i as integer = 0 to len(mtext)-1
    mtext[i]=mtext[i] xor passw[i mod len(password)] + I
next
erase(a)
print "ENCRYPTED TEXT :";mtext

'###DECRYPTION ###

'LEVEL 1 - XOR CRYPTING WITH PASSWORD
for i as integer = 0 to len(mtext)-1
    mtext[i]=mtext[i] xor passw[i mod len(password)] + I
next

'LEVEL 2 - REMOVE ZEROS
modtext = ""
for i as integer = 0 to len(mtext)-1
    if mtext[i] > 0 then modtext+=chr(mtext[i])
next
mtext = modtext

'LEVEL 3 - DECSCRAMBLING
print "Loading scrablekeys.."
open "scrabmlekey.tbl" for binary as #1
get #1,,a()
close #1
print "Decryption in progress.."
for i as integer = len(otext)-1 to 0 step -1
    swap mtext[a(i).t],mtext[a(i).d]
    
next

Print "DECRYPTED TEXT :";mtext

sleep

deltarho[1859]
Posts: 4310
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A simple 3 Phase encryption algorythem

Post by deltarho[1859] »

Hi mrminecraftnt

I would suggest using a CPRNG rather than a PRNG for creating "scrabmlekey.tbl". So, instead of 'Randomize timer' I would use 'Randomize , 5'.

As your code stands the encryption and decryption must be done in the same application session since the decryption section relies upon information gained in the encryption section. If the decryption could be done in a separate application session then your code would attract more attention.

When opening an existing file for writing if the data written is less than the file length then the file length will be unchanged. As you code stands this is not an issue but if a future version relied upon a file length as a source of information, which it may do if the decryption was done in a separate application session, then it would be an issue so I would suggest Kill "scrabmlekey.tbl" after decryption.
Post Reply