Tag Archives: RC4
RC4 cypher in C#
Main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static string RC4(string input, string key) { StringBuilder result = new StringBuilder(); int x, y, j = 0; int[] box = new int[256]; for (int i = 0; i < 256; i++) { box[i] = i; } for (int i = 0; i < 256; i++) { j = (key[i % key.Length] + box[i] + j) % 256; x = box[i]; box[i] = box[j]; box[j] = x; } for (int i = 0; i < input.Length; i++) { y = i % 256; j = (box[y] + j) % 256; x = box[y]; box[y] = box[j]; box[j] = x; result.Append((char)(input[i] ^ box[(box[y] + box[j]) % 256])); } return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to º§(ÓM¦Kéµ(äþ« string cypheredText = RC4("fluxbytes.com", "mykey"); // Results to fluxbytes.com string deCypheredText = RC4(cypheredText, "mykey"); |