Speading encryption

BlitzPlus Forums/BlitzPlus Programming/Speading encryption

Im trying to make a light encryption program that uses my own personal version of xor so that it xors single bytes rather than integers. Im looking for a few hints on the program. First off, the professional encryption programs out there use what they call "processes" to (as i believe) create several instances of the program to run at once on the computer. This (would) have obvious speed enhancements. Is there any way to do this through blitz? If so can i get an example? Also, would the encryption go faster if I simply used a command such as "output = val(right$(input xor mask,8))"? Are there any other ways to speed my encryption? I would very much appreciate any help you pros out there could give me.

AppTitle "Xor Me!"

;create main window
Global mainwin = CreateWindow("Xor Me!",GadgetWidth(Desktop()) / 3,GadgetHeight(Desktop()) / 3,350,105,0,1)
	CreateLabel("File: ",7,10,30,20,mainwin)
	Global Filebox=CreateTextField(40,10,200,20,mainwin)
	Global Browsebutt=CreateButton("Browse...",250,10,70,25,mainwin)
	Global Encbutt=CreateButton("Encrypt",10,40,70,25,mainwin)
	Global decbutt=CreateButton("Decrypt",95,40,70,25,mainwin)
	Global progbar=CreateProgBar(185,40,150,25,mainwin)

;assorted variables
;Global progbar
Global splshwin
Global canbutt
Global fileoutloc$
Global filein
Global fileout
Global source
Global fsize#
Global bytebuff
Global I#
Dim buff(9)

;main window control
Repeat
	If KeyDown(1) Then Exit
	WaitEvent()
	source = EventSource()
	Select EventID()
	Case $401
		If source = browsebutt
			rfbuffer$ = RequestFile$()
			SetGadgetText(Filebox,rfbuffer$)
		ElseIf source = encbutt
			If enc%(TextFieldText$(filebox))
				Notify("Succesful!")
				If Proceed("Delete Original File?",True) = 1 Then DeleteFile(TextFieldText$(filebox))
			Else
				Notify("Encryption Failed!")
			End If
		ElseIf source = decbutt
			If dec%(TextFieldText$(filebox))
				Notify("Succesful!")
				If Proceed("Delete Original File?",True) = 1 Then DeleteFile(TextFieldText$(filebox))
			Else
				Notify("Decryption Failed!")
			End If
		End If
	Case $803
		If source = mainwin Then End
	End Select
Forever 

;encryption header
Function Enc%(fileinloc$)
	If FileType(fileinloc) <> 1
		Notify "Could not open file: " + fileinloc$
		Return 0
	End If
	fsize = FileSize(fileinloc)
	fileoutloc$ = fileinloc + ".xor"
	filein = ReadFile(fileinloc)
	fileout = WriteFile(fileoutloc)
	
	For I = 0 To 9
		buff(I) = Rand(255)
		WriteByte(fileout,buff(I))
	Next
	Return main%()
End Function 

;decryption header
Function dec%(fileinloc$)
	If FileType(fileinloc) <> 1
		Notify "Could not open file: " + fileinloc$
		Return 0
	End If
	fsize = FileSize(fileinloc)
	fileoutloc$ = Left$(fileinloc,Len(fileinloc) - 4)
	filein = ReadFile(fileinloc)
	fileout = WriteFile(fileoutloc)
	For I = 0 To 9
		buff(I) = ReadByte(filein)
	Next
	Return main%()
End Function

;main loop
Function main%()
	;splshscr() ;start the splash screen
	DisableGadget(mainwin) ;disable unwanted events
	FlushEvents() ;ensure that the button is pressed once and only once
	I# = 0 ;needs to be floating point for division purposes
	Repeat
		If KeyDown(1) Then Return 0 ;possible exits of main loop
		If Eof(filein) Then Exit
		If I Mod(1000) = 0 Then UpdateProgBar(progbar,I/fsize) ;update the progbar
		WriteByte(fileout,bytexor(ReadByte(filein),buff(I Mod(10)))) ;calls most of the work
		I = I + 1
	Forever
	EnableGadget(mainwin)
	FlushEvents()
	Return 1
End Function

;converts binary to base 10
Function val(Inputstr$)
	For I# = 0 To 7
		integer = integer + ((2 ^ I) * Int(Right$(inputstr$,1)))
		inputstr$ = Left$(inputstr$,Len(inputstr) - 1)
	Next
	Return Int(integer)
End Function

;Xor's and converts base 10 to binary
Function bytexor(num,mask)
	bytestr$ = Right$(Bin$(num),8)
	maskstr$ = Right$(Bin$(mask),8)
	Local ret$ = ""
	For I = 0 To 7
		If Left$(maskstr,1) <> Left$(bytestr,1)
			ret = ret + "1"
		Else
			ret = ret + "0"
		End If
		maskstr = Right$(maskstr,Len(maskstr) - 1)
		bytestr = Right$(bytestr,Len(bytestr) - 1)
	Next
	ret2% = val(ret)
	Return ret2
End Function

Function main2%()
	DisableGadget(mainwin) ;disable unwanted events
	FlushEvents() ;ensure that the button is pressed once and only once
	I# = 0 ;needs to be floating point for division purposes
	Repeat
		If KeyDown(1) Then Return 0 ;possible exits of main loop
		If Eof(filein) Then Exit ;      "       "    "   "    "
		If I Mod(1000) = 0 Then UpdateProgBar(progbar,I/fsize) ;update the progbar
		PokeByte(bankbuff,I,bytexor(PeekByte(bankbuff,I),buff(I Mod(10)))) ;calls most of the work
		I = I + 1
	Forever
	EnableGadget(mainwin)
	FlushEvents()
	Return 1
End Function

Function loadbytes()
	ReadBytes(bankbuff,bankfile,0,fsize)
End Function

Function savebytes()
	WriteBytes(bankbuff,fileout,0,fsize)
End Function
		


Yes I do realize that I spelled speeding wrong in the title.

I haven't looked at your 'encryption' code, but I noticed a few misconceptions on your part.

First off, professional encryption programs would probably use 'threads' not 'processes'. Processes is a term that more closely approximates a program that is running on your computer. 'Threads' are sub-programs within your program.

Secondly, I really can't see how multi-threading would help to en/decrypt faster. Multi-threading is used more efficiently in other scenarios, and is often used to 'share' the CPU, which is often just being nice to the user.

But if you want to just 'crunch' numbers as fast as possible, I would stick to a single process running through the entire task without any interruptions.


For some better encryption techniques than just your XOR method, check out this page:
http://www.cs.usask.ca/resources/tutorials/csconcepts/1999_3/lessons/L3/SimpleEncryption.html
Keep clicking on Next to read it all.

O.K. yeah you are right I did misuse processes, but is there any way to read/write to/from a file faster than using read/writebytes? Currently it takes about five minutes for a single 444K file to encrypt/decrypt. And to throw off your last suggestion, the reason I used xor is so that I could speed the encryption, at the time it looked faster then the ways suggested in the site which you pointed me to.

This is extremely inefficient:
	Repeat
		If KeyDown(1) Then Return 0 ;possible exits of main loop
		If Eof(filein) Then Exit
		If I Mod(1000) = 0 Then UpdateProgBar(progbar,I/fsize) ;update the progbar
		WriteByte(fileout,bytexor(ReadByte(filein),buff(I Mod(10)))) ;calls most of the work
		I = I + 1
	Forever
Change it to load a huge amount of data into a bank using the ReadBytes command (NOT ReadByte), convert the data, then store it using the WriteBytes command. In fact, you can then remove the IF part from your If I Mod(1000) = 0 line. And personally, I would change it to more like 10,000 or so because UpdateProgBar is VERY slow.

Frick... Wow i really should have looked at my code closer... thanx a lot... I was in the middle of transfering the code to use readbyteS, and i guess I forgot to merge it at the most important parts...sorry for wasting time.