encryptionbasicqbasicqb64

How To Encrypt File In QB64


Am attempting to encrypt a file using this program in QB64.

It does not actually encrypt the file and always returns successful. Why?

DECLARE LIBRARY
    FUNCTION EncryptFile (f$)
    FUNCTION DecryptFile (f$, BYVAL f&)
END DECLARE
PRINT "Enter filename";
INPUT f$
IF f$ <> "" THEN
    f$ = f$ + CHR$(0)
    x = EncryptFile(f$)
    IF x = 0 THEN
        PRINT "Error encrypting file."
    ELSE
        PRINT "File encrypted."
    END IF
END IF
END

Solution

  • The solution was to detect the encryption status of a filename such as this:

    REM checks encryption status of a filename
    DECLARE DYNAMIC LIBRARY "advapi32"
        FUNCTION FileEncryptionStatusA% (f$, f&)
    END DECLARE
    DO
        PRINT "Filename";
        INPUT f$
        IF f$ = "" THEN END
        x = FileEncryptionStatusA(f$, f&)
        IF x = 0 THEN
            PRINT "Error accessing file."
        END IF
        IF x THEN
            SELECT CASE f&
                CASE 0
                    PRINT "File can be encrypted."
                CASE 1
                    PRINT "File is encrypted."
                CASE 2
                    PRINT "File is system."
                CASE 3
                    PRINT "File is root."
                CASE 4
                    PRINT "File is system directory."
                CASE 5
                    PRINT "Encryption status unknown."
                CASE 6
                    PRINT "File system does not support encryption."
                CASE 7 ' reserved
                CASE 8
                    PRINT "File is read-only."
            END SELECT
        END IF
    LOOP
    END