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;
}