This library implements some hash and cryptographic algorithms.

Dependents:   mBuinoBlinky PB_Emma_Ethernet SLOTrashHTTP Garagem ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RC4.cpp Source File

RC4.cpp

00001 #include "RC4.h"
00002 
00003 RC4::RC4(uint8_t *key, uint8_t keyLength):
00004 StreamCipher(),
00005 s(),
00006 i(0),
00007 j(0)
00008 {
00009     for(int k = 0; k < 256; ++k)
00010         s[k] = k;
00011     int l = 0;
00012     for(int k = 0; k < 256; ++k)
00013     {
00014         l = (l + s[k] + key[k % keyLength]) % 256;
00015         uint8_t tmp = s[l];
00016         s[l] = s[k];
00017         s[k] = tmp;
00018     } 
00019 }
00020 
00021 uint8_t RC4::encryptByte(uint8_t in)
00022 {
00023     ++i;
00024     j += s[i];
00025     uint8_t tmp = s[i];
00026     s[i] = s[j];
00027     s[j] = tmp;
00028     uint8_t c = s[(s[i]+s[j])%256];
00029     return in^c;  
00030 }
00031 
00032 uint8_t RC4::decryptByte(uint8_t in)
00033 {
00034     return encryptByte(in);
00035 }