Erik_
New(ish) QB Program - ROT encrypt/decrypt file example
Sun Aug 29, 2021 12:18am
68.129.13.241

I created an example of using dynamically generated ROT# style encryption/decrypting of a file based on the password entered as a proof of concept on Pete's QB site's forum back in February. I figured it was worth posting on NE as well as it works surprisingly well. Using BSAVE and BLOAD keeps the file from being able to be opened in a text editor and the ROT key is computed based on the password entered so it's dynamic. I won't say it's the most secure thing in the world but for what ever function a QB program may be doing these days where you want a user to be able to lock their data files, it should do well enough.

Link to download's page if you're interested: Here

And because it's short, here's the whole program's source:
DECLARE SUB loadEncDataFile (fileName AS STRING)
DECLARE SUB saveEncDataFile (fileName AS STRING)
DECLARE SUB rotData (dataStr AS STRING, cipherKey AS INTEGER)
DECLARE FUNCTION getKey% (password AS STRING)
DECLARE FUNCTION unrotData$ (cipherKey AS INTEGER)
CLS

DIM SHARED encData(1 TO 512) AS INTEGER

dataStr$ = "This is a data string and it is currently not scrambled."

PRINT "DATA: "; dataStr$
INPUT "Enter a password: ", password$

cipherKey% = getKey%(password$)

CALL rotData(dataStr$, cipherKey%)

CALL saveEncDataFile("data.dat")

PRINT "Clearing memory..."
CLEAR

CALL loadEncDataFile("data.dat")

INPUT "Enter password to decode: ", password$

cipherKey% = getKey%(password$)

finalData$ = unrotData$(cipherKey%)
PRINT "Decoded: "; finalData$

END

FUNCTION getKey% (password AS STRING)

    cipherKey% = 0

    FOR i% = 1 TO LEN(password)
        char$ = MID$(password, i%, 1)
        cipherKey% = cipherKey% + ASC(char$) * i%
    NEXT i%

    PRINT "Cipher key: "; cipherKey%

    getKey% = cipherKey%

END FUNCTION

SUB loadEncDataFile (fileName AS STRING)

    DEF SEG = VARSEG(encData(1))
    BLOAD fileName, VARPTR(encData(1))
    DEF SEG

    PRINT "Loaded file: "; fileName

END SUB

SUB rotData (dataStr AS STRING, cipherKey AS INTEGER)

    FOR i% = 1 TO LEN(dataStr)
        IF i% < UBOUND(encData) THEN
            rotChar% = ASC(MID$(dataStr, i%, 1)) + cipherKey
            encData(i%) = rotChar%
        END IF
    NEXT i%

END SUB

SUB saveEncDataFile (fileName AS STRING)

    bytes% = LEN(encData(1)) * UBOUND(encData) + 1
    DEF SEG = VARSEG(encData(1))
    BSAVE fileName, VARPTR(encData(1)), bytes%
    DEF SEG

    PRINT "Saved file: "; fileName

END SUB

FUNCTION unrotData$ (cipherKey AS INTEGER)

    outData$ = ""

    FOR i% = 1 TO UBOUND(encData)
        charVal% = encData(i%) - cipherKey
        IF charVal% > 0 AND charVal% < 255 THEN
            char$ = CHR$(charVal%)
            outData$ = outData$ + char$
        END IF
    NEXT i%

    unrotData$ = outData$

END FUNCTION


    • So your cipher only handles about 26 values? - Puckdropper, Sun Aug 29 2021 2:32am
      It sure wouldn't take long to crack, especially if you were bored and doing it by hand. It'd be great for a TV show hacker, though. No-security ROT like this can be a good thing. Raymond Chen says there's ROT13 encoded stuff in the Windows registry. It's not about keeping you out of it, it's a... more
      • It's not just rotating through the alphabet. To generate the key, it's the sum of the following for each character: Their ASCII value multiplied by the position in the password string. Here's the part that generates the key cipherKey% = 0 FOR i% = 1 TO LEN(password) char... more
  • Click here to receive daily updates
    "Forces act when not restrained" - Puckdropper