RC4 lib
Posted: Mon Jul 11, 2016 10:09 pm
;### RC4 lib from the WaterJuice CryptLib
;### License: None, public domain
;### Purebasic demo with libs for Windows, OSX, Linux, 32+64
DOWNLOAD (26kb) MIRRORS:
http://www.mediafire.com/download/0d615zdxy5zyurj/rc4lib.zip
https://www.sendspace.com/file/ejvimp
http://s000.tinyupload.com/index.php?file_id=18220965242140535050
hello I was looking at RC4 as I like it because its small and i find it easier to understand and use compared to some like AES which I can spell but that's about it lol, and being a stream cipher instead of block cipher i think its easier to work with! I understand there are some security issues with RC4 so it shouldn't be used for serious security but it still remains useful!
my search showed there are already a couple of RC4's for Purebasic, this is just one more option!
just for completeness I think this is the best PB implementation so far
- http://www.purebasic.fr/english/viewtopic.php?t=6905
I found this WaterJuice CryptLib library, it is Public Domain and compiles to around ~1-2kb (one of the Windows builds is 920 bytes), and that's the full object file
https://github.com/WaterJuice/CryptLib
All we need are the librc4.c and librc4.h files, so it's one of the easier libs to compile! (phew!)
COMPILING:
I've already compiled all the libs (see download above) for all 3 OS, 32+64, and all 4 levels of optimization, but to do it yourself i used the GCC compiler on all 3 OS (for Windows i used TDM-GCC) to build the .o object file:
gcc -c librc4.c
The "-m32" or "-m64" flag can be added to specify machine target.
The "-On" (n=0-3) flag can be used to specify optimization level with 3 the highest, ive included builds at all levels.
GCC provides hundreds of flags for other things like SSE etc but thats out of scope and its all new to me anyway.
I had no compile errors on Windows or OSX. On Linux i initially got an error "sys/cdefs.h: No such file or directory", which after googling i fixed simply with: sudo apt-get install gcc-multilib
DEMO (slightly more updated than the one in the zip due to compiler directives but still functionally equivalent):
Here is my demo attempt, i think i've got it right, hopefully one of the crypto experts here can verify please
;### License: None, public domain
;### Purebasic demo with libs for Windows, OSX, Linux, 32+64
DOWNLOAD (26kb) MIRRORS:
http://www.mediafire.com/download/0d615zdxy5zyurj/rc4lib.zip
https://www.sendspace.com/file/ejvimp
http://s000.tinyupload.com/index.php?file_id=18220965242140535050
hello I was looking at RC4 as I like it because its small and i find it easier to understand and use compared to some like AES which I can spell but that's about it lol, and being a stream cipher instead of block cipher i think its easier to work with! I understand there are some security issues with RC4 so it shouldn't be used for serious security but it still remains useful!
my search showed there are already a couple of RC4's for Purebasic, this is just one more option!
just for completeness I think this is the best PB implementation so far

I found this WaterJuice CryptLib library, it is Public Domain and compiles to around ~1-2kb (one of the Windows builds is 920 bytes), and that's the full object file

https://github.com/WaterJuice/CryptLib
All we need are the librc4.c and librc4.h files, so it's one of the easier libs to compile! (phew!)
COMPILING:
I've already compiled all the libs (see download above) for all 3 OS, 32+64, and all 4 levels of optimization, but to do it yourself i used the GCC compiler on all 3 OS (for Windows i used TDM-GCC) to build the .o object file:
gcc -c librc4.c
The "-m32" or "-m64" flag can be added to specify machine target.
The "-On" (n=0-3) flag can be used to specify optimization level with 3 the highest, ive included builds at all levels.
GCC provides hundreds of flags for other things like SSE etc but thats out of scope and its all new to me anyway.
I had no compile errors on Windows or OSX. On Linux i initially got an error "sys/cdefs.h: No such file or directory", which after googling i fixed simply with: sudo apt-get install gcc-multilib
DEMO (slightly more updated than the one in the zip due to compiler directives but still functionally equivalent):
Here is my demo attempt, i think i've got it right, hopefully one of the crypto experts here can verify please

Code: Select all
#RC4OPT$ = "3" ;0-3
CompilerIf (#PB_Compiler_OS = #PB_OS_Linux) Or (#PB_Compiler_OS = #PB_OS_Windows And #PB_Compiler_Processor = #PB_Processor_x64)
#APIPrefix = ""
CompilerElse
#APIPrefix = "_" ;some APIs are prefixed with "_", you know how it is!
CompilerEndIf
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows: #RC4OS$ = "Windows"
CompilerCase #PB_OS_MacOS: #RC4OS$ = "OSX"
CompilerCase #PB_OS_Linux: #RC4OS$ = "Linux"
CompilerEndSelect
CompilerSelect #PB_Compiler_Processor
CompilerCase #PB_Processor_x64: #RC4CPU$ = "64"
CompilerCase #PB_Processor_x86: #RC4CPU$ = "32"
CompilerEndSelect
#RC4LIB$ = #RC4OS$+"/rc4-"+#RC4CPU$+".O"+#RC4OPT$+".o"
;// Rc4Context - This must be initialised using Rc4Initialised. Do not modify the contents of this structure directly.
Structure RC4Context
i.l
j.l
S.a[256]
EndStructure
ImportC #RC4LIB$ ;"Windows/rc4-32.O3.o"
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;// Rc4Initialise
;// Initialises an RC4 cipher And discards the specified N number of first bytes.
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Rc4Initialise(*Context.RC4Context, *Key, KeySize.l, DropN.l) As #APIPrefix+"Rc4Initialise"
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;// Rc4Output (Not called in this demo, doesnt seem needed unless you want to make your own version of Rc4Xor)
;// Outputs the requested number of bytes from the RC4 stream
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Rc4Output(*Context.Rc4Context, *Buffer, BufSize.l) As #APIPrefix+"Rc4Output"
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;// Rc4Xor
;// XORs the RC4 stream With an input buffer And puts the results in an output buffer. This is used For encrypting
;// And decrypting Data. InBuffer And OutBuffer can point To the same location For inplace encrypting/decrypting
;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Rc4Xor(*Context.Rc4Context, *InBuffer, *OutBuffer, BufSize.l) As #APIPrefix+"Rc4Xor"
EndImport
Define ctx.RC4Context
Define keystr.s = "secret key"
Define keylen.i = Len(keystr)
Define *keybuf = AllocateMemory(256) ;can be any size
Define datstr.s = "my secret message"
Define datlen.i = Len(datstr)
Define *datbuf = AllocateMemory(65535) ;can be any size
PokeS(*keybuf, keystr, -1, #PB_Ascii)
PokeS(*datbuf, datstr, -1, #PB_Ascii)
;// ALICE
;1a. Initialise
Rc4Initialise(ctx,*keybuf,keylen,0)
;2a. Encrypt
Rc4Xor(ctx,*datbuf,*datbuf,datlen) ;can use the same buffer for inplace encrypting/decrypting
ShowMemoryViewer(*datbuf, datlen)
MessageRequester("RC4","Encrypted")
;//... Alice sends Bob the message ...
;// BOB
;1b. Initialise (now in sync with Alice's stream)
Rc4Initialise(ctx,*keybuf,keylen,0)
;2b Decrypt
Rc4Xor(ctx,*datbuf,*datbuf,datlen)
ShowMemoryViewer(*datbuf, datlen)
MessageRequester("RC4","Decrypted")