Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple String Encryption without Dependencies

Tags:

c#

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 System.Security 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 System.Security.*

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

like image 979
dknaack Avatar asked Aug 07 '16 16:08

dknaack


People also ask

What are the three 3 different encryption methods?

The various encryption types. The three major encryption types are DES, AES, and RSA.

Can encryption be done without a key?

You can't encrypt something without a key. If there's no key, either anyone can recover the plain-text, or it is unrecoverable to everyone.


1 Answers

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] = (byte)(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] = (byte)(data[i] ^ xorConstant)
}
string plainText = Encoding.UTF8.GetString(data);
like image 89
Mitch Avatar answered Oct 22 '22 17:10

Mitch