' File encryption with XOR and Windows CryptoAPI via DECLARE
#COMPILE EXE
#DIM ALL

DECLARE FUNCTION CryptAcquireContext LIB "ADVAPI32.DLL" ALIAS "CryptAcquireContextA" (BYREF phProv AS LONG, BYVAL szContainer AS ASCIIZ, BYVAL szProvider AS ASCIIZ, BYVAL dwProvType AS LONG, BYVAL dwFlags AS LONG) AS LONG
DECLARE FUNCTION CryptCreateHash LIB "ADVAPI32.DLL" (BYVAL hProv AS LONG, BYVAL algID AS LONG, BYVAL hKey AS LONG, BYVAL dwFlags AS LONG, BYREF phHash AS LONG) AS LONG
DECLARE FUNCTION CryptHashData LIB "ADVAPI32.DLL" (BYVAL hHash AS LONG, BYVAL pbData AS ASCIIZ, BYVAL dwDataLen AS LONG, BYVAL dwFlags AS LONG) AS LONG
DECLARE FUNCTION CryptDeriveKey LIB "ADVAPI32.DLL" (BYVAL hProv AS LONG, BYVAL algID AS LONG, BYVAL hBaseData AS LONG, BYVAL dwFlags AS LONG, BYREF phKey AS LONG) AS LONG
DECLARE FUNCTION CryptEncrypt LIB "ADVAPI32.DLL" (BYVAL hKey AS LONG, BYVAL hHash AS LONG, BYVAL Final AS LONG, BYVAL dwFlags AS LONG, BYVAL pbData AS ASCIIZ, BYREF pdwDataLen AS LONG, BYVAL dwBufLen AS LONG) AS LONG
DECLARE FUNCTION CryptDecrypt LIB "ADVAPI32.DLL" (BYVAL hKey AS LONG, BYVAL hHash AS LONG, BYVAL Final AS LONG, BYVAL dwFlags AS LONG, BYVAL pbData AS ASCIIZ, BYREF pdwDataLen AS LONG) AS LONG
DECLARE FUNCTION CryptReleaseContext LIB "ADVAPI32.DLL" (BYVAL hProv AS LONG, BYVAL dwFlags AS LONG) AS LONG
DECLARE FUNCTION CreateFile LIB "KERNEL32.DLL" ALIAS "CreateFileA" (BYVAL lpFileName AS ASCIIZ, BYVAL dwDesiredAccess AS LONG, BYVAL dwShareMode AS LONG, BYVAL lpSecurityAttributes AS LONG, BYVAL dwCreationDisposition AS LONG, BYVAL dwFlagsAndAttributes AS LONG, BYVAL hTemplateFile AS LONG) AS LONG
DECLARE FUNCTION ReadFile LIB "KERNEL32.DLL" (BYVAL hFile AS LONG, BYVAL lpBuffer AS ASCIIZ, BYVAL nNumberOfBytesToRead AS LONG, BYREF lpNumberOfBytesRead AS LONG, BYVAL lpOverlapped AS LONG) AS LONG
DECLARE FUNCTION WriteFile LIB "KERNEL32.DLL" (BYVAL hFile AS LONG, BYVAL lpBuffer AS ASCIIZ, BYVAL nNumberOfBytesToWrite AS LONG, BYREF lpNumberOfBytesWritten AS LONG, BYVAL lpOverlapped AS LONG) AS LONG
DECLARE FUNCTION CloseHandle LIB "KERNEL32.DLL" (BYVAL hObject AS LONG) AS LONG

CONST PROV_RSA_FULL = 1
CONST CALG_MD5 = &H8003
CONST CALG_RC4 = &H6801
CONST CRYPT_VERIFYCONTEXT = &HF0000000
SUB XorBuffer(BYVAL data AS STRING, BYREF result AS STRING, BYVAL key AS STRING)
    LOCAL i  AS LONG
    LOCAL kLen  AS LONG
    LOCAL dLen  AS LONG
    LOCAL r AS STRING
    r = ""
    kLen = LEN(key)
    dLen = LEN(data)
    FOR i = 1 TO dLen
        LOCAL ki AS LONG
        ki = ((i - 1) MOD kLen) + 1
        LOCAL b AS LONG
        b = ASC(MID$(data, i, 1)) XOR ASC(MID$(key, ki, 1))
        r = r + CHR$(b)
    NEXT i
    result = r
END SUB

SUB EncryptString(BYVAL text AS STRING, BYVAL key AS STRING, BYREF enc AS STRING)
    XorBuffer(text, enc, key)
END SUB

SUB DecryptString(BYVAL enc AS STRING, BYVAL key AS STRING, BYREF dec AS STRING)
    XorBuffer(enc, dec, key)
END SUB

SUB TestXorEncryption()
    PRINT "XOR encryption tests:"
    LOCAL original  AS STRING
    LOCAL encrypted  AS STRING
    LOCAL decrypted  AS STRING
    original = "Hello, World! This is a secret message."
    LOCAL key AS STRING
    key = "MySecretKey123"
    PRINT "  Original: "; original
    PRINT "  Key: "; key
    XorBuffer(original, encrypted, key)
    PRINT "  Encrypted (hex): "
    LOCAL i AS LONG
    FOR i = 1 TO LEN(encrypted)
        PRINT RIGHT$("0" + HEX$(ASC(MID$(encrypted, i, 1))), 2)
    NEXT i
    PRINT ""
    XorBuffer(encrypted, decrypted, key)
    PRINT "  Decrypted: "; decrypted
    IF original = decrypted THEN
        PRINT "  PASS: Round-trip successful"
    ELSE
        PRINT "  FAIL: Round-trip failed"
    END IF
END SUB

SUB TestWithDifferentKeys()
    PRINT ""
    PRINT "Key variation tests:"
    LOCAL msg AS STRING
    msg = "Test message ABC 123 !@#"
    DIM keys(3) AS STRING
    keys(0) = "A"
    keys(1) = "AB"
    keys(2) = "Key123"
    keys(3) = "LongerPassphraseForTesting"
    LOCAL i AS LONG
    FOR i = 0 TO 3
        LOCAL e  AS STRING
        LOCAL d  AS STRING
        XorBuffer(msg, e, keys(i))
        XorBuffer(e, d, keys(i))
        PRINT "  Key '"; keys(i); "': len="; LEN(e); " match=";
        IF msg = d THEN PRINT "ok" ELSE PRINT "FAIL"
        
    NEXT i
END SUB

SUB FileEncryptSimulation()
    PRINT ""
    PRINT "File encryption simulation:"
    LOCAL content AS STRING
    content = "This is the content of a file that needs to be encrypted."
    LOCAL password AS STRING
    password = "SecurePassword123"
    LOCAL enc AS STRING
    XorBuffer(content, enc, password)
    PRINT "  Original size: "; LEN(content); " bytes"
    PRINT "  Encrypted size: "; LEN(enc); " bytes"
    PRINT "  Encrypted (first 16 hex): "
    LOCAL i AS LONG
    FOR i = 1 TO 16
        IF i <= LEN(enc) THEN
            PRINT RIGHT$("0" + HEX$(ASC(MID$(enc, i, 1))), 2)
        END IF
    NEXT i
    PRINT "..."
    PRINT ""
    PRINT "  Saving to 'output.enc'... (simulated)"
    PRINT "  Loading and decrypting... (simulated)"
    LOCAL dec AS STRING
    XorBuffer(enc, dec, password)
    PRINT "  Decrypted: "; dec
END SUB

SUB CryptoAPISim()
    PRINT ""
    PRINT "Windows CryptoAPI simulation:"
    PRINT "  1. CryptAcquireContext -> provider handle"
    PRINT "  2. CryptCreateHash(MD5) -> hash object"
    PRINT "  3. CryptHashData(password) -> hash data"
    PRINT "  4. CryptDeriveKey(RC4) -> session key"
    PRINT "  5. CryptEncrypt(data) -> encrypted data"
    PRINT "  6. CryptDecrypt(data) -> decrypted data"
    PRINT "  7. CryptReleaseContext -> cleanup"
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== File Encryption Demo ==="
    PRINT "Win32: CryptoAPI, XOR, File I/O via DECLARE"
    PRINT ""
    TestXorEncryption()
    TestWithDifferentKeys()
    FileEncryptSimulation()
    CryptoAPISim()
    PRINT ""
    PRINT "=== End File Encrypt ==="
    FUNCTION = 0
END FUNCTION


