Strategies to encode or label small particles or beads for use in high-throughput screening and bioassay applications focus on either spatially differentiated, on-chip arrays or random distributions of encoded beads. Attempts to encode large numbers of polymeric, metallic or glass beads in random arrays or in fluid suspension have used a variety of entities to provide coded elements (bits)--fluorescent molecules, molecules with specific vibrational signatures, quantum dots, or discrete metallic layers. Here we report a method for optically encoding micrometre-sized nanostructured particles of porous silicon. We generate multilayered porous films in crystalline silicon using a periodic electrochemical etch. This results in photonic crystals with well-resolved and narrow optical reflectivity features, whose wavelengths are determined by the etching parameters. Millions of possible codes can be prepared this way. Micrometre-sized particles are then produced by ultrasonic fracture, mechanical grinding or by lithographic means. A simple antibody-based bioassay using fluorescently tagged proteins demonstrates the encoding strategy in biologically relevant media.