Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[DCing Algorithm - C#] For Those Who Code
#1
Here is my cool DCing Algorithm for those who like to play with pointers and like the power of unsafe code:
    CSHARP-Code:
using System;
using System.Text;
using System.IO;
 
namespace LF2.IDE
{
	public static class LF2DataUtil
	{
		public static string EncryptionKey { get { return Settings.Current.encryptionKey; } }
		public static string DecryptionKey { get { return Settings.Current.decryptionKey; } }
 
		public static string Decrypt(string filepath)
		{
			byte[] buffer = File.ReadAllBytes(filepath);
			byte[] decryptedtext = new byte[Math.Max(0, buffer.Length - 123)];
			string password = EncryptionKey;
 
			if (string.IsNullOrEmpty(password)) return Encoding.Default.GetString(buffer);
 
			for (int i = 0, j = 123; i < decryptedtext.Length; i++, j++)
				decryptedtext[i] = (byte)(buffer[j] - (byte)password[i % password.Length]);
 
			return Encoding.Default.GetString(decryptedtext);
		}
 
		public static unsafe string DecryptUnsafe(string filepath)
		{
			int dec, pass;
			byte[] buffer = File.ReadAllBytes(filepath);
			byte[] decryptedtext = new byte[dec = Math.Max(0, buffer.Length - 123)];
			byte* password = stackalloc byte[pass = EncryptionKey.Length];
 
			if (pass == 0) return Encoding.Default.GetString(buffer);
 
			for (int i = 0; i < pass; i++)
				password[i] = (byte)EncryptionKey[i];
 
			fixed (byte* b = buffer, d = decryptedtext)
			{
				for (int i = 0, j = 123; i < dec; i++, j++)
					d[i] = (byte)(b[j] - password[i % pass]);
			}
 
			return Encoding.Default.GetString(decryptedtext);
		}
 
		public static void Encrypt(string text, string filepath)
		{
			byte[] dat = new byte[123 + text.Length];
			string password = DecryptionKey;
 
			for (int i = 0; i < 123; i++)
				dat[i] = 0;
			if (string.IsNullOrEmpty(password))
				for (int i = 0, j = 123; i < text.Length; i++, j++)
					dat[j] = (byte)text[i];
			else
				for (int i = 0, j = 123; i < text.Length; i++, j++)
					dat[j] = (byte)((byte)text[i] + (byte)password[i % password.Length]);
 
			File.WriteAllBytes(filepath, dat);
		}
 
		public static unsafe void EncryptUnsafe(string text, string filepath)
		{
			int len, pass, txt;
			byte[] dat = new byte[len = 123 + (txt = text.Length)];
			byte* password = stackalloc byte[pass = DecryptionKey.Length];
 
			for (int i = 0; i < pass; i++)
				password[i] = (byte)DecryptionKey[i];
 
			fixed (byte* d = dat)
			{
				for (int i = 0; i < 123; i++)
					d[i] = 0;
 
				fixed (char* t = text)
				{
					if (pass == 0)
						for (int i = 0; i < txt; i++)
							d[i + 123] = (byte)t[i];
					else
						for (int i = 0, j = 123; i < txt; i++, j++)
							d[j] = (byte)((byte)t[i] + password[i % pass]);
				}
			}
 
			File.WriteAllBytes(filepath, dat);
		}
	}
}

These work like a charm (especially unsafe ones) ;)
Why I posted it? - Cuz I'm bored and someone may find it useful in the future...
Ultimately, my constant dissatisfaction with the way things are becomes the driving force behind everything I do.
[Image: sigline.png]
LF2 IDE - Advanced visual data changer featuring instant data loader
LF2 Sprite Sheet Generator - Template based sprite sheet generator based on Gad's method
[Image: sigline.png]
There is no perfect language, but C++ is the worst.
Reply
Thanks given by: Redbeard
#2
can i run it?
Reply
Thanks given by:
#3
(09-15-2013, 05:58 AM)dan007/^"" Wrote:  can i run it?

Your question is so general and if you're asking it... Looks like you can't -_- - at least for now.
Of course you can run it. How? Look down
    CSHARP-Code:
using System;
using System.IO;
using System.Text;
using System.Globalization;
 
