Advertisement
Guest User

RC4.cpp

a guest
Oct 24th, 2010
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include "RC4.h"
  2.  
  3. void RC4::exchb(BYTE *b1, BYTE *b2)
  4. {
  5.     BYTE tmp = *b1;
  6.     *b1 = *b2;
  7.     *b2 = tmp;
  8. }
  9.  
  10. RC4::RC4(const BYTE *bpKey, DWORD dwKeyLen)
  11. {
  12.     this->bpKey = new BYTE[dwKeyLen];
  13.     memcpy(this->bpKey, bpKey, dwKeyLen);
  14.     this->dwKeyLen = dwKeyLen;
  15.     this->SBOX = new BYTE[256];
  16.     this->initSBOX();
  17. }
  18.  
  19. void RC4::initSBOX()
  20. {
  21.     int i = 0, j = 0;
  22.  
  23.     for(i = 0; i < 256; i++)
  24.         this->SBOX[i] = i;
  25.  
  26.     for(i = 0; i < 256; i++)
  27.     {
  28.         j = (j + this->SBOX[i] + this->bpKey[i % this->dwKeyLen]) % 256;
  29.         this->exchb(&this->SBOX[i], &this->SBOX[j]);
  30.     }
  31. }
  32.  
  33. void RC4::encrypt(BYTE *msg, DWORD mlen)
  34. {
  35.     int i = 0, j = 0;
  36.  
  37.     for(DWORD itr = 0; itr < mlen; itr++)
  38.     {
  39.         i = (i + 1) % 256;
  40.         j = (j + this->SBOX[i]) % 256;
  41.         this->exchb(&this->SBOX[i], &this->SBOX[j]);
  42.         msg[itr]  = msg[itr] ^ this->SBOX[(this->SBOX[i] + this->SBOX[j]) % 256];
  43.     }
  44.     this->initSBOX();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement