We propose a novel method for optical security by using a randomly selected binary phase code. The phase code is recorded with holographic volume gratings, and the recording material can be regarded as an optical lock, while the original phase code acts as the key. When an arbitrary phase code is used for decryption, the decryption probability is less than when the phase code contains only five rows. The decryption probability decreases dramatically as the row number of the code or the threshold value increases. Furthermore, the security system performs good tolerance for data loss of the key. Both the theory and experiment are demonstrated. © 2004 Society of Photo-Optical Instrumentation Engineers.