Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "RC4.h"
- void RC4::exchb(BYTE *b1, BYTE *b2)
- {
- BYTE tmp = *b1;
- *b1 = *b2;
- *b2 = tmp;
- }
- RC4::RC4(const BYTE *bpKey, DWORD dwKeyLen)
- {
- this->bpKey = new BYTE[dwKeyLen];
- memcpy(this->bpKey, bpKey, dwKeyLen);
- this->dwKeyLen = dwKeyLen;
- this->SBOX = new BYTE[256];
- this->initSBOX();
- }
- void RC4::initSBOX()
- {
- int i = 0, j = 0;
- for(i = 0; i < 256; i++)
- this->SBOX[i] = i;
- for(i = 0; i < 256; i++)
- {
- j = (j + this->SBOX[i] + this->bpKey[i % this->dwKeyLen]) % 256;
- this->exchb(&this->SBOX[i], &this->SBOX[j]);
- }
- }
- void RC4::encrypt(BYTE *msg, DWORD mlen)
- {
- int i = 0, j = 0;
- for(DWORD itr = 0; itr < mlen; itr++)
- {
- i = (i + 1) % 256;
- j = (j + this->SBOX[i]) % 256;
- this->exchb(&this->SBOX[i], &this->SBOX[j]);
- msg[itr] = msg[itr] ^ this->SBOX[(this->SBOX[i] + this->SBOX[j]) % 256];
- }
- this->initSBOX();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement