I'm trying to encrypt a query string on a game I'm making when opening a url. It doesn't have to be complicated, in fact since I'm working from a game engine it needs to be as simple as possible. It tends to fuss if I get too low level.
I've already created the query string, I just need to take each char of it and subtract 15 from the char to lightly encrypt it. I'm just wanting to make a simple encryption that will deter most users.
I wish I could give a code example but I'm not too experienced in C, and I'm not even sure where to begin. The game engine's api usually makes everything simple for me.
if you want to, you can just xor the bytes
by iterating through the array and using ^= this decrypts and encrypts
None of these answers really constitute any form of reasonable encryption.
What you actually want to do, is use some form of authenticated encryption, and some form of secure key derivation algorithm. My personal recommendation is libsodium. It provides very good defaults, and an API that is relatively hard to get wrong.
There's several different ways to do this:
All of these possibilities are integrated into libsodium and implementable with relative ease.
The following code examples are taken directly from the libsodium documentation.
For 1:
For 2: (Deriving a key from a password)
Now, the key-array contains a key that is suitable for the use in the code sample above. Instead of
randombytes_buf(key, sizeof key)
for generating a random key, we generated a key derived from a user-defined password, and use that for encryption.3 is the "most complicated" of the 3 types. It is what you use if you have two parties communicating. Each of the parties generates a "keypair", which contains a public and a secret key. With those keypairs, they can together agree on a "shared key" that they can use for encrypting (and signing) data for each other:
This code first generates both keypairs (typically, this would happen on bob's and alice's machine separately, and they would send each other their public key, while keeping their secret key, well, secret).
Then, a random nonce is generated, and the call to
crypto_box_easy(...)
encrypts a message from alice to bob (using bob's public key to encrypt, and alice's secret key to make a signature).Then (after potentially sending the message over the network), the call to
crypto_box_open_easy(...)
is used by bob to decrypt the message (using his own secret key to decrypt, and alice's public key to verify the signature). If the verification of the message failed for some reason (someone tried to tamper with it), this is indicated by the non-zero return code.Notice the function name. What you want to do is silly, and pretty worthless.
You can use a variant of base64 with a custom alphabet, or just a shuffled alphabet. It's not really secure, but in your case it is probably sufficient. The algorithm is widely used, so it will be easy for you to find an implementation where you can provide a custom alphabet.
The bonus point is, that whatever you put into the query string, the encoded form will consist of valid URL characters, if you choose the alphabet appropriately.
Your "encryption" won't fool anybody.
There are good implementatons of well-known and secure encryption algorithms available online.
For example: Twofish
Edit:
Example implementation of XOR:
Assumes that the array containing the query string is 8 or less bytes long. Increase the length of
secret
to meet your needs.SonOfRa already proposed the right answer.
But if you're intent on using something terrible to obscure a string without actually encrypting it, the GNU C library provides a
memfrob(3)
that is already-written and easily reversible.