Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' <summary>
- ''' Decrypt raw Pokémon data.
- ''' </summary>
- ''' <param name="Data"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function Decrypt_Data(ByVal Data() As Byte) As Byte()
- Dim Checksum As UInt16 = BitConverter.ToUInt16(Data, &H6)
- Dim ucheck As UInt16 = Checksum
- Dim prng As New PokemonLib.PokePRNG '(ucheck)
- prng.Seed = ucheck
- For i As Integer = 8 To 135 Step 2
- Dim bef As UShort = BitConverter.ToUInt16(Data, i)
- Dim aft As UShort = CUShort((bef Xor (prng.NextNum() >> &H10)))
- Data(i + 1) = CByte((aft >> &H8))
- Data(i) = CByte((aft And &HFF))
- Next
- If Data.Length = 136 Then Return Data
- prng.Seed = BitConverter.ToUInt32(Data, 0)
- For i As Integer = 136 To 235 Step 2
- Dim bef As UShort = BitConverter.ToUInt16(Data, i)
- Dim aft As UShort = CUShort((bef Xor (prng.NextNum() >> &H10)))
- Data(i + 1) = CByte((aft >> &H8))
- Data(i) = CByte((aft And &HFF))
- Next
- Return Data
- End Function
- ''' <summary>
- ''' Decrypt and unshuffle the given raw Pokémon data.
- ''' </summary>
- ''' <param name="PKM"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function DecryptPokemon(ByVal PKM() As Byte) As Byte()
- Return UnShuffleBytes(Decrypt_Data(PKM))
- End Function
- ''' <summary>
- ''' Encrypt and shuffle the given raw Pokémon data.
- ''' </summary>
- ''' <param name="PKM"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function EncryptPokemon(ByVal PKM() As Byte) As Byte()
- Dim checksum As UInt16 = Calculate_Checksum(PKM)
- Dim chkBytes() As Byte = BitConverter.GetBytes(checksum)
- PKM(6) = chkBytes(0)
- PKM(7) = chkBytes(1)
- Dim Encrypted() As Byte = ShuffleBytes(PKM)
- Encrypted = Decrypt_Data(Encrypted)
- Return Encrypted
- Encrypted = Nothing
- PKM = Nothing
- End Function
- ''' <summary>
- ''' Calculate the checksum of the given Pokémon data.
- ''' </summary>
- ''' <param name="PKM"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function Calculate_Checksum(ByVal PKM() As Byte) As UInt16
- Dim Data(PKM.Length - 1) As Byte
- For i As Integer = 0 To Data.Length - 1
- Data(i) = PKM(i)
- Next
- Dim index As UInteger = 0
- For i As Integer = &H8 To &H86 Step 2
- index += Data(i) + (Data(i + 1) * 256)
- Next
- Dim bin As String = DecToBin(index, 32)
- bin = bin.Substring(16, 16)
- Return Convert.ToUInt16(bin, 2)
- End Function
- ''' <summary>
- ''' Shuffle the bytes in the given .pkm byte array.
- ''' </summary>
- ''' <param name="UnencryptedData"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function ShuffleBytes(ByVal UnencryptedData() As Byte) As Byte()
- InitializeDictionaries()
- Dim PID As UInt32 = BitConverter.ToUInt32(UnencryptedData, 0)
- Dim UnShuffleIndex As UInt16 = ((PID >> &HD) And &H1F) Mod 24
- Dim ShuffleOrder As String = dpPKMShuffle(UnShuffleIndex)
- Dim Block1(31) As Byte
- Dim Block2(31) As Byte
- Dim Block3(31) As Byte
- Dim Block4(31) As Byte
- Select Case ShuffleOrder.Substring(0, 1)
- Case "A"
- For i As Integer = &H8 To &H27
- Block1(i - &H8) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H28 To &H47
- Block1(i - &H28) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H48 To &H67
- Block1(i - &H48) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H68 To &H87
- Block1(i - &H68) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(1, 1)
- Case "A"
- For i As Integer = &H8 To &H27
- Block2(i - &H8) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H28 To &H47
- Block2(i - &H28) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H48 To &H67
- Block2(i - &H48) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H68 To &H87
- Block2(i - &H68) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(2, 1)
- Case "A"
- For i As Integer = &H8 To &H27
- Block3(i - &H8) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H28 To &H47
- Block3(i - &H28) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H48 To &H67
- Block3(i - &H48) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H68 To &H87
- Block3(i - &H68) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(3, 1)
- Case "A"
- For i As Integer = &H8 To &H27
- Block4(i - &H8) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H28 To &H47
- Block4(i - &H28) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H48 To &H67
- Block4(i - &H48) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H68 To &H87
- Block4(i - &H68) = UnencryptedData(i)
- Next
- End Select
- For i As Integer = &H8 To &H27
- UnencryptedData(i) = Block1(i - &H8)
- Next
- For i As Integer = &H28 To &H47
- UnencryptedData(i) = Block2(i - &H28)
- Next
- For i As Integer = &H48 To &H67
- UnencryptedData(i) = Block3(i - &H48)
- Next
- For i As Integer = &H68 To &H87
- UnencryptedData(i) = Block4(i - &H68)
- Next
- Return UnencryptedData
- End Function
- ''' <summary>
- ''' Unshuffle the bytes in the given .pkm byte array.
- ''' </summary>
- ''' <param name="UnencryptedData"></param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shared Function UnShuffleBytes(ByVal UnencryptedData() As Byte) As Byte()
- InitializeDictionaries()
- Dim PID As UInt32 = BitConverter.ToUInt32(UnencryptedData, 0)
- Dim UnShuffleIndex As UInt16 = ((PID >> &HD) And &H1F) Mod 24
- Dim ShuffleOrder As String = dpPKMShuffle(UnShuffleIndex)
- Dim Block1(31) As Byte
- Dim Block2(31) As Byte
- Dim Block3(31) As Byte
- Dim Block4(31) As Byte
- Select Case ShuffleOrder.Substring(0, 1)
- Case "A"
- For i As Integer = &H8 To &H27
- Block1(i - &H8) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H8 To &H27
- Block2(i - &H8) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H8 To &H27
- Block3(i - &H8) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H8 To &H27
- Block4(i - &H8) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(1, 1)
- Case "A"
- For i As Integer = &H28 To &H47
- Block1(i - &H28) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H28 To &H47
- Block2(i - &H28) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H28 To &H47
- Block3(i - &H28) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H28 To &H47
- Block4(i - &H28) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(2, 1)
- Case "A"
- For i As Integer = &H48 To &H67
- Block1(i - &H48) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H48 To &H67
- Block2(i - &H48) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H48 To &H67
- Block3(i - &H48) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H48 To &H67
- Block4(i - &H48) = UnencryptedData(i)
- Next
- End Select
- Select Case ShuffleOrder.Substring(3, 1)
- Case "A"
- For i As Integer = &H68 To &H87
- Block1(i - &H68) = UnencryptedData(i)
- Next
- Case "B"
- For i As Integer = &H68 To &H87
- Block2(i - &H68) = UnencryptedData(i)
- Next
- Case "C"
- For i As Integer = &H68 To &H87
- Block3(i - &H68) = UnencryptedData(i)
- Next
- Case "D"
- For i As Integer = &H68 To &H87
- Block4(i - &H68) = UnencryptedData(i)
- Next
- End Select
- For i As Integer = &H8 To &H27
- UnencryptedData(i) = Block1(i - &H8)
- Next
- For i As Integer = &H28 To &H47
- UnencryptedData(i) = Block2(i - &H28)
- Next
- For i As Integer = &H48 To &H67
- UnencryptedData(i) = Block3(i - &H48)
- Next
- For i As Integer = &H68 To &H87
- UnencryptedData(i) = Block4(i - &H68)
- Next
- Return UnencryptedData
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement