Fork of François Berder Crypto, fixed AES CBC and small rework
Dependents: AES_example shaun_larada Smartage
Fork of Crypto by
RC4.cpp@0:7a1237bd2d13, 2013-09-07 (annotated)
- Committer:
- feb11
- Date:
- Sat Sep 07 23:47:28 2013 +0000
- Revision:
- 0:7a1237bd2d13
initial import
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 0:7a1237bd2d13 | 1 | #include "RC4.h" |
feb11 | 0:7a1237bd2d13 | 2 | |
feb11 | 0:7a1237bd2d13 | 3 | RC4::RC4(uint8_t *key, uint8_t keyLength): |
feb11 | 0:7a1237bd2d13 | 4 | Cipher(), |
feb11 | 0:7a1237bd2d13 | 5 | s(), |
feb11 | 0:7a1237bd2d13 | 6 | i(0), |
feb11 | 0:7a1237bd2d13 | 7 | j(0) |
feb11 | 0:7a1237bd2d13 | 8 | { |
feb11 | 0:7a1237bd2d13 | 9 | for(int k = 0; k < 256; ++k) |
feb11 | 0:7a1237bd2d13 | 10 | s[k] = k; |
feb11 | 0:7a1237bd2d13 | 11 | int l = 0; |
feb11 | 0:7a1237bd2d13 | 12 | for(int k = 0; k < 256; ++k) |
feb11 | 0:7a1237bd2d13 | 13 | { |
feb11 | 0:7a1237bd2d13 | 14 | l = (l + s[k] + key[k % keyLength]) % 256; |
feb11 | 0:7a1237bd2d13 | 15 | uint8_t tmp = s[l]; |
feb11 | 0:7a1237bd2d13 | 16 | s[l] = s[k]; |
feb11 | 0:7a1237bd2d13 | 17 | s[k] = tmp; |
feb11 | 0:7a1237bd2d13 | 18 | } |
feb11 | 0:7a1237bd2d13 | 19 | } |
feb11 | 0:7a1237bd2d13 | 20 | |
feb11 | 0:7a1237bd2d13 | 21 | uint8_t RC4::encyptByte(uint8_t in) |
feb11 | 0:7a1237bd2d13 | 22 | { |
feb11 | 0:7a1237bd2d13 | 23 | ++i; |
feb11 | 0:7a1237bd2d13 | 24 | j += s[i]; |
feb11 | 0:7a1237bd2d13 | 25 | uint8_t tmp = s[i]; |
feb11 | 0:7a1237bd2d13 | 26 | s[i] = s[j]; |
feb11 | 0:7a1237bd2d13 | 27 | s[j] = tmp; |
feb11 | 0:7a1237bd2d13 | 28 | uint8_t c = s[(s[i]+s[j])%256]; |
feb11 | 0:7a1237bd2d13 | 29 | return in^c; |
feb11 | 0:7a1237bd2d13 | 30 | } |
feb11 | 0:7a1237bd2d13 | 31 | |
feb11 | 0:7a1237bd2d13 | 32 | void RC4::encrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 0:7a1237bd2d13 | 33 | { |
feb11 | 0:7a1237bd2d13 | 34 | for(uint32_t l = 0; l < length; ++l) |
feb11 | 0:7a1237bd2d13 | 35 | out[l] = encyptByte(in[l]); |
feb11 | 0:7a1237bd2d13 | 36 | } |
feb11 | 0:7a1237bd2d13 | 37 | |
feb11 | 0:7a1237bd2d13 | 38 | void RC4::decrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 0:7a1237bd2d13 | 39 | { |
feb11 | 0:7a1237bd2d13 | 40 | encrypt(out, in, length); |
feb11 | 0:7a1237bd2d13 | 41 | } |
feb11 | 0:7a1237bd2d13 | 42 | |
feb11 | 0:7a1237bd2d13 | 43 | uint32_t RC4::getBlockSize() const |
feb11 | 0:7a1237bd2d13 | 44 | { |
feb11 | 0:7a1237bd2d13 | 45 | return 1; |
feb11 | 0:7a1237bd2d13 | 46 | } |