Tag Archives: cryptography
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"); |
ROT-13 cypher in C#
ROT-13 is a letter substitution cypher that replaces a letter with the letter 13 letters after it in the alphabet. ROT-13 is an example of the Caesar cypher with the difference that Caesar cypher allows you to specify the number of letters to shift.
Main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static string Rot13(string input) { StringBuilder result = new StringBuilder(); Regex regex = new Regex("[A-Za-z]"); foreach (char c in input) { if (regex.IsMatch(c.ToString())) { int charCode = ((c & 223) - 52) % 26 + (c & 32) + 65; result.Append((char)charCode); } else { result.Append(c); } } return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to syhkolgrf.pbz string cypheredText = Rot13("fluxbytes.com"); // Results to fluxbytes.com string deCypheredText = Rot13(cypheredText); |
Atom-128 algorithm in C#
Encode 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 32 33 34 35 36 37 38 39 | public static string Atom128Encode(string input) { string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC"; StringBuilder result = new StringBuilder(); int i = 0; int[] indexes = new int[4]; int[] chars = new int[3]; do { chars[0] = i + 1 > input.Length ? 0 : (int)input[i++]; chars[1] = i + 2 > input.Length ? 0 : (int)input[i++]; chars[2] = i + 3 > input.Length ? 0 : (int)input[i++]; indexes[0] = chars[0] >> 2; indexes[1] = ((chars[0] & 3) << 4) | (chars[1] >> 4); indexes[2] = ((chars[1] & 15) << 2) | (chars[2] >> 6); indexes[3] = chars[2] & 63; if ((char)chars[1] == 0) { indexes[2] = 64; indexes[3] = 64; } else if ((char)chars[2] == 0) { indexes[3] = 64; } foreach (int index in indexes) { result.Append(key[index]); } } while (i < input.Length); return result.ToString(); } |
Decode 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 32 | public static string Atom128Decode(string input) { string key = "/128GhIoPQROSTeUbADfgHijKLM+n0pFWXY456xyzB7=39VaqrstJklmNuZvwcdEC"; StringBuilder result = new StringBuilder(); int[] indexes = new int[4]; int[] chars = new int[3]; int i = 0; do { indexes[0] = key.IndexOf(input[i++]); indexes[1] = key.IndexOf(input[i++]); indexes[2] = key.IndexOf(input[i++]); indexes[3] = key.IndexOf(input[i++]); chars[0] = (indexes[0] << 2) | (indexes[1] >> 4); chars[1] = (indexes[1] & 15) << 4 | (indexes[2] >> 2); chars[2] = (indexes[2] & 3) << 6 | indexes[3]; result.Append((char)chars[0]); if (indexes[2] != 64) result.Append((char)chars[1]); if (indexes[3] != 64) result.Append((char)chars[2]); } while (i < input.Length); return result.ToString(); } |
Usage:
1 2 3 4 5 | // Results to LxrkpIQu0IHtOxSC+qCC+bCC string encodedString = Atom128Encode("fluxbytes.com"); // Results to fluxbytes.com string decodedString = Atom128Decode(encodedString); |
How to encrypt and decrypt files in C#
The following snippets will allow you to encrypt and decrypt files in C#.
Needless to say there are numerous methods that this can be achieved but for the reasons I will explain at the end I came about using this one in my projects.
Also keep in mind that assuming you want to implement something like this in your project you should extend the error catching to detect invalid key lengths, file locations and so on.
Posted in C#.
Tagged aes, C#, cryptography, csharp, decrypt file, encrypt file, RijndaelManaged, snippet, winforms