Last active
March 18, 2022 17:42
-
-
Save formix/4076442db2304fb326a0f30bcbbebff5 to your computer and use it in GitHub Desktop.
EasyCrypt: An Obfuscation C# Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************************** | |
* Copyright 2009-2018 Jean-Philippe Gravel, P. Eng., PSEM | |
* | |
* Modified 07/31/16 - Jean-Philippe Gravel (@formixian) - Changed the class | |
* def to static instead of being an instance class. | |
* - Changed license to Apache 2.0 | |
* 02/08/18 - Added Obfuscate and Clarify methods | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
***************************************************************************/ | |
using System; | |
namespace Formix | |
{ | |
/// <summary> | |
/// Use a random 4 bytes prefix and a feedback loop to obfuscate the same | |
/// input array differently each time. Expects small, self contained | |
/// data chunks. Not designed to be efficient with large data streams. | |
/// Unsafe for real life encryption, safe enough for unwanted prying eyes | |
/// to peek into your settings and get connection strings and passwords | |
/// easily. | |
/// </summary> | |
public static class EasyCrypt | |
{ | |
public static byte[] Cypher(byte[] data, uint key) | |
{ | |
byte[] cypher = new byte[data.Length + 4]; | |
Random header = new Random(); | |
cypher[0] = (byte)header.Next(256); | |
cypher[1] = (byte)header.Next(256); | |
cypher[2] = (byte)header.Next(256); | |
cypher[3] = (byte)header.Next(256); | |
byte[] keybuf = new byte[data.Length]; | |
Random rnd = new Random(key.GetHashCode()); | |
rnd.NextBytes(keybuf); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
cypher[i + 4] = (byte)(cypher[i] ^ data[i] ^ keybuf[i]); | |
} | |
return cypher; | |
} | |
public static byte[] Decypher(byte[] cypher, uint key) | |
{ | |
Random rnd = new Random(key.GetHashCode()); | |
byte[] data = new byte[cypher.Length - 4]; | |
byte[] keybuf = new byte[cypher.Length - 4]; | |
rnd.NextBytes(keybuf); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
data[i] = (byte)(cypher[i] ^ cypher[i + 4] ^ keybuf[i]); | |
} | |
return data; | |
} | |
public static string Obfuscate(string clearText, uint key) | |
{ | |
var cypher = Cypher(Encoding.UTF8.GetBytes(clearText), key); | |
return $"#:{Convert.ToBase64String(cypher)}"; | |
} | |
public static string Clarify(string obfuscatedText, uint key) | |
{ | |
if (!obfuscatedText.StartsWith("#:")) | |
{ | |
// The obfuscation marker is not present. We assume that the | |
// input is not obfuscated. | |
return obfuscatedText; | |
} | |
var cypher = Convert.FromBase64String(obfuscatedText.Substring(2)); | |
var data = Decypher(cypher, key); | |
return Encoding.UTF8.GetString(data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will end up with 256 possible result only. Having 4 random bytes does not improve entropy when XORed on byte at time.