trying to write a decryption program

kurt69

Well-known member
Joined
Jan 17, 2006
Messages
78
Location
Australia
Programming Experience
Beginner
Hi, I'm trying to write a decryption program. Firstly I'll tell you how the encryption works; in the beginning of the file there is 123 useless bytes before the actual data. Now each encrypted character is created by adding the sum of the unencrypted character and the matching character in the key (which is a string). The character that matches depends on what the characters position is in the line.
EG: If it is the first character in that line then it is added to the first character of the key string, if it is the 2nd then it is added too the 2nd character of the key string. If the end of the key string is reached and the last character is used, then it goes back to the 1st character of the key string.

So please help me, So far this is the closest I can get to my goal; (I haven't tested this yet)

VB.NET:
Public Sub crypt(ByVal strmR As StreamReader)
        Dim i As Integer = 0
        For i = 0 To 123
            strmR.BaseStream.ReadByte()
        Next
        Dim buf() As Integer
        Dim j As Integer = 0
        Dim prev
        While strmR.Peek <> -1
            prev = strmR.BaseStream.Position
            For i = 0 To strmR.ReadLine.Length
                strmR.BaseStream.Position = prev
                buf(i) = strmR.Read()
                If j < key.Length Then
                    j += 1
                Else
                    j = 0
                    j += 1
                End If
                buf(i) += j
                prev = strmR.BaseStream.Position
                strmR.ReadLine()
            Next
        End While
    End Sub
 
Looking at the sample file that you gave me, I don't think that it is formatted in any standard way. It is XML-Like, but doesn't look like it would be parsed like XML.

I could write something that would parse this file, but I have no idea what the requirements are. Do you need all the data in the file, or are you just counting the Frame elements and file(x-y) attributes?

I would need context on how this file would be used, to actually do anything.

Even with that information, the file is so weirdly constructed it would be very ugly coding to convert this file into an object.

Rob
 
Looking at the sample file that you gave me, I don't think that it is formatted in any standard way. It is XML-Like, but doesn't look like it would be parsed like XML.

I could write something that would parse this file, but I have no idea what the requirements are. Do you need all the data in the file, or are you just counting the Frame elements and file(x-y) attributes?

I would need context on how this file would be used, to actually do anything.

Even with that information, the file is so weirdly constructed it would be very ugly coding to convert this file into an object.

Rob

Ok, all the data in between the <bmp_begin> and <bmp_end> needs to parsed first, then each frame individually, 1 after the other.

A <frame> parsing function would be good, where I just pass it the frame number I wish too parse and it returns all the data the frame had.
 
Ok here is the code to parse out this data.

Use it like this

VB.NET:
Dim newCharacter As New Character("C:\Decrypted.txt")

I tested this with the decrypted file you attached earlier.

I'm not totally happy with this code, but with the format of this file it is difficult to be happy with anything.

Any let me know how it works out.
 

Attachments

  • Code.txt
    14.5 KB · Views: 23
Thank you very much hauptra, if I could give you more 1000 reputation I would.

PS I've only skimmed the surface of the code, I'll let you know soon how I went with it, thanks again.

edit: Ok, I'm a bit lost in your code here, sorry. I'm trying to display the correct image for frame 0, I know that to do that I need to read the frame's "pic:" value, then get the correct file to use.

Now honestly, I have no idea what I need to do to even get frame 0, let alone the rest, do you think you could provide a quick example snippet of how to get frame 0 then read the value of pic?
 
Last edited:
VB.NET:
        Dim newCharacter As New Character("C:\Decrypted.txt")

        Try
            Dim newFrame As Character.Frame = newCharacter.GetFrame(0)
            Dim intPic As Integer = newFrame.IntValue("pic")
            MsgBox("Pic #: " & intPic)
        Catch ex As Character.BadData
            MsgBox("Error")
        End Try

So this will give you the Pic # for frame 0. Here is a breakdown line-by-line

  1. Read the data from the file
  2. Try statement
  3. Get Frame 0
  4. Get Integer Value for pic
  5. print the pic
  6. Catch the exception of BadData
  7. Print Error
  8. End Try

So if you wanted frame 1 you'd say
VB.NET:
newCharacter.GetFrame(1)

Inside the frame are 3 Dictionary objects. Two for Values (Integer and String) and another for Sub attributes. As you probably noticed the Frames have Integer and String values for properties. So I try to parse for Integers first, if that fails then I put it in as a string.

The original code I gave you was missing the property to retrieve the Sub Attribute. Attached is a fixed version.

All of these use their name as the key. I would've used 'standing' for frame 0, but that is used multiple times.

Also it is important to use the try statement. If you ask for an item in any dictionary object and it doesn't exist it will throw that exception. I did this because returning items for keys that don't exist as integers will likely return 0 instead of Nothing. This will lead to confusion.

Let me know if you need more help.
 

Attachments

  • Code.txt
    14.8 KB · Views: 17
Ahhhhhhhhh that's what I was doing wrong. I kept doing this:
Dim newFram As New Character.Frame() {I forget what I put in the brackets}. I also kept adding the ":" when getting intvalue. I was close.

The other thing is that I added
VB.NET:
Public ReadOnly Property FileCollection() As Dictionary(Of Integer, File)
        Get
            Return _Files
        End Get
    End Property

So I could do this:
VB.NET:
            newFrame = character.GetFrame(50)
            intPic = newFrame.IntValue("pic")
            For Each newFile In character.FileCollection.Values
                If newFile.Low <= intPic And newFile.High >= intPic Then
                    path = newFile.FileName()
                End If
            Next

Anyway thanks, this is a great help!
 
Last edited:
Feel free to modify my code however you want. But I will say that you are breaking encapsulation by using the property that you created. By using the property function to directly return a dictionary class you are giving access to all the methods, functions, and properties of that class.

Observe the following behavior using your new property.

VB.NET:
MsgBox(newCharacter.FileCollection.Count)
newCharacter.FileCollection.Clear()
MsgBox(newCharacter.FileCollection.Count)

This clears the contents of the _Files dictionary object. It isn't your intention since you declared the property as readonly, but that is not how readonly works.

I would suggest that instead you do the following

VB.NET:
    Public ReadOnly Property FileCol() As Dictionary(Of Integer, File).ValueCollection
        Get
            Return _Files.Values
        End Get
    End Property

Then use the following

VB.NET:
newFrame = Character.GetFrame(50)
            intPic = newFrame.IntValue("pic")
            For Each newFile As Character.File In newCharacter.FileCol
                If newFile.Low <= intPic And newFile.High >= intPic Then
                    path = newFile.FileName()
                End If
            Next

I don't think that this breaks encapsulation.
 
Will do, I gave access to all of the dictionary on purpose, just so I could see if there was anything else I would need to expose. Thanks hauptra.

1 thing though, could you open up the encrypted sample I sent. (bat.dat) and get frame 0's data. The values are working except for anything past hit_d, for instance hit_j's value becomes "0 hit_Fa", nothing after that is recorded.

pic: 0 state: 0 wait: 5 next: 1 dvx: 0 dvy: 0 dvz: 0 centerx: 39 centery: 79 hit_a: 0 hit_d: 0 hit_j: 0 hit_Fa: 240 hit_Fj: 270 hit_Uj: 260

I'm sure it would be because hit_a, hit_d, have double spaces after them while hit_j and the rest only have a single space? Sorry to bother you again.
 
ugh. Yeah that is a problem. You best option now is to just walk through the string in this case with the following algorithm

While StrLine isn't empty
read property name
exclude colon
read property value
store
End While

The other option is using Split with the colon. Then you'd have to take the first index as the name and the first part of the next index as the value and the second part as the name for the next property.

Either way it sucks.

Let me know if you need help modifying my code.
 
Hey I just had a thought, would replacing all the double whitespaces programmatically with just a single whitespace before parsing and then altering your parsing methods to accomodate that work?
 
Back
Top