# Feistel Help

• 07-08-2005
JoshR
Feistel Help
I never have seen a plain feistel structure, but implementations like skipjack and DES etc... So based on theories and drawings this is my VERY simple implementation. Am I on the right track? I know I will be adding a better rounding function (F) but using an easy one for now...

Code:

```typedef unsigned int  WORD; typedef unsigned char BYTE; inline WORD F (BYTE k) {   return (((~k) << 8) + (k)); } void PEA (const bool encrypt, string &d, const string k, const int begin) {   const int ROUNDS = 32;      /* Number of Rounds */   const int SIZE = k.size();  /* Size of key */   WORD temp, left, right;      /* 32 Bit Words */   int index, step;            /* For Feistel Structure */     /* Change direction depending on encrypt or decrypt */   if (encrypt) index = 0, step = 1;   else index = ROUNDS-1, step = -1;     /* Push data bytes into words */   left =  ((d[begin] << 8) + d[begin+1]);   right = ((d[begin+2] << 8) + d[begin+3]);     /* Feistel Structure */   for (int loop = 0; loop < ROUNDS; loop++) {       temp = right;       right = left ^ F(k[index%(SIZE-1)]);       left = temp;       index += step;   }     /* Unpack words into bytes */   d[begin+3] = (left & 255), d[begin+2] = (left >> 8) & 255,   d[begin+1] = (right & 255), d[begin] = (right >> 8) & 255; }```
• 07-09-2005
dwks
Code:

`for (int loop = 0; loop < ROUNDS; loop++) {`
Was that supposed to use index and step?

I'm sorry, I don't know what "feistel structure" is, so I can't help too much.
• 07-09-2005
dwks
It seems to me that you don't need the parameter begin. You would just call the function with string+begin rather than func(string, begin).
• 07-09-2005
JoshR
I know but it only uses 4 bytes of data at a time so i had a string that i would pass the current location through PEA.

In response to your first post, no. Because step changes depending on encrypt or decrypt.
• 07-09-2005
dwks
Code:

```void func1(char *s, int begin) {     cout << (s+begin); } func1(string, 3);```
is the same as
Code:

```void func2(char *s) {     cout << s; } func2((string+3));```
• 07-09-2005
JoshR
Ok well I wanted to know if the structure was right, it doesnt matter about the begin etc...

Begin does what I want it to so ill worry about it later.
• 07-09-2005
JoshR
Quote:

Originally Posted by dwks
Code:

```void func1(char *s, int begin) {     cout << (s+begin); } func1(string, 3);```
is the same as
Code:

```void func2(char *s) {     cout << s; } func2((string+3));```

Well thats not the concept either. its not s + begin, its s[begin] so you cant do the second thing...
• 07-09-2005
dwks
No, you just go *s.

Or *(s+0) or s[0] etc.
[/edit]
• 07-09-2005
JoshR
Well I'm not using char indexing. I'm using c++ strings. So you cant add 3 to string...
• 07-09-2005
dwks
Oops, sorry, I missed that.