Thread: Feistel Help

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    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;
    }
    Last edited by JoshR; 07-09-2005 at 01:13 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    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.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    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.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    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...

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, you just go *s.

    [edit]
    Or *(s+0) or s[0] etc.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    Well I'm not using char indexing. I'm using c++ strings. So you cant add 3 to string...

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oops, sorry, I missed that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed