Simple obfuscation of string in .NET?
.net, c#, obfuscation, string
Solution
THIS IS NOT CRYPTOGRAPHY
Do not use this answer for any information that must be kept secret.
It will make a string hard for a human to read.
It will round-trip but not may not if your string is not "vanilla" and you use a large value for shift.
This code will not protect the data from a concerted effort to "crack" it. An intelligent and skilled human can decode this with pen and paper.
Original answer follows below.
How about something classical (with a modern twist).
public static string Caesar(this string source, Int16 shift)
{
var maxChar = Convert.ToInt32(char.MaxValue);
var minChar = Convert.ToInt32(char.MinValue);
var buffer = source.ToCharArray();
for (var i = 0; i < buffer.Length; i++)
{
var shifted = Convert.ToInt32(buffer[i]) + shift;
if (shifted > maxChar)
{
shifted -= maxChar;
}
else if (shifted < minChar)
{
shifted += maxChar;
}
buffer[i] = Convert.ToChar(shifted);
}
return new string(buffer);
}
Which obviously you would use like this
var plain = "Wibble";
var caesered = plain.Caesar(42);
var newPlain = caesered.Caesar(-42);
Its quick, your key is just an `Int16` and it will prevent the casual observer from copy pasting the value but, its not secure.
Problem
I need to send a string of about 30 chars over the internet which will probably end up as an ID in a another company's database. While the string itself will not be identifying, I would still like it not to be recognisable in any way. What is the easiest way to obfuscate such a string in .NET, so that it can be easily reversed when necessary?