Jump to content

Recommended Posts

Posted (edited)

How do I convert *.pkx to *.ekx, x being 4, 5, 6, or 7? I'm trying to write a program in Visual Basic, and I need it to be able to encrypt *.pkx files into *.ekx files. The problem is I can't figure out how.

Edited by Regnum
Posted
1 hour ago, Regnum said:

How do I convert *.pkx to *.ekx, x being 4, 5, 6, or 7? I'm trying to write a program in Visual Basic, and I need it to be able to encrypt *.pkx files into *.ekx files. The problem is I can't figure out how.

Gen IV and V
Read up on PKM structure, and see checksum.


And how the system got minor updates in Gen VI and VII

 

Posted
35 minutes ago, theSLAYER said:

Gen IV and V
Read up on PKM structure, and see checksum.


And how the system got minor updates in Gen VI and VII

 

I had looked at those, it’s that I don’t know how to apply the formulas written there.

Posted
4 minutes ago, Regnum said:

I had looked at those, it’s that I don’t know how to apply the formulas written there.

The shuffling order formula is basically:
dim order as integer = (pid >> (13) And 31) Mod 24

and the horrible implementation for order shuffling I did a long time ago:

Spoiler

Select Case order
            Case 0
                firstblock = "A"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "D"
            Case 1
                firstblock = "A"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "C"
            Case 2
                firstblock = "A"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "D"
            Case 3
                firstblock = "A"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "C"
            Case 4
                firstblock = "A"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "B"
            Case 5
                firstblock = "A"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "B"
            Case 6
                firstblock = "B"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "D"
            Case 7
                firstblock = "B"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "C"
            Case 8
                firstblock = "C"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "D"
            Case 9
                firstblock = "D"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "C"
            Case 10
                firstblock = "C"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "B"
            Case 11
                firstblock = "D"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "B"
            Case 12
                firstblock = "B"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "D"
            Case 13
                firstblock = "B"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "C"
            Case 14
                firstblock = "C"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "D"
            Case 15
                firstblock = "D"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "C"
            Case 16
                firstblock = "C"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "B"
            Case 17
                firstblock = "D"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "B"
            Case 18
                firstblock = "B"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "A"
            Case 19
                firstblock = "B"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "A"
            Case 20
                firstblock = "C"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "A"
            Case 21
                firstblock = "D"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "A"
            Case 22
                firstblock = "C"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "A"
            Case 23
                firstblock = "D"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "A"
        End Select

 

 

Posted
1 hour ago, theSLAYER said:

The shuffling order formula is basically:
dim order as integer = (pid >> (13) And 31) Mod 24

and the horrible implementation for order shuffling I did a long time ago:

  Reveal hidden contents


Select Case order
            Case 0
                firstblock = "A"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "D"
            Case 1
                firstblock = "A"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "C"
            Case 2
                firstblock = "A"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "D"
            Case 3
                firstblock = "A"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "C"
            Case 4
                firstblock = "A"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "B"
            Case 5
                firstblock = "A"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "B"
            Case 6
                firstblock = "B"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "D"
            Case 7
                firstblock = "B"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "C"
            Case 8
                firstblock = "C"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "D"
            Case 9
                firstblock = "D"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "C"
            Case 10
                firstblock = "C"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "B"
            Case 11
                firstblock = "D"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "B"
            Case 12
                firstblock = "B"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "D"
            Case 13
                firstblock = "B"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "C"
            Case 14
                firstblock = "C"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "D"
            Case 15
                firstblock = "D"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "C"
            Case 16
                firstblock = "C"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "B"
            Case 17
                firstblock = "D"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "B"
            Case 18
                firstblock = "B"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "A"
            Case 19
                firstblock = "B"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "A"
            Case 20
                firstblock = "C"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "A"
            Case 21
                firstblock = "D"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "A"
            Case 22
                firstblock = "C"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "A"
            Case 23
                firstblock = "D"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "A"
        End Select

 

 

Thanks.

