A mask is selected amongst a plurality of masks. A first masked random number is generated by converting a first random number using the selected mask, and a first key is generated from the first masked random number and a first biometric code generated from biometric information. In addition, mask information indicating the selected mask is stored. A second masked random number is generated by converting a second random number using the selected mask or a different mask having a predetermined relationship with the selected mask, and a second key is generated from the second masked random number and a second biometric code. A ciphertext is generated using one of the first key and the second key and an error-correction encoding method.