Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: AES_example shaun_larada Smartage
Fork of Crypto by
Diff: cipher/RC4.cpp
- Revision:
- 7:2dbbdfb08123
- Parent:
- 0:7a1237bd2d13
- Child:
- 8:a090264e9b2d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cipher/RC4.cpp Sat Sep 14 18:21:32 2013 +0000
@@ -0,0 +1,46 @@
+#include "RC4.h"
+
+RC4::RC4(uint8_t *key, uint8_t keyLength):
+Cipher(),
+s(),
+i(0),
+j(0)
+{
+ for(int k = 0; k < 256; ++k)
+ s[k] = k;
+ int l = 0;
+ for(int k = 0; k < 256; ++k)
+ {
+ l = (l + s[k] + key[k % keyLength]) % 256;
+ uint8_t tmp = s[l];
+ s[l] = s[k];
+ s[k] = tmp;
+ }
+}
+
+uint8_t RC4::encyptByte(uint8_t in)
+{
+ ++i;
+ j += s[i];
+ uint8_t tmp = s[i];
+ s[i] = s[j];
+ s[j] = tmp;
+ uint8_t c = s[(s[i]+s[j])%256];
+ return in^c;
+}
+
+void RC4::encrypt(uint8_t *out, uint8_t *in, uint32_t length)
+{
+ for(uint32_t l = 0; l < length; ++l)
+ out[l] = encyptByte(in[l]);
+}
+
+void RC4::decrypt(uint8_t *out, uint8_t *in, uint32_t length)
+{
+ encrypt(out, in, length);
+}
+
+uint32_t RC4::getBlockSize() const
+{
+ return 1;
+}
