Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DCing algorithm
#18
(03-26-2015, 12:40 PM)Someone else Wrote:  My version of the code:
    CSHARP-Code:
public class ReadFile
{
    public static string main(String FileName)
    {
        string text = "";
        FileStream finp = new FileStream(FileName,FileMode.Open);
        int counta = 0;
        int b;
        byte d;
        finp.Seek(123,SeekOrigin.Begin);
        while((b = finp.ReadByte()) != -1)
        {
            d = DecryptByte(counta, (byte)b);
            counta++;
            if (counta > 36)
            {
                counta = 0;
            }
            text += (char)d;
        }
        finp.Close();
        return text;
    }
 
    public static byte DecryptByte(int counta, byte b)
    {
        String plainEncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
        b -= (byte)plainEncryptionKey[counta];
        return b;
    }
}

Okay, guys, it seems that you just can't give up on LF ;). And I also noticed that you need some helping hand with C#, so I wrote a much faster program:
    CSHARP-Code:
static class DatDecryptor
    {
        static IEnumerator<char> EncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo".GetEnumerator();
 
        static void Main(string[] args)
        {
            var fileName = Console.ReadLine();
            if (fileName == null || !File.Exists(fileName)) return;
            // First 123 bytes of lf2 .dat files are useless
            var bytes = File.ReadAllBytes(fileName).Skip(123);
            var text = DecryptByteSequence(bytes);
            // Put here your code that outputs decrypted text to wherever you want
        }
 
        static string DecryptByteSequence(IEnumerable<byte> byteStream)
        {
            EncryptionKey.Reset();
            return new string(byteStream.Select(DecryptByte).ToArray());
        }
 
        static Func<byte, char> DecryptByte = b => (char)(b - NextEncryptionByte());
 
        static byte NextEncryptionByte()
        {
            if (!EncryptionKey.MoveNext())
            {
                EncryptionKey.Reset();
                EncryptionKey.MoveNext();
            }
            return (byte)EncryptionKey.Current;
        }
    }


Here is comparison to Someone else's code(on davis.dat):
Code:
My program: 9 ms
Someone else's program: 6297 ms

As you can see, perfomance skyrockets by almost x700 times(mainly because you open file every time you read a single byte, while I do it only once). Yes, this code contains no dirty hacks and it doesn't use any third-party libraries, just System:
    CSHARP-Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


P.S: One can say that I should parse filename from args, but I don't think it's a big deal to do it so I omitted it.
Reply
Thanks given by: Ariyan , Som1Lse , YinYin


Messages In This Thread
DCing algorithm - by Silverthorn - 01-14-2009, 07:33 PM
RE: DCing algorithm - by Boop - 01-14-2009, 08:23 PM
RE: DCing algorithm - by YinYin - 03-26-2015, 07:58 AM
RE: DCing algorithm - by Som1Lse - 03-26-2015, 12:40 PM
RE: DCing algorithm - by YinYin - 03-26-2015, 12:57 PM
RE: DCing algorithm - by Silverthorn - 01-14-2009, 08:26 PM
RE: DCing algorithm - by Azriel - 01-15-2009, 06:09 PM
RE: DCing algorithm - by Boop - 01-15-2009, 06:29 PM
RE: DCing algorithm - by 1477 - 01-15-2009, 09:50 PM
RE: DCing algorithm - by Boop - 01-15-2009, 11:09 PM
RE: DCing algorithm - by Silverthorn - 01-16-2009, 03:06 PM
RE: DCing algorithm - by Yakui - 01-18-2009, 09:19 PM
RE: DCing algorithm - by Kevin - 08-01-2012, 10:45 AM
RE: DCing algorithm - by Boop - 08-01-2012, 11:53 AM
RE: DCing algorithm - by A-Man - 03-26-2015, 08:11 AM
RE: DCing algorithm - by YinYin - 03-26-2015, 08:31 AM
RE: DCing algorithm - by A-Man - 03-26-2015, 02:14 PM
RE: DCing algorithm - by Dia6lo - 06-19-2015, 11:57 AM
RE: DCing algorithm - by Som1Lse - 06-19-2015, 05:14 PM
RE: DCing algorithm - by Dia6lo - 06-19-2015, 05:37 PM
RE: DCing algorithm - by Hellblazer - 06-21-2015, 10:33 AM
RE: DCing algorithm - by Zelphir - 06-13-2021, 10:38 AM
RE: DCing algorithm - by Mesujin - 08-30-2022, 03:47 PM



Users browsing this thread: 1 Guest(s)