5.2 Cyclic Encoding

  

The following algorithm is used for NDB_CRYPT_CYCLIC. Note that this is a symmetric cipher that is used to both encode and decode. While pv and cb represent the buffer and size for the data to encode or decode, the value to use for dwKey is the lower DWORD of the BID associated with this data block. Note that the data is encoded or decoded in place

  
 void CryptCyclic(PVOID pv, int cb, DWORD dwKey)
 {
    byte * pb = (byte *)pv;
    byte b;
    WORD w;
  
    w = (WORD)(dwKey ^ (dwKey >> 16));
  
    while (--cb >= 0) {
       b = *pb;
       b = (byte)(b + (byte)w);
       b = mpbbR[b];
       b = (byte)(b + (byte)(w >> 8));
       b = mpbbS[b];
       b = (byte)(b - (byte)(w >> 8));
       b = mpbbI[b];
       b = (byte)(b - (byte)w);
       *pb++ = b;
  
       w = (WORD)(w + 1);
    }
 }