Advertisement
Guest User

Untitled

a guest
Apr 25th, 2011
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.38 KB | None | 0 0
  1.     ''' <summary>
  2.     ''' Decrypt raw Pokémon data.
  3.     ''' </summary>
  4.     ''' <param name="Data"></param>
  5.     ''' <returns></returns>
  6.     ''' <remarks></remarks>
  7.     Public Shared Function Decrypt_Data(ByVal Data() As Byte) As Byte()
  8.  
  9.         Dim Checksum As UInt16 = BitConverter.ToUInt16(Data, &H6)
  10.  
  11.         Dim ucheck As UInt16 = Checksum
  12.         Dim prng As New PokemonLib.PokePRNG '(ucheck)
  13.         prng.Seed = ucheck
  14.  
  15.         For i As Integer = 8 To 135 Step 2
  16.             Dim bef As UShort = BitConverter.ToUInt16(Data, i)
  17.             Dim aft As UShort = CUShort((bef Xor (prng.NextNum() >> &H10)))
  18.             Data(i + 1) = CByte((aft >> &H8))
  19.             Data(i) = CByte((aft And &HFF))
  20.         Next
  21.  
  22.         If Data.Length = 136 Then Return Data
  23.  
  24.         prng.Seed = BitConverter.ToUInt32(Data, 0)
  25.  
  26.         For i As Integer = 136 To 235 Step 2
  27.             Dim bef As UShort = BitConverter.ToUInt16(Data, i)
  28.             Dim aft As UShort = CUShort((bef Xor (prng.NextNum() >> &H10)))
  29.             Data(i + 1) = CByte((aft >> &H8))
  30.             Data(i) = CByte((aft And &HFF))
  31.         Next
  32.  
  33.         Return Data
  34.  
  35.     End Function
  36.  
  37.     ''' <summary>
  38.     ''' Decrypt and unshuffle the given raw Pokémon data.
  39.     ''' </summary>
  40.     ''' <param name="PKM"></param>
  41.     ''' <returns></returns>
  42.     ''' <remarks></remarks>
  43.     Public Shared Function DecryptPokemon(ByVal PKM() As Byte) As Byte()
  44.         Return UnShuffleBytes(Decrypt_Data(PKM))
  45.     End Function
  46.  
  47.     ''' <summary>
  48.     ''' Encrypt and shuffle the given raw Pokémon data.
  49.     ''' </summary>
  50.     ''' <param name="PKM"></param>
  51.     ''' <returns></returns>
  52.     ''' <remarks></remarks>
  53.     Public Shared Function EncryptPokemon(ByVal PKM() As Byte) As Byte()
  54.         Dim checksum As UInt16 = Calculate_Checksum(PKM)
  55.         Dim chkBytes() As Byte = BitConverter.GetBytes(checksum)
  56.  
  57.         PKM(6) = chkBytes(0)
  58.         PKM(7) = chkBytes(1)
  59.  
  60.         Dim Encrypted() As Byte = ShuffleBytes(PKM)
  61.  
  62.         Encrypted = Decrypt_Data(Encrypted)
  63.  
  64.         Return Encrypted
  65.  
  66.         Encrypted = Nothing
  67.         PKM = Nothing
  68.     End Function
  69.  
  70.     ''' <summary>
  71.     ''' Calculate the checksum of the given Pokémon data.
  72.     ''' </summary>
  73.     ''' <param name="PKM"></param>
  74.     ''' <returns></returns>
  75.     ''' <remarks></remarks>
  76.     Public Shared Function Calculate_Checksum(ByVal PKM() As Byte) As UInt16
  77.  
  78.         Dim Data(PKM.Length - 1) As Byte
  79.         For i As Integer = 0 To Data.Length - 1
  80.             Data(i) = PKM(i)
  81.         Next
  82.  
  83.         Dim index As UInteger = 0
  84.  
  85.         For i As Integer = &H8 To &H86 Step 2
  86.             index += Data(i) + (Data(i + 1) * 256)
  87.         Next
  88.  
  89.         Dim bin As String = DecToBin(index, 32)
  90.         bin = bin.Substring(16, 16)
  91.         Return Convert.ToUInt16(bin, 2)
  92.  
  93.     End Function
  94.  
  95.     ''' <summary>
  96.     ''' Shuffle the bytes in the given .pkm byte array.
  97.     ''' </summary>
  98.     ''' <param name="UnencryptedData"></param>
  99.     ''' <returns></returns>
  100.     ''' <remarks></remarks>
  101.     Public Shared Function ShuffleBytes(ByVal UnencryptedData() As Byte) As Byte()
  102.         InitializeDictionaries()
  103.         Dim PID As UInt32 = BitConverter.ToUInt32(UnencryptedData, 0)
  104.         Dim UnShuffleIndex As UInt16 = ((PID >> &HD) And &H1F) Mod 24
  105.         Dim ShuffleOrder As String = dpPKMShuffle(UnShuffleIndex)
  106.  
  107.         Dim Block1(31) As Byte
  108.         Dim Block2(31) As Byte
  109.         Dim Block3(31) As Byte
  110.         Dim Block4(31) As Byte
  111.  
  112.         Select Case ShuffleOrder.Substring(0, 1)
  113.             Case "A"
  114.                 For i As Integer = &H8 To &H27
  115.                     Block1(i - &H8) = UnencryptedData(i)
  116.                 Next
  117.             Case "B"
  118.                 For i As Integer = &H28 To &H47
  119.                     Block1(i - &H28) = UnencryptedData(i)
  120.                 Next
  121.             Case "C"
  122.                 For i As Integer = &H48 To &H67
  123.                     Block1(i - &H48) = UnencryptedData(i)
  124.                 Next
  125.             Case "D"
  126.                 For i As Integer = &H68 To &H87
  127.                     Block1(i - &H68) = UnencryptedData(i)
  128.                 Next
  129.         End Select
  130.  
  131.  
  132.  
  133.         Select Case ShuffleOrder.Substring(1, 1)
  134.             Case "A"
  135.                 For i As Integer = &H8 To &H27
  136.                     Block2(i - &H8) = UnencryptedData(i)
  137.                 Next
  138.             Case "B"
  139.                 For i As Integer = &H28 To &H47
  140.                     Block2(i - &H28) = UnencryptedData(i)
  141.                 Next
  142.             Case "C"
  143.                 For i As Integer = &H48 To &H67
  144.                     Block2(i - &H48) = UnencryptedData(i)
  145.                 Next
  146.             Case "D"
  147.                 For i As Integer = &H68 To &H87
  148.                     Block2(i - &H68) = UnencryptedData(i)
  149.                 Next
  150.         End Select
  151.  
  152.  
  153.  
  154.         Select Case ShuffleOrder.Substring(2, 1)
  155.             Case "A"
  156.                 For i As Integer = &H8 To &H27
  157.                     Block3(i - &H8) = UnencryptedData(i)
  158.                 Next
  159.             Case "B"
  160.                 For i As Integer = &H28 To &H47
  161.                     Block3(i - &H28) = UnencryptedData(i)
  162.                 Next
  163.             Case "C"
  164.                 For i As Integer = &H48 To &H67
  165.                     Block3(i - &H48) = UnencryptedData(i)
  166.                 Next
  167.             Case "D"
  168.                 For i As Integer = &H68 To &H87
  169.                     Block3(i - &H68) = UnencryptedData(i)
  170.                 Next
  171.         End Select
  172.  
  173.  
  174.  
  175.         Select Case ShuffleOrder.Substring(3, 1)
  176.             Case "A"
  177.                 For i As Integer = &H8 To &H27
  178.                     Block4(i - &H8) = UnencryptedData(i)
  179.                 Next
  180.             Case "B"
  181.                 For i As Integer = &H28 To &H47
  182.                     Block4(i - &H28) = UnencryptedData(i)
  183.                 Next
  184.             Case "C"
  185.                 For i As Integer = &H48 To &H67
  186.                     Block4(i - &H48) = UnencryptedData(i)
  187.                 Next
  188.             Case "D"
  189.                 For i As Integer = &H68 To &H87
  190.                     Block4(i - &H68) = UnencryptedData(i)
  191.                 Next
  192.         End Select
  193.  
  194.         For i As Integer = &H8 To &H27
  195.  
  196.             UnencryptedData(i) = Block1(i - &H8)
  197.  
  198.         Next
  199.  
  200.         For i As Integer = &H28 To &H47
  201.  
  202.             UnencryptedData(i) = Block2(i - &H28)
  203.  
  204.         Next
  205.  
  206.         For i As Integer = &H48 To &H67
  207.  
  208.             UnencryptedData(i) = Block3(i - &H48)
  209.  
  210.         Next
  211.  
  212.         For i As Integer = &H68 To &H87
  213.  
  214.             UnencryptedData(i) = Block4(i - &H68)
  215.  
  216.         Next
  217.  
  218.         Return UnencryptedData
  219.     End Function
  220.  
  221.     ''' <summary>
  222.     ''' Unshuffle the bytes in the given .pkm byte array.
  223.     ''' </summary>
  224.     ''' <param name="UnencryptedData"></param>
  225.     ''' <returns></returns>
  226.     ''' <remarks></remarks>
  227.     Public Shared Function UnShuffleBytes(ByVal UnencryptedData() As Byte) As Byte()
  228.         InitializeDictionaries()
  229.         Dim PID As UInt32 = BitConverter.ToUInt32(UnencryptedData, 0)
  230.         Dim UnShuffleIndex As UInt16 = ((PID >> &HD) And &H1F) Mod 24
  231.         Dim ShuffleOrder As String = dpPKMShuffle(UnShuffleIndex)
  232.  
  233.         Dim Block1(31) As Byte
  234.         Dim Block2(31) As Byte
  235.         Dim Block3(31) As Byte
  236.         Dim Block4(31) As Byte
  237.  
  238.         Select Case ShuffleOrder.Substring(0, 1)
  239.             Case "A"
  240.                 For i As Integer = &H8 To &H27
  241.                     Block1(i - &H8) = UnencryptedData(i)
  242.                 Next
  243.             Case "B"
  244.                 For i As Integer = &H8 To &H27
  245.                     Block2(i - &H8) = UnencryptedData(i)
  246.                 Next
  247.             Case "C"
  248.                 For i As Integer = &H8 To &H27
  249.                     Block3(i - &H8) = UnencryptedData(i)
  250.                 Next
  251.             Case "D"
  252.                 For i As Integer = &H8 To &H27
  253.                     Block4(i - &H8) = UnencryptedData(i)
  254.                 Next
  255.         End Select
  256.  
  257.         Select Case ShuffleOrder.Substring(1, 1)
  258.             Case "A"
  259.                 For i As Integer = &H28 To &H47
  260.                     Block1(i - &H28) = UnencryptedData(i)
  261.                 Next
  262.             Case "B"
  263.                 For i As Integer = &H28 To &H47
  264.                     Block2(i - &H28) = UnencryptedData(i)
  265.                 Next
  266.             Case "C"
  267.                 For i As Integer = &H28 To &H47
  268.                     Block3(i - &H28) = UnencryptedData(i)
  269.                 Next
  270.             Case "D"
  271.                 For i As Integer = &H28 To &H47
  272.                     Block4(i - &H28) = UnencryptedData(i)
  273.                 Next
  274.         End Select
  275.  
  276.         Select Case ShuffleOrder.Substring(2, 1)
  277.             Case "A"
  278.                 For i As Integer = &H48 To &H67
  279.                     Block1(i - &H48) = UnencryptedData(i)
  280.                 Next
  281.             Case "B"
  282.                 For i As Integer = &H48 To &H67
  283.                     Block2(i - &H48) = UnencryptedData(i)
  284.                 Next
  285.             Case "C"
  286.                 For i As Integer = &H48 To &H67
  287.                     Block3(i - &H48) = UnencryptedData(i)
  288.                 Next
  289.             Case "D"
  290.                 For i As Integer = &H48 To &H67
  291.                     Block4(i - &H48) = UnencryptedData(i)
  292.                 Next
  293.         End Select
  294.  
  295.         Select Case ShuffleOrder.Substring(3, 1)
  296.             Case "A"
  297.                 For i As Integer = &H68 To &H87
  298.                     Block1(i - &H68) = UnencryptedData(i)
  299.                 Next
  300.             Case "B"
  301.                 For i As Integer = &H68 To &H87
  302.                     Block2(i - &H68) = UnencryptedData(i)
  303.                 Next
  304.             Case "C"
  305.                 For i As Integer = &H68 To &H87
  306.                     Block3(i - &H68) = UnencryptedData(i)
  307.                 Next
  308.             Case "D"
  309.                 For i As Integer = &H68 To &H87
  310.                     Block4(i - &H68) = UnencryptedData(i)
  311.                 Next
  312.         End Select
  313.  
  314.         For i As Integer = &H8 To &H27
  315.  
  316.             UnencryptedData(i) = Block1(i - &H8)
  317.  
  318.         Next
  319.  
  320.         For i As Integer = &H28 To &H47
  321.  
  322.             UnencryptedData(i) = Block2(i - &H28)
  323.  
  324.         Next
  325.  
  326.         For i As Integer = &H48 To &H67
  327.  
  328.             UnencryptedData(i) = Block3(i - &H48)
  329.  
  330.         Next
  331.  
  332.         For i As Integer = &H68 To &H87
  333.  
  334.             UnencryptedData(i) = Block4(i - &H68)
  335.  
  336.         Next
  337.  
  338.         Return UnencryptedData
  339.     End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement