We develop a common tunable optical encoder/decoder based on fiber delayers and a logical controller. A new tunable optical encoder/decoder for optical code division multiplexing is designed and analyzed by using a delayer based on photonic crystals (PhCs) and a logical controller. In this scheme, it is found that the optical pulse delay is flexibly controlled by using PhC-based delayers to achieve optical encoding and decoding with a logical controller. The reflection, transmission, time delay characteristic, and optical spectrum of the encoded and decoded pulses are studied by numerical calculation for waves tuned in the PhC band gap. Theoretical analysis and numerical results show that optical pulses are achieved with the proper time delay. The advantages of this scheme are integration, reconfigurability, and easy operation.