Now, how do I use the formula, X[n+1] = (0x41C64E6D * X[n] + 0x6073), what do I put for X[n] and X[n+1]?

Posted
18 minutes ago, Regnum said:

Thanks.

Now, how do I use the formula, X[n+1] = (0x41C64E6D * X[n] + 0x6073), what do I put for X[n] and X[n+1]?

These are the functions I have relating to rand and the seed generation:

Spoiler

Public seed As UInt32

Public Sub srnd(ByVal newSeed As UInt32)
        seed = newSeed
End Sub

Public Function rand() As UInt32
        seed = (((&H41C64E6D * seed) + &H6073)) And &HFFFFFFFF
        Return seed >> 16
End Function

 


So at every instance that you block xor rand(), the rand will give the updated random number as per the game's RNG.

I'm pretty sure I got help for this a long time ago, but I can't remember from who.
In any case, that is how it is done :)

(edit: I think I use the srnd function relating to checksum)

Posted
6 minutes ago, theSLAYER said:

These are the functions I have relating to rand and the seed generation:

  Reveal hidden contents


Public seed As UInt32

Public Sub srnd(ByVal newSeed As UInt32)
        seed = newSeed
End Sub

Public Function rand() As UInt32
        seed = (((&H41C64E6D * seed) + &H6073)) And &HFFFFFFFF
        Return seed >> 16
End Function

 


So at every instance that you block xor rand(), the rand will give the updated random number as per the game's RNG.

I'm pretty sure I got help for this a long time ago, but I can't remember from who.
In any case, that is how it is done :)

(edit: I think I use the srnd function relating to checksum)

Is seed the checksum?

Posted
17 minutes ago, Regnum said:

So, where does the checksum come in?

here, Lemme show you:

Spoiler


    Public Function encrypt(ByVal pkm() As Byte)
      	'variables here
		'stuff happens here	
      	
      	'such as Shuffle the blocks using the block shuffling algorithm above. 
      
      	'Seed the PRNG with the checksum (let X[n] be the checksum), 
        srnd(checksum)
        
      	'finally, Sequentially, for each 2-byte word Y from 0x08 to 0x87, apply the transformation: unencryptedByte = Y xor rand() 
		'example:    

        Select Case firstblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
          
      	'remainder of code
        Return pkm
    End Function

 


so srnd(checksum) uses checksum as seed.
Rand() will call a new value.
and remainder rand()s in the function doesn't need to be reseeded using the checksum.

