Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Encryption Done Proper
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
md




PostPosted: Wed Oct 18, 2006 9:47 pm   Post subject: Encryption Done Proper

Because I am paranoid (and thus spend a lot of time thinking about encryption), and to keep people from posting their own (not at all secure) encryption schemes I present the code for TEA (Tiny Encryption Algorythm). The code presented here suffers from a minor key collision; however it's best known attack is still > brute-force. It's a 64bit block cypher, with a 128bit key.

c:

 void encrypt(unsigned long* v, unsigned long* k) {
     unsigned long v0=v[0], v1=v[1], sum=0, i;           /* set up */
     unsigned long delta=0x9e3779b9;                     /* a key schedule constant */
     unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
     for (i=0; i < 32; i++) {                            /* basic cycle start */
         sum += delta;
         v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
         v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);   /* end cycle */
     }
     v[0]=v0; v[1]=v1;
 }
 
 void decrypt(unsigned long* v, unsigned long* k) {
     unsigned long v0=v[0], v1=v[1], sum=0xC6EF3720, i;  /* set up */
     unsigned long delta=0x9e3779b9;                     /* a key schedule constant */
     unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
     for(i=0; i<32; i++) {                               /* basic cycle start */
         v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
         v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
         sum -= delta;                                   /* end cycle */
     }
     v[0]=v0; v[1]=v1;
 }


That encrypts a block, so clearly you need to implement more then that. But at least no one needs to go trying to implement AES or Blowfish.

[edit] Actually TEA does suffer from a few known weaknesses, however if your really serious you can use XTEA which is just as simple (maybe 2 more lines of code) and yet solves most (if not all) of the flaws in TEA.

To quote Wikipedia (which is surprisingly good on encryption)
Wikipedia wrote:
TEA has a few weaknesses. Most notably, it suffers from equivalent keys "” each key is equivalent to three others, and this means that the effective key size is only 126 bits (Kelsey et. al., 1996, and Vikram Andem, 2003). This weakness led to a method for hacking Microsoft's Xbox game console, where the cipher was used as a hash function. TEA is also susceptible to a related-key attack which requires 223 chosen plaintexts under a related-key pair, with 232 time complexity (Kelsey et. al., 1997).

Because of these weaknesses, a number of revisions of TEA have been designed, including XTEA


[edit 2] Yes, it's C code... but really given how simple it is I should hope that anyone could implement it in their language of choice, even *shudders* turing.
Sponsor
Sponsor
Sponsor
sponsor
apomb




PostPosted: Thu Oct 19, 2006 6:26 pm   Post subject: (No subject)

wow, md, thats pretty cool, ive been starting to read "Digital Fortress" - Dan Brown again, and was actually looking into encryption algorithms in languages i know. thanks for the example! Smile

Note: Turing will NOT be used by this guy
bugzpodder




PostPosted: Sat Oct 21, 2006 9:15 pm   Post subject: (No subject)

Razz weren't you the same person who came up with that encryption method that encrypts different strings onto the same encoding?
md




PostPosted: Sat Oct 21, 2006 9:28 pm   Post subject: (No subject)

bugzpodder wrote:
Razz weren't you the same person who came up with that encryption method that encrypts different strings onto the same encoding?


No no, it was much more complex then that; it was a ceasar cypher, that changed depending on the number of characters seen before hte current one. It was secure... but only in that it mangled the data much to well. It wasn't possible to decrypt things. Razz

I know I do not (yet) have the expertise to creat a secure cypher, hence why I have been reading up on other peoples Wink
bugzpodder




PostPosted: Sun Oct 22, 2006 7:35 pm   Post subject: (No subject)

there's a crytography class offered, may want to look into that. its under C&O
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: