Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DCing algorithm
#11
(08-01-2012, 10:45 AM)Kevin Wrote:  DAMN YOU ALL genius people
or more like DAMN YOU java (for me it's a 'language from planet')

I've been trying many things but none of them works :(

Please try to make this noob understand how to encode/decode dat files. I'd like to start from how to convert the encoded version into a readable version. I'm learning from Silva's code, but also read BP's to help me understand. but....

1. Count to 123 first - does that mean the first 123 characters are unused? Or... I don't have any idea, probably this is what makes me fail coz I don't even have any idea what else to try about this
2. b=(byte)finp.read(); --> I can't find this on my dictionary. What does this mean in this planet's language?
3. d = DecryptByte(counta,b); ---> does DecryptByte give you the KeyAscii (keycode or sth) from the character? But if so, I don't understand why there must be the variable 'b' behind it.
4. System.out.print((char)d); ---> does this prints the character stored in variable 'd' ? which is the result of DecryptByte(counta,b) ? But that's strange, I can't see where you modify / encode the byte :0 .

And damn you gravedigging warning :( . And damn me :( .
and thanks for replying and helping :D !!

1. Yes. The first 123 bytes aren't used.

2. It just reads one byte from the file.

3. It calls the DecryptByte method, if you scroll down you'll see it.

Code:
public static byte DecryptByte(int counta,byte b){
         String plainEncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
         b -= (byte)plainEncryptionKey.charAt(counta);
         return b;
      }

"counta" (which is a retarded name for a variable) is the place we are currently at in the key. It goes from 0 to 35(as soon as we reach the end of the key, we go back to the start). b is the byte we read from the file. We take the value we read from the file and subtract the value of the key.

4. Yes, it is the result of DecryptByte. DecryptByte IS the encoding method.

What language are you trying to do it in? I can help you if it's something C-ish (C/PHP/Java/C#/Go) or Python.
[Image: doty7Xn.gif]

10 ʏᴇᴀʀs sɪɴᴄᴇ ɪʀᴄ ɢᴏᴏᴅ.ɪ ᴡᴀʟᴋ ᴛʜʀᴏᴜɢʜ ᴛʜᴇ ᴇᴍᴘᴛʏ sᴛʀᴇᴇᴛs ᴛʀʏɪɴɢ ᴛᴏ ᴛʜɪɴᴋ ᴏғ sᴏᴍᴇᴛʜɪɴɢ ᴇʟsᴇ ʙᴜᴛ ᴍʏ ᴘᴀᴛʜ ᴀʟᴡᴀʏs ʟᴇᴀᴅs ᴛᴏ ᴛʜᴇ ɪʀᴄ. ɪ sᴛᴀʀᴇ ᴀᴛ ᴛʜᴇ sᴄʀᴇᴇɴ ғᴏʀ ʜᴏᴜʀs ᴀɴᴅ ᴛʀʏ ᴛᴏ sᴜᴍᴍᴏɴ ᴛʜᴇ ɢᴏᴏᴅ ɪʀᴄ. ɪ ᴡᴀᴛᴄʜ ᴏᴛʜᴇʀ ɪʀᴄ ᴄʜᴀɴɴᴇʟs ʙᴜᴛ ɪᴛ ɪs ɴᴏ ɢᴏᴏᴅ. ɪ ᴘᴇsᴛᴇʀ ᴢᴏʀᴛ ᴀɴᴅ ᴛʀʏ ᴛᴏ ʀᴇsɪsᴛ ʜɪs sᴇxɪɴᴇss ʙᴜᴛ ɪᴛ ɪs ᴀʟʟ ᴍᴇᴀɴɪɴɢʟᴇss. ᴛʜᴇ ᴇɴᴅ ɪs ɴᴇᴀʀ.ɪ ᴛʜᴇɴ ᴜsᴜᴀʟʟʏ ʀᴇᴀᴅ sᴏᴍᴇ ᴏʟᴅ ɪʀᴄ ʟᴏɢs ᴀɴᴅ ᴄʀʏ ᴍʏsᴇʟғ ᴛᴏ sʟᴇᴇᴘ.


Reply
Thanks given by:
#12
(01-14-2009, 08:23 PM)Lord Silva Wrote:  
Code:
/*Programmed by Silva
Stolen stuff from Blue Phoenix
Latest version: 14 Jan 2009
*/

   import java.io.*;

    public class ReadFile{
       public static void main(String[] args) throws IOException{
         File f;
         f=new File("template.dat");// file rawr
          
         if(!f.exists()&& f.length()<0)
            System.out.println("The specified file is not exist");
        
         else{
            FileInputStream finp=new FileInputStream(f);
            int counta = 0;
            int countTo123 = 0;
            byte b;
            byte d;
            do{
                countTo123++;
                b = (byte)finp.Read();
                if (countTo123 > 123)
                {

                    d = DecryptByte(counta, b);
                    counta++;
                    if (counta > 36)
                    {
                        counta = 0;
                    }
                    text += (char)d;
                }
            }
            while(b!=-1);
            finp.close();
         }
      }
      
       public static byte DecryptByte(int counta,byte b){
         String plainEncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
         b -= (byte)plainEncryptionKey.charAt(counta);
         return b;
      }
   }

My version :) (works faster because it uses streams ;) ).

C# version that only returns gibberish for me on both b+ and b-, any idea why?

Code:
public class ReadFile
    {
        public static string main(String FileName)
        {
            string text = "";
            StreamReader finp = new StreamReader(FileName);
            int counta = 0;
            int countTo123 = 0;
            byte b;
            byte d;
            while (finp.Peek() >= 0)
            {
                countTo123++;
                b = (byte)finp.Read();
                if (countTo123 > 123)
                {

                    d = DecryptByte(counta, 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;
        }
    }
Reply
Thanks given by:
#13
In
Code:
if (countTo123 > 123)
Doesn't this mean you're starting from index 124 (which is the 125th character) instead of 123?

p.s: it's "b+="

A python variation I wrote sometime ago:
    PYTHON-Code:
def decrypt_lf2(edata):
    key="odBearBecauseHeIsVeryGoodSiuHungIsAGo"
    pos=0
    ddata=''
    for a in range(123, len(edata)-1):
        try:
            ddata+=chr(ord(edata[a])-ord(key[pos]))
        except ValueError:
            pass
        pos+=1
        if pos==len(key):
            pos=0
    return ddata
 
def encrypt_lf2(ddata):
    key="odBearBecauseHeIsVeryGoodSiuHungIsAGo"
    pos=0
    edata=''
    for a in range(0, len(ddata)-1):
        edata+=chr(ord(ddata[a])+ord(key[pos]))
        pos+=1
        if pos==len(key):
            pos=0
    return (" "*123)+edata


Edit: Would be cool to try doing a one liner with lambdas.
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by:
#14
(03-26-2015, 08:11 AM)Doctor A Wrote:  Doesn't this mean you're starting from index 124 (which is the 125th character) instead of 123?
Yeah, changing to <= still returns gibberish though (and a few numbers up/down both directions).

(03-26-2015, 08:11 AM)Doctor A Wrote:  p.s: it's "b+="
+ certainly consists of more discernible characters than -

(03-26-2015, 08:11 AM)Doctor A Wrote:  Edit: Would be cool to try doing a one liner with lambdas.
blergh
Reply
Thanks given by:
#15
(03-26-2015, 07:58 AM)YinYin Wrote:  
    CSHARP-Code:
public class ReadFile
    {
        public static string main(String FileName)
        {
            string text = "";
            StreamReader finp = new StreamReader(FileName);
            int counta = 0;
            int countTo123 = 0;
            byte b;
            byte d;
            while (finp.Peek() >= 0) 
            {
                countTo123++;
                b = (byte)finp.Read();
                if (countTo123 > 123)
                {
 
                    d = DecryptByte(counta, 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;
        }
    }
Your code is wrong.
StreamReader.Read() returns the next character read, which is not necessarily the same as the next byte. Using a FileStream instead of StreamReader solves this as it has a ReadByte() method, which returns the next byte or -1 if the end-of-file has been reached.
I'll also point out that calling both Peek() and Read() is both redundant and slow, and the same thing goes for reading the first 123 characters only to not do anything with them. FileStream.Seek() is a better solution.
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;
    }
}

I would also like to add than C♯ is a dumb language, and that C++ is way better.
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
Gaming isn't hostile to women. It is hostile to easily offended idiots.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
LFE Red Theme: 85%
Reply
Thanks given by: YinYin
#16
Thank you so much :D
(03-26-2015, 12:40 PM)Someone else Wrote:  Your code is wrong. [...] calling both Peek() and Read() is both redundant and slow
Damn you msdn docs, damn you!
I just guessed my casual stream reader would work the same, which it obviously didn't. And upon discovering it doesn't return -1 on end I looked up how to use it and the samples all had peek for the while condition.

(03-26-2015, 12:40 PM)Someone else Wrote:  I would also like to add than C♯ is a dumb language, and that C++ is way better.
Well aware. This is just what I'm working with most of the time now and easiest to dive into coming from java. Also I intend not to get my hands dirty on crazy hacks.
Reply
Thanks given by:
#17
(03-26-2015, 08:31 AM)YinYin Wrote:  blergh
Did it anyways :P
    PYTHON-Code:
def dlf2(e,d='',a=123,p=0,k="odBearBecauseHeIsVeryGoodSiuHungIsAGo"): return (lambda:d, (lambda:dlf2(e, d+chr(ord(e[a])-ord(k[p])), a+1, p+1),lambda:dlf2(e, d+chr(ord(e[a])-ord(k[0])), a+1, 1))[int(p == len(k))])[a != len(e)]()


I personally don't like C# either, but it proves to be really useful for when you'd like to do something in a hurry.
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by:
#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 , Someone else , YinYin
#19
(06-19-2015, 11:57 AM)Dia6lo Wrote:  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:
Here is comparison to Someone else's code(on davis.dat):
Code:
My program: 9 ms
Someone else's program: 6297 ms
That is a pretty good speedup, and yes I am not particularly good with C# as I greatly prefer C++. I just copied YinYin's code and fixed a few bugs in his code.

(06-19-2015, 11:57 AM)Dia6lo Wrote:  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).
Are you sure that the code opens the file every byte?
I do not know much about C#, but I would imagine that it opens the file once, and then reads every byte separately, which is obviously still causes a major slowdown, but not nearly the same as opening the file each time a byte is read.

Good job on improving the code.

@below: Interesting.
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
Gaming isn't hostile to women. It is hostile to easily offended idiots.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
LFE Red Theme: 85%
Reply
Thanks given by:
#20
(06-19-2015, 05:14 PM)Someone else Wrote:  Are you sure that the code opens the file every byte?
I do not know much about C#, but I would imagine that it opens the file once, and then reads every byte separately, which is obviously still causes a major slowdown, but not nearly the same as opening the file each time a byte is read.

Good job on improving the code.

Thanks. AFAIK opening files is more expensive(in performance terms) operation than reading data from opened ones. And FileStream just gives you interface to operate with given file(and doesn't read any data from it). So, everytime by calling ReadByte() you ask computer to open file and read a single byte, while File.ReadAllBytes() opens file once and reads all data in one go. While first method is memory-friendly and usually used to read small amounts of data, latter is more fast in reading large chunks of data.
OFC I've done other optimisations(by using LINQ and Enumerators), but ReadByte() caused main slowdown.
Reply
Thanks given by: Someone else




Users browsing this thread: 1 Guest(s)