Tag Archives: csharp
How to get your IP in C#
There are two ways of getting your IP address in C#. One of them is by using the methods provided by the .NET framework and the other one by using a third party api.
What is the difference you might ask. The answer is fairly simple. There are two kinds of IP’s, internal IP’s and external IP’s. Sadly the .NET framework methods are not able to retrieve the external IP if the computer is not directly connected to the internet (no router or any other similar devices). Since this limitation exists there is only one option if you wish to get the external IP, and this is by using an online service.
I’ve constructed two methods below. One uses only the .NET framework methods but is not 100% accurate when trying to retrieve the external IP (requires the system to be directly connected to the internet). The second method uses an online service which should have fairly accurate results as long as the service is online and the computer is not behind a proxy.
Method 1
LINQ approach
1 2 3 4 5 6 | IPAddress localAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); if (localAddress != null) { // do something with localAddress } |
Non-LINQ approach (for .NET framework versions before 3.5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | IPAddress localAddress = null; foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { localAddress = address; break; } } if (localAddress != null) { // do something with localAddress } |
Method 2
1 2 3 4 5 6 7 8 9 10 | string ipAddress; using (WebClient wc = new WebClient()) { ipAddress = wc.DownloadString("http://icanhazip.com/"); } if (!string.IsNullOrEmpty(ipAddress)) { // do something with ipAddress } |
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); |