Created
May 4, 2019 02:50
-
-
Save Creta5164/7efb69bb9414a556169a5c8782923a67 to your computer and use it in GitHub Desktop.
An obfuscation class that uses the simplest way to prevent arbitrary modification of strings.
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
using System; | |
using System.Text; | |
public static class StringObfuscator | |
{ | |
const int MAX = byte.MaxValue + 1; | |
public static byte[] Convert(string str) { | |
if (string.IsNullOrEmpty(str)) | |
return null; | |
byte[] text = Encoding.UTF8.GetBytes(str); | |
byte[] result = new byte[text.Length + 1]; | |
result[0] = (byte)(0x7F + new Random().Next(0x7F)); | |
for (long i = 1; i < result.Length; i++) | |
result[i] = (byte)((text[i - 1] + result[0]) % MAX); | |
return result; | |
} | |
public static string Parse(byte[] bin) { | |
byte[] text = new byte[bin.Length - 1]; | |
int key = bin[0]; | |
for (long i = 0; i < text.Length; i++) { | |
text[i] = (byte)((bin[i + 1] - key) % MAX); | |
} | |
return Encoding.UTF8.GetString(text); | |
} | |
} |
Author
Creta5164
commented
May 4, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment