Skip to content

Instantly share code, notes, and snippets.

@Creta5164
Created May 4, 2019 02:50
Show Gist options
  • Save Creta5164/7efb69bb9414a556169a5c8782923a67 to your computer and use it in GitHub Desktop.
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.
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);
}
}
@Creta5164
Copy link
Author

Obfuscate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment