(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 msAs 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.

Chat
