Created
March 7, 2016 08:46
-
-
Save hoiogi/89cf2e9aa99ffc3640a4 to your computer and use it in GitHub Desktop.
C# RC4 Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RC4 { | |
public static byte[] Encrypt(byte[] pwd, byte[] data) { | |
int a, i, j, k, tmp; | |
int[] key, box; | |
byte[] cipher; | |
key = new int[256]; | |
box = new int[256]; | |
cipher = new byte[data.Length]; | |
for (i = 0; i < 256; i++) { | |
key[i] = pwd[i % pwd.Length]; | |
box[i] = i; | |
} | |
for (j = i = 0; i < 256; i++) { | |
j = (j + box[i] + key[i]) % 256; | |
tmp = box[i]; | |
box[i] = box[j]; | |
box[j] = tmp; | |
} | |
for (a = j = i = 0; i < data.Length; i++) { | |
a++; | |
a %= 256; | |
j += box[a]; | |
j %= 256; | |
tmp = box[a]; | |
box[a] = box[j]; | |
box[j] = tmp; | |
k = box[((box[a] + box[j]) % 256)]; | |
cipher[i] = (byte)(data[i] ^ k); | |
} | |
return cipher; | |
} | |
public static byte[] Decrypt(byte[] pwd, byte[] data) { | |
return Encrypt(pwd, data); | |
} | |
} |
Thanks! I searched simple encoding for communicate my net core application with microcontroller. I use it in app and ported this code to c++for microcontroller!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great thanks!