09-14-2013, 08:57 PM
(This post was last modified: 07-31-2015, 07:59 PM by NightmareX1337.)
Here is my cool DCing Algorithm for those who like to play with pointers and like the power of unsafe code:
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...
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]](http://www.lf-empire.de/forum/images/unrealblack/english/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]](http://www.lf-empire.de/forum/images/unrealblack/english/sigline.png)
There is no perfect language, but C++ is the worst.
![[Image: sigline.png]](http://www.lf-empire.de/forum/images/unrealblack/english/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]](http://www.lf-empire.de/forum/images/unrealblack/english/sigline.png)
There is no perfect language, but C++ is the worst.