edit:
so what I'm trying to say is that the variable seed isn't always the checksum.
You only seed the checksum upon initial encryption of that particular Pokemon (for it's first block),
and it new result will be reused to seed the RNG for the remainder 3 blocks.

 

I can give you my full function to refer to if needed.

Posted
15 minutes ago, theSLAYER said:

here, Lemme show you:

 

  Hide contents



    Public Function encrypt(ByVal pkm() As Byte)
      	'variables here
		'stuff happens here	
      	
      	'such as Shuffle the blocks using the block shuffling algorithm above. 
      
      	'Seed the PRNG with the checksum (let X[n] be the checksum), 
        srnd(checksum)
        
      	'finally, Sequentially, for each 2-byte word Y from 0x08 to 0x87, apply the transformation: unencryptedByte = Y xor rand() 
		'example:    

        Select Case firstblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
          
      	'remainder of code
        Return pkm
    End Function

 

 

So like this:

 

'block shuffling stuff

srnd(chksum)
rand()

 

Posted
1 minute ago, Regnum said:

So like this:

 


'block shuffling stuff

srnd(chksum)
rand()

 

That's the main gist. Seed the RNG first with checksum srnd(checksum), then apply transformation byte xor rand.

When you read the wiki, it says
" Sequentially, for each 2-byte word Y from 0x08 to 0x87, apply the transformation "

So you're not just rand() once, you'll have to rand() for every byte you xor against.

Posted

@Regnum

This is my encrypt code.
Since this was made years back, there's probably definitely better ways to optimize the code,
so you can keep that in mind if you're gonna adapt it.

 

Spoiler

 



    Public Function encrypt(ByVal pkm() As Byte)
        Dim pid As UInt32 = 0
        Dim checksum As UInt16 = 0
        pid = BitConverter.ToUInt32(pkm, 0)
        checksum = BitConverter.ToUInt16(pkm, 6)
        Dim order As Integer = (pid >> (13) And 31) Mod 24
        Dim firstblock As String
        Dim secondblock As String
        Dim thirdblock As String
        Dim fourthblock As String

        firstblock = 0
        secondblock = 0
        thirdblock = 0
        fourthblock = 0
        Select Case order
            Case 0
                firstblock = "A"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "D"
            Case 1
                firstblock = "A"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "C"
            Case 2
                firstblock = "A"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "D"
            Case 3
                firstblock = "A"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "B"
            Case 4
                firstblock = "A"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "C"
            Case 5
                firstblock = "A"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "B"
            Case 6
                firstblock = "B"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "D"
            Case 7
                firstblock = "B"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "C"
            Case 8
                firstblock = "B"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "D"
            Case 9
                firstblock = "B"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "A"
            Case 10
                firstblock = "B"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "C"
            Case 11
                firstblock = "B"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "A"
            Case 12
                firstblock = "C"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "D"
            Case 13
                firstblock = "C"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "B"
            Case 14
                firstblock = "C"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "D"
            Case 15
                firstblock = "C"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "A"
            Case 16
                firstblock = "C"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "B"
            Case 17
                firstblock = "C"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "A"
            Case 18
                firstblock = "D"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "C"
            Case 19
                firstblock = "D"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "B"
            Case 20
                firstblock = "D"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "C"
            Case 21
                firstblock = "D"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "A"
            Case 22
                firstblock = "D"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "B"
            Case 23
                firstblock = "D"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "A"
        End Select
        Dim z As Integer = 0
        Dim v As Integer = 8
        'Block A
        Dim blocka(16) As UInt16
        While z < 16
            blocka(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 40
        'Block B
        Dim blockb(16) As UInt16
        While z < 16
            blockb(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        'Block C
        v = 72
        Dim blockc(16) As UInt16
        While z < 16
            blockc(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        'Block D
        Dim blockd(16) As UInt16
        v = 104
        While z < 16
            blockd(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        srnd(checksum)
        Dim byter(16) As UInt16
        z = 0
        v = 8

        Select Case firstblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        v = 8
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 40
        Select Case secondblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 72
        Select Case thirdblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 104
        Select Case fourthblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        Return pkm
    End Function

 

 

Posted
12 hours ago, theSLAYER said:

@Regnum

This is my encrypt code.
Since this was made years back, there's probably definitely better ways to optimize the code,
so you can keep that in mind if you're gonna adapt it.

 

  Reveal hidden contents

 




    Public Function encrypt(ByVal pkm() As Byte)
        Dim pid As UInt32 = 0
        Dim checksum As UInt16 = 0
        pid = BitConverter.ToUInt32(pkm, 0)
        checksum = BitConverter.ToUInt16(pkm, 6)
        Dim order As Integer = (pid >> (13) And 31) Mod 24
        Dim firstblock As String
        Dim secondblock As String
        Dim thirdblock As String
        Dim fourthblock As String

        firstblock = 0
        secondblock = 0
        thirdblock = 0
        fourthblock = 0
        Select Case order
            Case 0
                firstblock = "A"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "D"
            Case 1
                firstblock = "A"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "C"
            Case 2
                firstblock = "A"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "D"
            Case 3
                firstblock = "A"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "B"
            Case 4
                firstblock = "A"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "C"
            Case 5
                firstblock = "A"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "B"
            Case 6
                firstblock = "B"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "D"
            Case 7
                firstblock = "B"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "C"
            Case 8
                firstblock = "B"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "D"
            Case 9
                firstblock = "B"
                secondblock = "C"
                thirdblock = "D"
                fourthblock = "A"
            Case 10
                firstblock = "B"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "C"
            Case 11
                firstblock = "B"
                secondblock = "D"
                thirdblock = "C"
                fourthblock = "A"
            Case 12
                firstblock = "C"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "D"
            Case 13
                firstblock = "C"
                secondblock = "A"
                thirdblock = "D"
                fourthblock = "B"
            Case 14
                firstblock = "C"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "D"
            Case 15
                firstblock = "C"
                secondblock = "B"
                thirdblock = "D"
                fourthblock = "A"
            Case 16
                firstblock = "C"
                secondblock = "D"
                thirdblock = "A"
                fourthblock = "B"
            Case 17
                firstblock = "C"
                secondblock = "D"
                thirdblock = "B"
                fourthblock = "A"
            Case 18
                firstblock = "D"
                secondblock = "A"
                thirdblock = "B"
                fourthblock = "C"
            Case 19
                firstblock = "D"
                secondblock = "A"
                thirdblock = "C"
                fourthblock = "B"
            Case 20
                firstblock = "D"
                secondblock = "B"
                thirdblock = "A"
                fourthblock = "C"
            Case 21
                firstblock = "D"
                secondblock = "B"
                thirdblock = "C"
                fourthblock = "A"
            Case 22
                firstblock = "D"
                secondblock = "C"
                thirdblock = "A"
                fourthblock = "B"
            Case 23
                firstblock = "D"
                secondblock = "C"
                thirdblock = "B"
                fourthblock = "A"
        End Select
        Dim z As Integer = 0
        Dim v As Integer = 8
        'Block A
        Dim blocka(16) As UInt16
        While z < 16
            blocka(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 40
        'Block B
        Dim blockb(16) As UInt16
        While z < 16
            blockb(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        'Block C
        v = 72
        Dim blockc(16) As UInt16
        While z < 16
            blockc(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        'Block D
        Dim blockd(16) As UInt16
        v = 104
        While z < 16
            blockd(z) = BitConverter.ToUInt16(pkm, v)
            z = z + 1
            v = v + 2
        End While
        z = 0
        srnd(checksum)
        Dim byter(16) As UInt16
        z = 0
        v = 8

        Select Case firstblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        v = 8
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 40
        Select Case secondblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 72
        Select Case thirdblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        v = 104
        Select Case fourthblock
            Case "A"
                While z < 16
                    byter(z) = blocka(z) Xor rand()
                    z = z + 1
                End While
            Case "B"
                While z < 16
                    byter(z) = blockb(z) Xor rand()
                    z = z + 1
                End While
            Case "C"
                While z < 16
                    byter(z) = blockc(z) Xor rand()
                    z = z + 1
                End While
            Case "D"
                While z < 16
                    byter(z) = blockd(z) Xor rand()
                    z = z + 1
                End While
        End Select
        z = 0
        While z < 16
            pkm(v) = byter(z) And 255
            pkm(v + 1) = byter(z) >> 8
            z = z + 1
            v = v + 2
        End While
        z = 0
        Return pkm
    End Function

 

 

I keep getting a System.OverflowException at the Select Case firstblock.

Posted
7 hours ago, Regnum said:

I keep getting a System.OverflowException at the Select Case firstblock.

you may need to set your own variables public and correct in other instances besides the function.

How are you reading the file, and what variable is handling the Pokemon file you're reading
(I'm getting the feeling you haven't loaded the file)

Posted
14 hours ago, theSLAYER said:

you may need to set your own variables public and correct in other instances besides the function.

How are you reading the file, and what variable is handling the Pokemon file you're reading
(I'm getting the feeling you haven't loaded the file)

The file is being loaded with:

Dim myFile = OpenFileDialog1.FileName
Dim pkx() As Byte = My.Computer.FileSystem.ReadAllBytes(myFile)
Dim ekx = encrypt(pkx)

 

Posted
1 hour ago, Regnum said:

The file is being loaded with:


Dim myFile = OpenFileDialog1.FileName
Dim pkx() As Byte = My.Computer.FileSystem.ReadAllBytes(myFile)
Dim ekx = encrypt(pkx)

 

Not that I'm very good at programming, but maybe you wanna
Dim ekx() as byte = encrypt(pkx)

Maybe also want to do a "if then" logic tests in your routine,
as Gen 6/7 encrypt is gonna be different from the Gen5 routine I showed you.

Posted (edited)
5 hours ago, theSLAYER said:

Not that I'm very good at programming, but maybe you wanna
Dim ekx() as byte = encrypt(pkx)

Maybe also want to do a "if then" logic tests in your routine,
as Gen 6/7 encrypt is gonna be different from the Gen5 routine I showed you.

I tried that, but the error persists. I then tried:

Private Sub toEnc()
	Dim myFile As String = OpenFileDialog1.FileName
	Dim myBytes As Byte() = My.Computer.FileSystem.ReadAllBytes(myFile)
	Dim txtTemp As New System.Text.StringBuilder()
	For Each myByte As Byte In myBytes
		txtTemp.Append(myByte.ToString("X2"))
	Next
	RichTextBox1.Text = txtTemp.ToString()

	Dim myData = RichTextBox1.Text
	Dim pkx As Byte() = HexStringToByteArray(myData)
	Dim ekx() As Byte = encrypt(pkx)
End Sub

Private Shared Function HexStringToByteArray(ByRef strInput As String) As Byte()
        Dim length As Integer
        Dim bOutput As Byte()
        Dim c(1) As Integer
        length = strInput.Length / 2
        ReDim bOutput(length - 1)
        For i As Integer = 0 To (length - 1)
            For j As Integer = 0 To 1
                c(j) = Asc(strInput.Chars(i * 2 + j))
                If ((c(j) >= Asc("0")) And (c(j) <= Asc("9"))) Then
                    c(j) = c(j) - Asc("0")
                ElseIf ((c(j) >= Asc("A")) And (c(j) <= Asc("F"))) Then
                    c(j) = c(j) - Asc("A") + &HA
                ElseIf ((c(j) >= Asc("a")) And (c(j) <= Asc("f"))) Then
                    c(j) = c(j) - Asc("a") + &HA
                End If
            Next j
            bOutput(i) = (c(0) * &H10 + c(1))
        Next i
     Return (bOutput)
End Function

 

Edited by Regnum
Posted (edited)
5 minutes ago, theSLAYER said:

before the select case, get your program to prompt a message box, and display the PID values.
(to ensure that the program is indeed reading the file)

It gave me 0x35C7F1EE, but the pk5 file in HxD says 0xEEF1C735.

Edited by Regnum
Posted
1 hour ago, Regnum said:

It gave me 0x35C7F1EE, but the pk5 file in HxD says 0xEEF1C735.

Remember that the data is in little endian and needs to be swapped. At least that part is more of less working properly.

now, slowly increment/push forward to see which sub or function isn't working

Posted (edited)
22 hours ago, theSLAYER said:

Remember that the data is in little endian and needs to be swapped. At least that part is more of less working properly.

now, slowly increment/push forward to see which sub or function isn't working

I tried incrementing forward as you said and found that byter(z) = blocka(z) Xor rand() is equal to byter(z) = 0xAA60CFF and byter() is UInt16. 

So I changed byter(z) = block_(z) Xor rand() to byter(z) = CType((block_(z) Xor rand()), System.UInt16), now the function rand() is giving me the System.OverflowException error.

Edited by Regnum
Posted
4 hours ago, Regnum said:

I tried incrementing forward as you said and found that byter(z) = blocka(z) Xor rand() is equal to byter(z) = 0xAA60CFF and byter() is UInt16. 

So I changed byter(z) = block_(z) Xor rand() to byter(z) = CType((block_(z) Xor rand()), System.UInt16), now the function rand() is giving me the System.OverflowException error.

So you changed it.. Because it was giving you a value?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...