• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

xxtea implementation how to use

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Cryptographic Software and Hardware

View previous topic :: View next topic  
Author Message
dhanu
Just Arrived
Just Arrived


Joined: 02 Nov 2008
Posts: 0


Offline

PostPosted: Sun Nov 02, 2008 4:18 pm    Post subject: xxtea implementation how to use Reply with quote

please help me implement XXTEA from wikipedia

Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>




  /* #define MX (z>>5^y<<2) + (y>>3^z<<4)^(sum^y) + (key[p&3^e]^z) */
  #define MX  ( (((z>>5)^(y<<2))+((y>>3)^(z<<4)))^((sum^y)+(key[(p&3)^e]^z)) )
 


 long btea(long* v, long length, long* key);

void main()
{
   long length;
   unsigned char buffer[30];
   unsigned char key[30];
   strcpy(buffer,"9248979020");
   strcpy(key,"7813498");

   length = strlen((long *)buffer);

   printf("original buffer %s %ld\n",buffer,length);

   btea((long *)buffer,length,(long *)key);
      printf("encrypted buffer %s %ld\n",(char *)buffer,length);


   length = strlen((long *)buffer);
   
   length = - length;
   btea((long *)buffer,length,(long *)key);
   printf("length =%ld\n",length);
   printf("decrypted buffer %s  %ld  \n",(char *)buffer,length);
   getchar();
   getchar();
}



  long btea(long* v, long length, long* key) {
    unsigned long z /* = v[length-1] */, y=v[0], sum=0, e, DELTA=0x9e3779b9;
    long p, q ;
    if (length > 1) {          /* Coding Part */
      z=v[length-1];           /* Moved z=v[length-1] to here, else segmentation fault in decode when length < 0 */
      q = 6 + 52/length;
      while (q-- > 0) {
        sum += DELTA;
        e = (sum >> 2) & 3;
        for (p=0; p<length-1; p++) y = v[p+1], z = v[p] += MX;
        y = v[0];
        z = v[length-1] += MX;
      }
      return 0 ;
    } else if (length < -1) {  /* Decoding Part */
      length = -length;
      q = 6 + 52/length;
      sum = q*DELTA ;
      while (sum != 0) {
        e = (sum >> 2) & 3;
        for (p=length-1; p>0; p--) z = v[p-1], y = v[p] -= MX;
        z = v[length-1];
        y = v[0] -= MX;
        sum -= DELTA;
      }
      return 0;
    }
    return 1;
  }


Moderator: added code tags - capi
Back to top
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Cryptographic Software and Hardware All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register