|
Pages: [1] 2 3 ... 7
|
 |
|
Author
|
Topic: FieldView encryption challenge (Read 20640 times)
|
Z!re
*/-\*
    
Posts: 4599
|
Description: Create an encryption with an accompanying decryption algorithm. The functions will be used in Plantasy Studios upcoming game FieldView, proper credits will ofcourse be given.
Rules: * Should be fully compatible with latest official version of FB (This does not include pre-releases and/or CVS builds) * Resulting encrypted message should be as close (or smaller) in lenght to the original message as possible. 1:1 (or less) encryptions are favored. * Should finish within a reasonable time, meaning 5-10 milliseconds or less for 200byte messages * Functions should be formated as: Function (Msg As STRING, Pwd As STRING) As STRING * Multiple entries are allowed, and encouraged * The routines should be able to handle any ASCII char from 0 to 255
Judging: Judging entries will be done not focusing on security, but more on speed and size. Many small fast entries are favored over few large slow more secure. This does not mean that a simple Julius/Caesar encryption is good enough. Also, the closer to 1:1 the resulting encrypted message is the better. Smaller encrypted messages than the original message are highly favored.
Prize: Fame and honor Grattitude fom the FV dev. team, and most likely ingame bonuses if you want them.
Additional info: Any entry submitted may be used in FieldView, even if it does not win the challenge. All used algorithms/functions will be properly credited. The reason for this challenge is to speed up FV developement, I'm only one person, I cant do everything.
Good luck, have fun!
EDIT: Rule added
|
|
|
|
|
Logged
|
|
|
|
|
|
TheBlueKeyboard
Wandering Guru
  
Posts: 334

|
Encryptor is done... Uses bit shifting with password. [Edit] Code has been optimized. [/Edit] [2nd Edit] Code has been optimized even more [/2nd Edit] [3rd Edit] Code has been optimized even even even more  , new code is at end of thread [/3rd Edit]
|
|
|
|
|
Logged
|
|
|
|
KiZ
__/--\__
    
Posts: 2879
|
Coding for me is tricky atm because I'm not at home, but here is a little scrambler routine.
It is very fast.
[syntax="qbasic"]declare function encode(msg as string, pwd as string) as string declare function decode(msg as string, pwd as string) as string
function encode(msg as string, pwd as string) as string for rt = 1 to len(msg) ascno = asc(mid$(msg, rt, 1))
pwdv = pwdv + 1 if pwdv > len(pwd) then pwdv = 1 offset = asc(mid$(pwd, pwdv, 1))
ascno = ascno + offset if ascno > 127 then ascno = ascno - 128 Sencode$ = Sencode$ + chr$(ascno) next encode = Sencode$ end function
function decode(msg as string, pwd as string) as string for rt = 1 to len(msg) ascno = asc(mid$(msg, rt, 1))
pwdv = pwdv + 1 if pwdv > len(pwd) then pwdv = 1 offset = asc(mid$(pwd, pwdv, 1))
ascno = ascno - offset if ascno < 0 then ascno = ascno + 128 Sdecode$ = Sdecode$ + chr$(ascno) next decode = Sdecode$ end function[/syntax]
Dammit! BKB beat me to the starter's gun. Looks much better as well. =P
|
|
|
|
|
Logged
|
|
|
|
whitetiger0990
__/--\__
    
Posts: 2964

|
declare function Encrypt (Msg As STRING, Pwd As STRING) As STRING declare function Decrypt (Msg As STRING, Pwd As STRING) As STRING
function Encrypt (Msg As STRING, Pwd As STRING) As STRING dim pavg as integer, i as integer, i2 as integer for i = 1 to len(pwd) pavg=pavg+asc(mid$(pwd,i,1))+i next i pavg=pavg/len(pwd) if pavg>255 then pavg=255-pavg for i=1 to len(msg) i2=asc(mid$(msg,i,1))+pavg+i+len(pwd) if i2>255 then i2=255-i2 mid$(msg,i,1)=chr$(i2) next i return msg end function
function Decrypt (Msg As STRING, Pwd As STRING) As STRING dim pavg as integer, i as integer, i2 as integer for i = 1 to len(pwd) pavg=pavg+asc(mid$(pwd,i,1))+i next i pavg=pavg/len(pwd) if pavg>255 then pavg=255-pavg for i=1 to len(msg) i2=asc(mid$(msg,i,1))-pavg-i-len(pwd) if i2<0 then i2=i2+255 mid$(msg,i,1)=chr$(i2) next i return msg end function Probably sucks but I'm too lazy to care =P EDIT: *wonders why the tabs look lined up in the post but not after submitted*
|
|
|
|
|
Logged
|
 [size=10]Back by popular demand! I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
|
|
|
|
|
|
|
KiZ
__/--\__
    
Posts: 2879
|
*wonders why the tabs look lined up in the post but not after submitted* *wonders why Wts uses a similar method as mine* except with 255 character support for extended ascii. I just used 128 character support. sounds like a familiar scandal that happened a year or so ago, ey WT!! or perhaps great minds just think alike. they'll be used in fieldview, and if they're on the boards, anyone could take them and find a way to send their own data to the servers to cheat, or manipulate incoming data. any good encoding routine should be able to have its source shown and still remain secure.
|
|
|
|
|
Logged
|
|
|
|
|
|
barok
Na_th_an
    
Posts: 1723

How about a tasty lead sandwich?
|
that could cause lag though on slower computers or computers with slower connections... While the new key is coming in to client computers, the clients would still be sending out data encrypted using out of date encryptions, therefore the data would be rejected by the server, and the game for that person would "lag" for a few seconds while the client is still receiving his new key... Just a minor detail, and easily fixed if the servers will still accept the old password for about 10 seconds... And every 10 minutes seems a bit extreme, don't you think? I'd think once every hour. Anyways, to quote Na_th_an from a year ago: If you want a good cryptosystem, google for RSA.
|
|
|
|
|
Logged
|
Jumping Jahoolipers!
|
|
|
|
|
dumbledore
Ancient Guru
   
Posts: 520

|
here's my entry, written in c (for speed) (noone said it was an fb-only challenge ;P): http://m0n573r.afraid.org/libcryptc.zipthe funcs are a bit different than you requested, but c doesn't use fb strings >_> i could put it in the format you requested if you like, but i'd need to use fb wrapper funcs, and as it is you'd have to be really lazy to require that ;P source: void* encrypt(void* data,char* pass,int len); void* decrypt(void* data,char* pass); double bin(char,int); unsigned long hash(unsigned long,unsigned long,unsigned long);
void* encrypt(void* data,char* pass,int len) { unsigned char byte[4],put=0,y; char x; int curpos=0; char* encr; unsigned long readsofar=0,tot=0,curhash=0,passlen; for(passlen=0;pass[passlen]!=0;passlen++); encr=calloc(1,len+7); //4-byte header for(x=6;x>=0;x-=2) encr[curpos++]=(int)floor(len/(1<<x*4))&255; //teh encryption while(readsofar<len) { if(readsofar+3<=len) { byte[0]=((char*)data)[readsofar]^pass[readsofar%passlen]; byte[1]=((char*)data)[readsofar+1]^pass[(readsofar+1)%passlen]; byte[2]=((char*)data)[readsofar+2]^pass[(readsofar+2)%passlen]; byte[3]=((char*)data)[readsofar+3]^pass[(readsofar+3)%passlen]; } else { byte[0]=((char*)data)[readsofar]^pass[readsofar%passlen]; byte[1]=(readsofar+1)>len?0:((char*)data)[readsofar+1]^pass[(readsofar+1)%passlen]; byte[2]=(readsofar+2)>len?0:((char*)data)[readsofar+2]^pass[(readsofar+2)%passlen]; byte[3]=(readsofar+3)>len?0:((char*)data)[readsofar+3]^pass[(readsofar+3)%passlen]; } tot=0; for(y=0;y<4;y++) tot+=(unsigned long)bin(byte[y],(int)(1<<y)); for(y=1;y<=7;y+=2) { put=(unsigned char)floor(tot/(1<<(7-y)*4)); tot-=(unsigned long)(put*(1<<(7-y)*4)); encr[curpos++]=put; } readsofar+=4; } //done. return (void *)encr; }
void* decrypt(void* data,char* pass) { unsigned long len=0,curpos=0,d,passlen; char* decr; char y,x; unsigned char tmpdecr,byte[4]; for(passlen=0;pass[passlen]!=0;passlen++); //get the len for(x=6;x>=0;x-=2) { len+=((unsigned char*)data)[curpos++]*(unsigned long)(1<<x*4); } decr=calloc(1,len+1); for(d=0;d<len;d+=4) { //encrypted bytes byte[0]=((char*)data)[curpos]; byte[1]=((char*)data)[curpos+1]; byte[2]=((char*)data)[curpos+2]; byte[3]=((char*)data)[curpos+3]; for(x=0;x<4;x++) { //decrypt it for(y=0;y<4;y++) { tmpdecr=(unsigned char)(((byte[x]&(int)(1<<7-y))!=0)*(1<<7-x*2)+((byte[x]&(int)(1<<3-y))!=0)*(1<<6-x*2)); if(tmpdecr!=0) decr[3-y+d]+=tmpdecr; } } for(y=0;(d+4<=len||y<(int)(len%4))&&y<4;y++) decr[y+d]^=pass[(d+y)%passlen]; curpos+=4; } return (void *)decr; }
double bin(char num,int mult) { double ans=0; int x; for(x=0;x<=8;x++) ans+=(1<<x*4)*mult*(((int)(1<<x)&num)!=0); //return hex return ans; }
*note: this is subject to change at any time. (mainly for fixing mentioned function differences if required)  :-? it's pretty small, only ~3kb in static lib format, and is pretty fast too, according to fb it takes 0.008291936240439028 msec per 200byte string  (well, it takes .414 seconds per 5000 200byte strings, so...) i'll post binaries once i get around to building / bugfixing the thing enough  (currently the source is kinda debugified...) [edit] code above updated, and here's the wrapper: declare function encrypt_c cdecl alias "encrypt" ( byval data as any ptr, byval pass as zstring ptr, byval len as integer) as any ptr declare function decrypt_c cdecl alias "decrypt" ( byval data as any ptr, byval pass as zstring ptr) as any ptr
type FBSTRING data as zstring ptr len as integer size as integer end type
function encrypt(msg as string, pwd as string) as string dim as string ret dim as FBSTRING ptr retptr dim as any ptr stuff stuff = encrypt_c( strptr( msg ), strptr( pwd ), len( msg ) + 1 ) retptr = cptr( FBSTRING ptr, @ret ) retptr->data = stuff retptr->len = len( msg ) + 7 retptr->size = len( msg ) + 7 return ret end function
function decrypt(msg as string, pwd as string) as string dim as string ret dim as FBSTRING ptr retptr dim as any ptr stuff stuff = decrypt_c( strptr( msg ), strptr( pwd ) ) retptr = cptr( FBSTRING ptr, @ret ) retptr->data = stuff retptr->len = len( msg ) - 7 retptr->size = len( msg ) - 6 return ret end function
oh, and as to size: 4-byte header, plus the original datasize to the nearest (higher) number divisible by 4. for simplicity the wrapper hacks the string size to len+7, but often it is less than this.  it uses bit "scrambling" plus password. by "scrambling" i mean that bits from one char get swapped with another char, etc (4 chars get stuff swapped actually, that's what the deal is with the len being divisible by 4).
|
|
|
|
|
Logged
|
ttp://m0n573r.afraid.org/ quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side ...phear
|
|
|
|
|
Z!re
*/-\*
    
Posts: 4599
|
just fyi, maybe you should take down those algorithms and PM them to z!re. After all, they'll be used in fieldview, and if they're on the boards, anyone could take them and find a way to send their own data to the servers to cheat, or manipulate incoming data. FV will use a nifty and evil system for encryption. Dosent matter if you have the encryption/decryption routines or not. Post source here, easier to see, and gives ideas to others, which keeps it interessting @Dumbledore, could you make a static lib of the C code? I dont have any good C compilers, and I dont plan to get any either Come on cowards, more entries! Or are you scared!? 
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Pages: [1] 2 3 ... 7
|
|
|
|
|