I've seen a few people writing password encryption code and have decided totry my hand at it.
This is what I've come up with.
Rem Project: Encrypt
Rem Created: 26/03/2005 22:35:04
Rem ***** Main Source File *****
Rem This is a keyword that is fixed in your program. The password you use
Rem Encrypts this word. You can then store it to a file.
Keyword$="EnCrYpTiOn"
do
set cursor 0,0
Print "Press 'S' to set a password"
Print "Press 'C' to check a stored password"
if upper$(inkey$())="S"
gosub setpassword
cls
endif
if upper$(inkey$())="C"
gosub checkpassword
cls
endif
loop
end
setpassword:
cls
input "please enter a password :",pass$
cls
rem Encode the keyword using the password you provide
enc$=EncodeString(pass$,Keyword$)
dec$=DecodeString(pass$,enc$)
print "Keyword=" , chr$(34),Keyword$,chr$(34)
print "Encoded String=" , chr$(34),enc$,chr$(34)
print "Decoded String=" , chr$(34),dec$,chr$(34)
delete file "password.txt"
open to write 1,"password.txt"
write string 1,enc$
close file 1
print "press a key to continue."
wait key
return
checkpassword:
cls
input "please enter a password :",pass$
cls
open to read 1,"password.txt"
read string 1,enc$
close file 1
rem Decode the keyword using the password you provide
dec$=DecodeString(pass$,enc$)
if dec$=Keyword$
print "password is correct"
else
print "password is incorrect"
endif
print "press a key to continue."
wait key
return
Function EncodeString(Password$,sString$)
rem set initial random number seed based on first character of password
randomize asc(mid$(Password$,1))
rem add together password ascii values to create random number seed for
rem text encryption
For iIndex = Len(Password$) to 1 step -1
nSeed = nSeed + (Asc(Mid$(Password$, iIndex)) * rnd(100))
Next iIndex
Randomize nSeed
For iIndex = 1 To Len(sString$)
nRandom=Rnd(254)+1
rem Xor random number with string to encrypt and conver to hex.
rem NOTE - I created a hex value because sometimes xoring an ascii
rem value creates a value of zero, so if you store an encrypted character
rem of ascii value '0' it signifies the end of your string. This could mean
rem that your encrypted string is missing half the characters.
shex$=hex$(Asc(Mid$(sString$, iIndex)) ~~ nRandom)
rem pad the hex out to 2 characters
if len(shex$)=1 then shex$="0"+shex$
sEncoded$ = sEncoded$ + shex$
Next iIndex
EndFunction sEncoded$
Function DecodeString(Password$,sString$)
rem set initial random number seed based on first character of password
randomize asc(mid$(Password$,1))
rem add together password ascii values to create random number seed for
rem text encryption
For iIndex = Len(Password$) to 1 step -1
nSeed = nSeed + (Asc(Mid$(Password$, iIndex)) * rnd(100))
Next iIndex
Randomize nSeed
For iIndex = 1 To Len(sString$) step 2
nRandom=Rnd(254)+1
rem Convert the hex to an ascii value
ascvalue=HexToDec(mid$(sString$,iIndex)+mid$(sString$,iIndex+1))
rem and xor it to return the decrypted character
sDecoded$ = sDecoded$ + Chr$(ascvalue ~~ nRandom)
Next iIndex
EndFunction sDecoded$
function HexToDec(HexValue$)
multiplier=0
total=0
HexValue$=upper$(HexValue$)
for i=len(HexValue$) to 1 step -1
ascvalue=asc(mid$(HexValue$,i))
rem Is character between 0 and 9. If so convert it to a decimal value
if ascvalue>47 and ascvalue<58 then value=ascvalue-48
rem Is character between A and F. If so convert it to a decimal value
if ascvalue>64 and ascvalue<71 then value=ascvalue-55
rem increase running total dependent on position in hex string
total=total+(value*16^multiplier)
inc multiplier
next i
endfunction total
You specify a keyword that remains constant in your program. This keyword is encrypted by using a password you specifiy. The encrypted text is then stored to a file.
To check your password the encrypted text is decrypted by using your password and is checked against the programs keyword. If these match the password must also match.
It's obviously not as good as the high level encryption available, but is should be enough to put off most casual hackers.
Cheers,
Cloggy