dknaack dknaack - 1 year ago 141
C# Question

Simple String Encryption without Dependencies

I need a simple algorithm to encrypt / decrypt a string. Something like Base64 but a little more secure. It is not mission critical.
All I need is some string manipulation. Not as easy as copy the string and make it human readable using a simple base 64 decoder.

Why not using AES?

Since my app is created using .NET Core, it runs on windows and mac. The problem i am facing is that in order to use
on mac, i need openssl to be installed.
Since i dont have sudo access, i cant install it.

So here are the requirements:

  • Simple String Encryption

  • No Dependencies on

Ive read Simple two-way encryption for C# but there is no solution without dependencies.

Answer Source

If you are looking for obfuscation rather than security, you could XOR the string with a constant or the output of a PRNG initialized with a constant seed.

Example with constant:

byte xorConstant = 0x53;

string input = "foo";
byte[] data = Encoding.UTF8.GetBytes(input);
for (int i = 0; i < data.Length: i++)
    data[i] = data[i] ^ xorConstant;
string output = Convert.ToBase64String(data);

To decode:

byte xorConstant = 0x53;
byte[] data = Convert.FromBase64String(input);
for (int i = 0; i < data.Length: i++)
    data[i] = data[i] ^ xorConstant;
string plainText = Encoding.UTF8.GetString(data);