internal sealed class Program
{
	public static void Main(string[] args)
	{
		if(args.Length == 0)
		{
			Console.WriteLine("Error: No input defined");
			Console.ReadKey(true);
			return;
		}
 
		for(int i = 0; i < args.Length; i++)
		{
			if(Path.GetExtension(args[i]) == ".dat")
			{
				Console.ForegroundColor = ConsoleColor.Cyan;
				Console.WriteLine(i + " << " + args[i]);
				string place = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
				string dst = place + "\\" + Path.GetFileName(Path.ChangeExtension(args[i], ".txt"));
				Console.WriteLine(i + " >> " + dst);
				File.WriteAllText(dst, LF2DataUtils.DatToPlainUnsafe(args[i]), Encoding.Default);
			}
			else if(Path.GetExtension(args[i]) == ".txt")
			{
				Console.ForegroundColor = ConsoleColor.Magenta;
				Console.WriteLine(i + " << " + args[i]);
				string place = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
				string dst = place + "\\" + Path.GetFileName(Path.ChangeExtension(args[i], ".dat"));
				Console.WriteLine(i + " >> " + dst);
				LF2DataUtils.PlainToDatUnsafe(File.ReadAllText(args[i], Encoding.Default), dst);
			}
		}
		Console.ResetColor();
		Console.WriteLine("Done");
		Console.ReadKey(true);
	}
}

This little program encrypts/decrypts given command line arguments (which are usually file paths), if the extension is ".dat" file is decrypted and if the extesion is ".txt" then it is encrypted.
Before compiling it change encryptionKey and decryptionKey properties like this:
    CSHARP-Code:
public static string encryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
public static string decryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
Ultimately, my constant dissatisfaction with the way things are becomes the driving force behind everything I do.
[Image: sigline.png]
LF2 IDE - Advanced visual data changer featuring instant data loader
LF2 Sprite Sheet Generator - Template based sprite sheet generator based on Gad's method
[Image: sigline.png]
There is no perfect language, but C++ is the worst.
Reply
Thanks given by: Redbeard
#4
Thanks for sharing, Nightmare. This is exactly what I need for further developement on my project(s).

I'm gonna use something like this:
    CSHARP-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
namespace LF2
{
    class Cryption
    {
 
        public static string encryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
        public static string decryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
 
        public static void Encrypt(string text, string filepath)
        {
            byte[] dat = new byte[123 + text.Length];
            string password = decryptionKey;
 
            for (int i = 0; i < 123; i++)
                dat[i] = 0;
            if (string.IsNullOrEmpty(password))
                for (int i = 0; i < text.Length; i++)
                    dat[i + 123] = (byte)text[i];
            else
                for (int i = 0; i < text.Length; i++)
                    dat[i + 123] = (byte)((byte)text[i] + (byte)password[i % password.Length]);
 
            File.WriteAllBytes(filepath, dat);
        }
 
        public static string Decrypt(string filepath)
        {
            byte[] buffer = File.ReadAllBytes(filepath);
            byte[] decryptedtext = new byte[buffer.Length];
            string password = encryptionKey;
 
            if (string.IsNullOrEmpty(password)) return Encoding.Default.GetString(buffer);
 
            for (int i = 123, j = 0; i < buffer.Length; i++, j++)
                decryptedtext[j] = (byte)(buffer[i] - (byte)password[j % password.Length]);
 
            return Encoding.Default.GetString(decryptedtext);
        }
 
    }
}


Check this awesome 16bit art out! www.effectgames.com/demos/canvascycle/(not mine)

Find my project here! it downloads and installs chars for you - automatically!
(not finished yet, try out the Alpha version and help out by reporting bugs and make suggestions)
Latest version: Alpha 1.2

Reply
Thanks given by:
#5
(09-17-2013, 06:21 PM)Redbeard Wrote:  Thanks for sharing, Nightmare. This is exactly what I need for further developement on my project(s).

It's my pleasure. Take my advice and use unsafe ones, they are completely safe :D Don't afraid of using unsafe code, they are fast.
Ultimately, my constant dissatisfaction with the way things are becomes the driving force behind everything I do.
[Image: sigline.png]
LF2 IDE - Advanced visual data changer featuring instant data loader
LF2 Sprite Sheet Generator - Template based sprite sheet generator based on Gad's method
[Image: sigline.png]
There is no perfect language, but C++ is the worst.
Reply
Thanks given by:
#6
havent started using csharp just using C now a days
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)