home *** CD-ROM | disk | FTP | other *** search
- 'This function has been modified to work with QBasic. Because of many
- 'features not supported in QBasic, namely PROCEDURE level ON ERROR
- 'handling, the 'FORCE' feature is unavailable (see READTHIS.TXT for
- 'info on 'FORCE' option). This Function was originally designed for
- 'VISUAL BASIC for WINDOWS 3.0 and was modified to QBasic. That means
- 'There may be strange looking stuff becasue I just changed things to
- 'to work and did not redesign it. The 'FORCE' feature can be implemented
- 'but requires more work than I want to put into it right now. Feel free
- 'to do it yourself if it is neccessary.
-
- 'This is just a simple DEMO program to try the Function. Have Fun!
- 'Programmed by Karl D Albrecht KARL25@AOL.COM
- 'Please read the READTHIS.TXT file!
- 'Thank you
-
-
- DECLARE FUNCTION KTEncrypt$ (password$, original$, Flag%, Errors$)
- CLS
- Msg$ = "Hello, this is a test string to scramble."
-
- CommandLoop:
-
- CLS
- PRINT Msg$
- PRINT : PRINT : PRINT : PRINT : PRINT : PRINT
- PRINT STRING$(80, "-");
- LINE INPUT "PASSWORD:"; password$
- PRINT "0 - Encode or 1 - Decode"
- OK = 0
- DO WHILE OK = 0
- a$ = INKEY$
- IF a$ = "1" OR a$ = "0" THEN OK = 1
- LOOP
- which% = VAL(a$)
-
- Msg$ = KTEncrypt$(password$, Msg$, which%, Errors$)
- IF Errors$ <> "" THEN
- BEEP
-
- PRINT : PRINT : PRINT " " + Errors$
- PRINT : PRINT " Press any key"
- a$ = INPUT$(1)
- END IF
-
- GOTO CommandLoop
-
- Function KTEncrypt$ (password$, original$, Flag%, Errors$)
-
- 'Dimension the Adjust array
- ReDim Adjust(4)
-
- 'Set strng$ to original so original is unaffected
- 'QBasic does not support ByVal
- 'We want to change strng$ but not original
- strng$ = original$
-
- 'Make sure Errors$=""
- Errors$ = ""
-
-
- 'Check for errors (Errorcodes are custom)
- 'Is there Password??
- If Len(password$) = 0 Then Errors$ = "NO PASSWORD!"
-
- 'Is there a strng$ to work with?
- If Len(strng$) = 0 Then Errors$ = "NO STRING!"
-
- 'Check to see if it is an encoded file
- If Right$(strng$, 5) = String$(5, 255) Then
- 'if encoding warn!
- If Flag% = 0 Then Errors$ = "FILE ALREADY ENCODED!"
- Else
- 'If decoding warn
- If Flag% <> 0 Then Errors$ = "FILE NOT ENCODED!"
- End If
-
- 'If an error then exit
- If Errors$ <> "" Then
- KTEncrypt$ = original$
- Exit Function
- End If
-
-
- 'Create a four part encryption code based on password
- 'First Adjust code based on length of password
- Adjust(1) = Len(password$)
-
- 'If first character ascii code even make adjust negative
- If Asc(Left$(password$, 1)) / 2 = Int(Asc(Left$(password$, 1)) / 2) Then
- Adjust(1) = Adjust(1) * -1
- End If
-
- 'Second Adjust code based on first and last character ascii codes
- Adjust(2) = Asc(Left$(password$, 1)) - Asc(Right$(password$, 1))
-
- 'Third code based on average of all ascii codes
- TotalAscii = 0
- For Looper = 1 To Len(password$)
- TotalAscii = TotalAscii + Asc(Mid$(password$, Looper, 1))
- Next Looper
- Adjust(3) = Int(TotalAscii / Len(password$) / 3)
-
- 'Fourth code based on previous three
- Adjust(4) = Adjust(1) + Adjust(2) + Adjust(3)
-
-
-
- 'Now check if any Adjust codes are zero
- 'If it is zero make it not zero (any number is fine!)
- For Looper = 1 To 4
- If Adjust(Looper) = 0 Then Adjust(Looper) = Looper + Len(password$)
- Next Looper
-
-
- 'Now check if any adjusts are the same
- NotYet% = 1
- Do While NotYet%
- NotYet% = 0
- For Loop1 = 1 To 4
- For Loop2 = 1 To 4
- 'Don't compare same items
- If Loop1 <> Loop2 Then
-
- 'Check for a match
- If Adjust(Loop1) = Adjust(Loop2) Then
- Adjust(Loop2) = Adjust(Loop2) + Len(password$)
-
- 'Make sure we didn't make it zero
- If Adjust(Loop2) = 0 Then Adjust(2) = Adjust(Loop2) + Len(password$)
-
- NotYet% = 1
- End If
-
- End If
- Next Loop2
- Next Loop1
- Loop
-
-
-
-
- 'Encode or deocde
- Counts = 0: Looper = 0
-
- 'Loop until scanned though the whole file
- Do While Looper < Len(strng$)
-
- 'Add to Looper
- Looper = Looper + 1
-
- 'Keep Adjust code Counts from 1 to 4
- Counts = Counts + 1
- If Counts = 5 Then Counts = 1
-
- 'Get the character to change
- ToChange = Asc(Mid$(strng$, Looper, 1))
-
- 'ENCODE Flag%=0
- If Flag% = 0 Then
-
- 'If adjustment to high or low then reverse the coding and
- 'add in a chr$(255) to mark the change
- If ToChange - Adjust(Counts) < 1 Or ToChange - Adjust(Counts) > 254 Then
-
- Addin$ = Chr$(255) + Chr$(ToChange + Adjust(Counts))
- strng$ = Left$(strng$, Looper - 1) + Addin$ + Mid$(strng$, Looper + 1)
- Looper = Looper + 1
-
- 'If adjustment OK then just cahnge the character
- Else
-
- Mid$(strng$, Looper, 1) = Chr$(ToChange - Adjust(Counts))
-
- End If
-
- 'DECODE Flag% <> 0
- Else
-
- 'If find a CHR$(255) then remove it and set Flag255% to
- 'ensure reverse codes on next pass reverse coding
- If ToChange = 255 Then
-
- strng$ = Left$(strng$, Looper - 1) + Mid$(strng$, Looper + 1)
- Flag255% = 1
- 'Since CHR$(255) was removed we need to back up Looper
- 'and Counts because characters all shifted to the left
- Looper = Looper - 1
- Counts = Counts - 1
-
- 'If not CHR$(255) then decode watching if Flag255% is set
- Else
- If Flag255% = 1 Then
-
- 'Check if error in decoding (Bad password or file)
- CheckIt = ToChange - Adjust(Counts)
- If CheckIt < 0 Or CheckIt > 254 Then
- Errors$ = "INVALID PASSWORD!"
- KTEncrypt$ = original$
- Exit Function
- End If
-
- Mid$(strng$, Looper, 1) = Chr$(ToChange - Adjust(Counts))
- Flag255% = 0
- Else
-
- 'Check if error in decoding (Bad password or file)
- CheckIt = ToChange + Adjust(Counts)
- If CheckIt < 0 Or CheckIt > 254 Then
- Errors$ = "INVALID PASSWORD!"
- KTEncrypt$ = original$
- Exit Function
- End If
-
- Mid$(strng$, Looper, 1) = Chr$(ToChange + Adjust(Counts))
- End If
- End If
-
- End If
-
- Loop
-
-
-
-
- 'Set function equal to changed string
- If Flag% = 0 Then
-
- 'Tack on CHR$(255) to end so it can be recognized as encoded
- KTEncrypt$ = strng$ + String$(5, 255)
-
- Else
-
- KTEncrypt$ = strng$
-
- End If
-
- End Function
-
-