Simple string encryption without dependencies

I need a simple algorithm to encrypt / decrypt a string. Something like Base64, but a bit more secure. This is not critical. All I need is some string manipulation. Not as simple as copying a string and making it readable with a simple base 64 decoder.

Why not use AES?

Since my application was created using .NET Core, it runs on Windows and Mac. The problem I am facing is that for use System.Securityon Mac I need to install openssl. Since I do not have sudo access, I cannot install it.

So here are the requirements:

  • Simple string encryption
  • No dependencies System.Security.*

Ive been reading Simple unsafe double-sided "obfuscation." for C # , but there is no solution without dependencies.

+4
source share
4 answers

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

Example with a 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);
+11
source

All asymmetric and symmetric encryption methods are in namespaces System.Security. From this SO answer :

The symmetric encryption options available in .NET Core are as follows:

  • AES (System.Security.Cryptography.Aes.Create ())
  • 3DES (System.Security.Cryptography.TripleDES.Create ())

And for asymmetric encryption

  • RSA (System.Security.Cryptography.RSA.Create ())

, , , System.Security.

EDIT: SO , . System.Security.

+1

XTEA, .

:

#include <stdint.h>

/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */

void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}
+1

- BitShifting:

 var topSecret = "This is%&/(/ TopSecret 111!!";
            int shft = 5;
            string encrypted = topSecret.Select(ch => ((int) ch) << shft).Aggregate("", (current, val) => current + (char) (val*2));
            encrypted = Convert.ToBase64String(Encoding.UTF8.GetBytes(encrypted));
            string decrypted = Encoding.UTF8.GetString(Convert.FromBase64String(encrypted)).Select(ch => ((int) ch) >> shft).Aggregate("", (current, val) => current + (char) (val/2));

:

4ZSA4aiA4amA4bOA4KCA4amA4bOA4KWA4KaA4K + A4KiA4K + A4KCA4ZSA4a + A4bCA4ZOA4aWA4aOA4bKA4aWA4bSA4KCA4LGA4LGA4LGA4KGA4KGA

:

% &/(/TopSecret 111!!

Not very nice and very minimal. You can customize salts and encodings to suit your needs.

Greetings

0
source

Source: https://habr.com/ru/post/1650467/


All Articles