Post by xcoder on Jun 5, 2023 13:56:55 GMT
'Text Encryption Real RC4 stream cipher
'Use a Pass Word as the Key
' Just paste text into the texteditor and press encrypt button
nomainwin
BackgroundColor$ = "darkcyan"
ForegroundColor$ = "black"
global key$, key2$
WindowWidth = 600 : WindowHeight = 400
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
texteditor #main.text, 200, 20, 360, 320
button #main.enc, "Encrypt", [encrypt], ul, 55, 155, 110, 25
button #main.dec, "Decrypt", [decrypt], ul, 55, 195, 110, 25
button #main.clearKey , "Clear Key", [clearKey], ul, 30, 75, 70, 20
button #main.dec , "Clear Text", [clearText], ul, 110, 75, 70, 20
button #main.refresh , "Delete Encrypted File", [refresh], ul, 40, 240, 135, 25
button #main.copytext , "Copy Text", [copytext], ul, 75, 105, 60, 25
textbox #main.key, 30, 45, 150, 20
statictext #main.editor, "Type or Paste Text Here", 335, 5, 190, 15
statictext #main.encKey, "Encryption Key ", 20, 30, 170, 15
Open "Real RC4 Stream Encryptor " for window as #main
#main "trapclose [quit]"
key$ = "MyFavoritePassWord" ''' default key if none entered
#main.key key$
#main.text "!autoresize"
if fileExists(DefaultDir$, "textEditor") then
open "textEditor" for input as #1
#main.text , text$ 'read from textEditor
close #1
#main.text "!origin 0 0" 'reset cursor
end if
wait
[encrypt]
#main.key "!contents? key$"
if key$ = "" then [advise]
#main.text "!contents? text$" ' read from textEditor
text$ = trim$(text$)
call keyPass '''^^^^^^^^^^^^^ modified
call KSA
alpha$ = txPRGA$(text$) '***** encrypt
#main.text "!cls"
#main.text alpha$ 'write to textEditor
open "textEditor" for output as #1
#1 alpha$
close #1
#main.text "!origin 0 0" 'reset cursor
wait
[decrypt]
#main.key "!contents? key$"
if key$ = "" then [advise]
#main.text "!contents? text$" 'read from textEditor
text$ = trim$(text$)
call keyPass '''^^^^^^ modified
call KSA
bravo$ = hex2Text$(text$) '***** decrypt
#main.text "!cls"
#main.text bravo$ 'write to textEditor
open "textEditor"for output as #1
#1 bravo$
close #1
#main.text "!origin 0 0" 'reset cursor
wait
[clearKey]
#main.key ""
wait
[clearText]
#main.text "!cls"
wait
[refresh]
if fileExists(DefaultDir$, "textEditor") then kill DefaultDir$;"\textEditor"
#main.text "!cls"
wait
[copytext]
#main.text "!selectall"
#main.text "!copy"
wait
[quit]
close #main
End
[advise]
notice "Key is required"
close #main
End
print "------------------------------------------------------------------------------------------------------------"
'function for checking file existence
function fileExists(path$, filename$)
dim info$(0, 0)
files path$, filename$, info$()
fileExists = val(info$(0, 0)) 'non zero is true
end function
sub keyPass '''^^^^ modified to accept keyboard characters
print "ascii value"
key2$ = key$ '''^^^^ global string variables
for i = 1 to len(key2$)
byte = asc(mid$(key2$,i,1))
print byte
next i
print key2$ '''mainwin testing
print
end sub
sub KSA '''''key expansion ^^^^^^^^^^^
dim S(256)
dim K(256)
i = 0: j = 0
for i = 0 to 255
S(i)= i
next i
for i = 0 to 255
K(i)= asc(mid$(key2$,i mod len(key2$)+1))
next i
for i = 0 to 255
j = (j+ S(i)+ K(i))mod 256
temp = S(i)
S(i)= S(j)
S(j)= temp
next i
end sub
print "############## Encryption Functions ##################"
function txPRGA$(inString$) '''''psuedo random byte generator
i = 0: j = 0
for x = 1 to len(inString$)
i = (i + 1) mod 256 '********
byte = asc(mid$(inString$,x,1))
j = (j + S(i))mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + S(j))mod 256 '''real RC4
k = S(t) ''' real RC4
ck = byte xor k ''' real RC4
char$ = char$ + chr$(ck) ''' real RC4
next x
txPRGA$ = text2Hex$(char$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$) 'encrypted text
byte = asc(mid$(cipher$,i,1)) 'encrypted ascii
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "############# Decryption functions ###############"
function hex2Text$(hex$)
for i = 1 to len(hex$)step 2 'encrypted hex
chunk$ = mid$(hex$,i,2)
byte = hexDec(chunk$) 'encrypted ascii
text$ = text$ + chr$(byte) 'encrypted text
next i
hex2Text$= rxPRGA$(text$)
end function
function rxPRGA$(inString$) '''^^^^psuedo random byte generator
i = 0: j = 0
for x = 1 to len(inString$)
i = (i + 1) mod 256 '**********
byte = asc(mid$(inString$,x,1))
j = (j + S(i))mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + S(j))mod 256 ''' real RC4
k = S(t) ''' real RC4
ck = byte xor k ''' real RC4
char$ = char$ + chr$(ck) ''' real RC4
next x
rxPRGA$ = char$
end function
'Use a Pass Word as the Key
' Just paste text into the texteditor and press encrypt button
nomainwin
BackgroundColor$ = "darkcyan"
ForegroundColor$ = "black"
global key$, key2$
WindowWidth = 600 : WindowHeight = 400
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
texteditor #main.text, 200, 20, 360, 320
button #main.enc, "Encrypt", [encrypt], ul, 55, 155, 110, 25
button #main.dec, "Decrypt", [decrypt], ul, 55, 195, 110, 25
button #main.clearKey , "Clear Key", [clearKey], ul, 30, 75, 70, 20
button #main.dec , "Clear Text", [clearText], ul, 110, 75, 70, 20
button #main.refresh , "Delete Encrypted File", [refresh], ul, 40, 240, 135, 25
button #main.copytext , "Copy Text", [copytext], ul, 75, 105, 60, 25
textbox #main.key, 30, 45, 150, 20
statictext #main.editor, "Type or Paste Text Here", 335, 5, 190, 15
statictext #main.encKey, "Encryption Key ", 20, 30, 170, 15
Open "Real RC4 Stream Encryptor " for window as #main
#main "trapclose [quit]"
key$ = "MyFavoritePassWord" ''' default key if none entered
#main.key key$
#main.text "!autoresize"
if fileExists(DefaultDir$, "textEditor") then
open "textEditor" for input as #1
#main.text , text$ 'read from textEditor
close #1
#main.text "!origin 0 0" 'reset cursor
end if
wait
[encrypt]
#main.key "!contents? key$"
if key$ = "" then [advise]
#main.text "!contents? text$" ' read from textEditor
text$ = trim$(text$)
call keyPass '''^^^^^^^^^^^^^ modified
call KSA
alpha$ = txPRGA$(text$) '***** encrypt
#main.text "!cls"
#main.text alpha$ 'write to textEditor
open "textEditor" for output as #1
#1 alpha$
close #1
#main.text "!origin 0 0" 'reset cursor
wait
[decrypt]
#main.key "!contents? key$"
if key$ = "" then [advise]
#main.text "!contents? text$" 'read from textEditor
text$ = trim$(text$)
call keyPass '''^^^^^^ modified
call KSA
bravo$ = hex2Text$(text$) '***** decrypt
#main.text "!cls"
#main.text bravo$ 'write to textEditor
open "textEditor"for output as #1
#1 bravo$
close #1
#main.text "!origin 0 0" 'reset cursor
wait
[clearKey]
#main.key ""
wait
[clearText]
#main.text "!cls"
wait
[refresh]
if fileExists(DefaultDir$, "textEditor") then kill DefaultDir$;"\textEditor"
#main.text "!cls"
wait
[copytext]
#main.text "!selectall"
#main.text "!copy"
wait
[quit]
close #main
End
[advise]
notice "Key is required"
close #main
End
print "------------------------------------------------------------------------------------------------------------"
'function for checking file existence
function fileExists(path$, filename$)
dim info$(0, 0)
files path$, filename$, info$()
fileExists = val(info$(0, 0)) 'non zero is true
end function
sub keyPass '''^^^^ modified to accept keyboard characters
print "ascii value"
key2$ = key$ '''^^^^ global string variables
for i = 1 to len(key2$)
byte = asc(mid$(key2$,i,1))
print byte
next i
print key2$ '''mainwin testing
end sub
sub KSA '''''key expansion ^^^^^^^^^^^
dim S(256)
dim K(256)
i = 0: j = 0
for i = 0 to 255
S(i)= i
next i
for i = 0 to 255
K(i)= asc(mid$(key2$,i mod len(key2$)+1))
next i
for i = 0 to 255
j = (j+ S(i)+ K(i))mod 256
temp = S(i)
S(i)= S(j)
S(j)= temp
next i
end sub
print "############## Encryption Functions ##################"
function txPRGA$(inString$) '''''psuedo random byte generator
i = 0: j = 0
for x = 1 to len(inString$)
i = (i + 1) mod 256 '********
byte = asc(mid$(inString$,x,1))
j = (j + S(i))mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + S(j))mod 256 '''real RC4
k = S(t) ''' real RC4
ck = byte xor k ''' real RC4
char$ = char$ + chr$(ck) ''' real RC4
next x
txPRGA$ = text2Hex$(char$)
end function
function text2Hex$(cipher$)
for i = 1 to len(cipher$) 'encrypted text
byte = asc(mid$(cipher$,i,1)) 'encrypted ascii
hex$ = decHex$(byte)
hex$ = right$("0" + hex$,2)
max$ = max$ + hex$ 'encrypted hex
next i
text2Hex$ = max$
end function
print "############# Decryption functions ###############"
function hex2Text$(hex$)
for i = 1 to len(hex$)step 2 'encrypted hex
chunk$ = mid$(hex$,i,2)
byte = hexDec(chunk$) 'encrypted ascii
text$ = text$ + chr$(byte) 'encrypted text
next i
hex2Text$= rxPRGA$(text$)
end function
function rxPRGA$(inString$) '''^^^^psuedo random byte generator
i = 0: j = 0
for x = 1 to len(inString$)
i = (i + 1) mod 256 '**********
byte = asc(mid$(inString$,x,1))
j = (j + S(i))mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + S(j))mod 256 ''' real RC4
k = S(t) ''' real RC4
ck = byte xor k ''' real RC4
char$ = char$ + chr$(ck) ''' real RC4
next x
rxPRGA$ = char$
end function