Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:PK4 and PK5

From ReverseEngineering.wiki
Format
On this wiki Facts · Code · Notes · Topics

Gen 4 and Gen 5 Pokémon entity (.pk4, .pk5; 136 bytes stored, 236 party in Gen 4, 220 in Gen 5): 8-byte header with PID and checksum, four 32-byte blocks shuffled by (PID >> 13) & 31 and encrypted with a 16-bit LCG stream seeded by the checksum.

The Pokémon record of the Nintendo DS games (Diamond, Pearl, Platinum, HeartGold, SoulSilver; Black, White, Black 2, White 2) as read from Tool:PKHeX (PK4.cs, PK5.cs, PokeCrypto.cs). Platform: Platform:Nintendo DS.

Sizes

  • Stored 136 bytes (0x88). Party: 236 bytes in Gen 4, 220 in Gen 5; the extra bytes after 0x88 are party stats (level, HP, stats), encrypted separately.
  • The Gen 4 Battle Revolution form of the record is big-endian and has no stream encryption at rest, only the block shuffle.

Header

Offset Field
0x00 PID u32 (in Gen 5 the "encryption constant" is the PID)
0x04 sanity u16 (0 in a valid entity)
0x06 checksum u16
0x08 species u16
0x0A held item u16
0x0C trainer id u16, 0x0E secret id u16
0x10 experience u32

Encryption

Bytes 0x08-0x87 are four 32-byte blocks. Stream encryption: for each little-endian u16 in that range, advance seed = seed * 0x41C64E6D + 0x6073 (u32) and XOR the u16 with seed >> 16; the starting seed is the checksum from offset 6. Then the blocks are shuffled by (PID >> 13) & 31 (values 24-31 behave like 0-7), the same 24 orders as Format:PK3. Party bytes after 0x88 use the same generator seeded with the PID. The checksum is the 16-bit sum of the u16 words 0x08-0x87 of the decrypted data. Details: Fact:PK4 and PK5/behavior/PK4 and PK5 stream encryption.

Later generations

Gen 6/7 (PK6/PK7, 0xE8 stored, 0x104 party, 56-byte blocks) seed the same stream with the PID instead of the checksum; Gen 8/9 are in Format:PK8, PA8 and PK9.

Unverified: only the field offsets 0x04-0x10 were read in PK4.cs/PK5.cs; the contents of the individual blocks were not.

Code & repositories

  • https://github.com/kwsch/PKHeX @ 641c26ba100225f3605e5a44b61b3b16e15a3774 · license: GPL-3.0 (declared by the contributor; GitHub's detector did not recognize LICENSE)
    Pokémon save and entity editor; PKHeX.Core is the reference for save sectors, checksums and PK3-PK9 encryption (PokeCrypto.cs, SAV3.cs, PK3.cs, SAV1.cs, SAV2.cs).
    Tested on: source read, not run: source read, not run · checked 2026-10-18