Qbasicnews.com
October 16, 2017, 10:50:57 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3 ... 7
  Print  
Author Topic: FieldView encryption challenge  (Read 51446 times)
Z!re
*/-\*
*****
Posts: 4599


« on: August 13, 2005, 11:54:40 AM »

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



« Reply #1 on: August 13, 2005, 02:23:44 PM »

Ok, I'm gonna give this an attempt... Cheesy
Encryption....hmmm, /me tries to think of a good way to encrypt Tongue
Hmmmm....
Logged

It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
TheBlueKeyboard
Wandering Guru
***
Posts: 334



« Reply #2 on: August 15, 2005, 09:34:24 PM »

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 Tongue, new code is at end of thread [/3rd Edit]
Logged

It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #3 on: August 15, 2005, 09:54:08 PM »

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



WWW
« Reply #4 on: August 15, 2005, 10:10:10 PM »

Code:
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]
barok
Na_th_an
*****
Posts: 1727


How about a tasty lead sandwich?


« Reply #5 on: August 16, 2005, 01:29:28 AM »

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.
Logged

Jumping Jahoolipers!
dumbledore
Ancient Guru
****
Posts: 520



WWW
« Reply #6 on: August 16, 2005, 01:46:21 AM »

in case you haven't noticed, the good ones have passwords, you won't be able to just start sending stuff and magically know the pass ;P
Logged

ttp://m0n573r.afraid.org/
Quote from: "HexDude"
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
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #7 on: August 16, 2005, 02:07:48 AM »

Quote from: "whitetiger"
*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.

Quote
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
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #8 on: August 16, 2005, 03:59:03 AM »

Make the server send out an encrypted password to the clients which changes every ten minjutes or so. When it changes, it's sent out and the client adapts.

Just a thought...

>anarky
Logged

Screwing with your reality since 1998.
barok
Na_th_an
*****
Posts: 1727


How about a tasty lead sandwich?


« Reply #9 on: August 16, 2005, 04:36:55 AM »

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:
Quote

If you want a good cryptosystem, google for RSA.
Logged

Jumping Jahoolipers!
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #10 on: August 16, 2005, 04:44:17 AM »

I thought every hour as well. yes the old password could be accepted for 45 seconds. This allows for standard timeouts. I haven't once seen anything online take more than 45 seconds to connect. Most servers are capable of handling that quite well.

>anarky
Logged

Screwing with your reality since 1998.
dumbledore
Ancient Guru
****
Posts: 520



WWW
« Reply #11 on: August 16, 2005, 05:25:33 AM »

here's my entry, written in c (for speed) (noone said it was an fb-only challenge ;P):
http://m0n573r.afraid.org/libcryptc.zip
the 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:
Code:
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) Wink :-?
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 Wink (well, it takes .414 seconds per 5000 200byte strings, so...)
i'll post binaries once i get around to building / bugfixing the thing enough Wink (currently the source is kinda debugified...)

[edit] code above updated, and here's the wrapper:
Code:
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. Wink
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 from: "HexDude"
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
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #12 on: August 16, 2005, 09:23:11 AM »

Quote from: "dark_prevail"
*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.

Oh at least one has a timemachine... =P
No really I did not look at yours. My encryption part was already done by the time you posted yours. I just finished the decryption (switching the + to - basically) and posted.

So you must have a time machine.
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
Z!re
*/-\*
*****
Posts: 4599


« Reply #13 on: August 16, 2005, 10:18:16 AM »

Quote from: "barok"
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 Tongue

@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 Tongue



Come on cowards, more entries! Or are you scared!? Tongue
Logged
barok
Na_th_an
*****
Posts: 1727


How about a tasty lead sandwich?


« Reply #14 on: August 16, 2005, 10:56:36 AM »

i'll try to dream up an algorithm at work.  Obviously XOR encryption is too simple.  :lol:
Logged

Jumping Jahoolipers!
Pages: [1] 2 3 ... 7
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!