'Coded by S.Boukas'
'A secure memo pad with RC4 encryption'
'Before open an encrypted file,or save a file you want to be encrypted'
'set the password,otherwise will be opened or saved as it is'
'You will think that it's only an example,but soon you'll use it'
$include "rapidq.inc"
declare sub dclose
dim pw$ as string
dim s(0 to 255)
dim k(0 to 255)
dim filename as string
dim file1 as qfilestream
dim n$ as string
dim d$ as string
dim i as integer
dim x as integer
dim y as integer
dim j as integer
dim b as byte

dim mainform as qform
 mainform.Caption = "Secure memo v1.0..."
 mainform.Width = 600
 mainform.Height = 400
 mainform.Center
 dim richedit as qrichedit
 richedit.parent=mainform
 richedit.width=mainform.clientwidth
 richedit.height=mainform.clientheight
 richedit.scrollbars=2
 richedit.plaintext=true
 wordwrap=true 
dim mainmenu as qmainmenu
mainmenu.parent=mainform
dim file as qmenuitem
dim about as qmenuitem
dim list as qmenuitem
list.caption="&By S.Boukas (c)2004"
mainmenu.additems file,about
file.caption="&File"
about.caption="&About"
dim new as qmenuitem
new.caption="&New"
dim open as qmenuitem
open.caption="&Open"
dim saveas as qmenuitem
saveas.caption="&Save as.."
dim password as qmenuitem
password.caption="&Password"
dim exits as  qmenuitem 
exits.caption="&Exit"
file.additems new,open,saveas,password,exits
about.additems list
dim dialogbox as qform
dim editbox as qedit
dim button as qbutton
dialogbox.borderstyle=bsdialog
dialogbox.caption="Password.."
dialogbox.width=300
dialogbox.height=80
dialogbox.center
editbox.parent=dialogbox
editbox.top=15
editbox.left=20
editbox.width=170
editbox.text=""
button.parent=dialogbox
button.caption="&OK"
button.top=13
button.left=210
button.width=70
button.onclick=dclose


DIM FxFont AS QFONT 
FxFont.Name = "courier new" 
FxFont.size=9
FxFont.color=0 
richedit.font=fxfont




sub newclick
richedit.clear
mainform.caption="Secure memo v1.0"+" Untitled"
editbox.text=""
end sub

sub passwordclick
dialogbox.showmodal
end sub

sub openclick
d$=""
PW$=editbox.text
dim opendialog as qopendialog
if opendialog.execute then
filename=opendialog.filename
mainform.caption="Secure memo v1.0"+" "+filename
if pw$="" then richedit.loadfromfile filename:goto dd
i=0
j=0
for i=0 to 255
s(i)=i
next i
j=1
for i=0 to 255
if j>len(pw$) then j=1
k(i)=asc(mid$(pw$,j,1))
j=j+1
next i
j=0
for i=0 to 255
j=(j+s(i)+k(i))mod 256
swap s(i),s(j)
next i
file1.open(filename,fmopenread)
cc:
if file1.eof=-1 then file1.close:goto aa
file1.read(b)
i=(i+1)mod 256
j=(j+s(i))mod 256
swap s(i),s(j)
t=(s(i)+(s(j)mod 256))mod 256
y=s(t)
d$=d$+chr$(b xor y):goto cc
aa:
richedit.text=d$
editbox.text=""
dd:
end if
end sub

sub saveasclick
d$=""
pw$=editbox.text
dim savedialog as qsavedialog
if savedialog.execute then 
filename=savedialog.filename
mainform.caption="Secure memo v1.0"+" "+filename
if richedit.text="" then showmessage ("nothing to save !"):exit sub
if pw$="" then richedit.savetofile filename:goto ee
n$=richedit.text
i=0
j=0
for i=0 to 255
s(i)=i
next i
j=1
for i=0 to 255
if j>len(pw$) then j=1
k(i)=asc(mid$(pw$,j,1))
j=j+1
next i
j=0
for i=0 to 255
j=(j+s(i)+k(i))mod 256
swap s(i),s(j)
next i
for x=1 to len(n$)
a=asc(mid$(n$,x,1))
i=(i+1)mod 256
j=(j+s(i))mod 256
swap s(i),s(j)
t=(s(i)+(s(j)mod 256))mod 256
y=s(t)
d$=d$+chr$(a xor y)
next x
file1.open(filename,fmcreate)
file1.write(d$)
file1.close
showmessage ("text encrypted and saved !")
editbox.text=""
ee:
end if
end sub

sub dclose
dialogbox.close
end sub

sub exitsclick
mainform.close

end sub

sub aboutclick
showmessage ("S.Boukas (c)2004")
end sub


sub formresize
richedit.width=mainform.clientwidth
richedit.height=mainform.clientheight
end sub
'subroutines always at the end'
new.onclick=newclick
open.onclick=openclick
saveas.onclick=saveasclick
exits.onclick=exitsclick
password.onclick=passwordclick

mainform.onresize=formresize
MAINFORM.SHOWMODAL

 