A security method for Device-to-Device, D2D, mode B discovery, comprising four processes: a passive terminal process, an active terminal process, a query process and a matching process; wherein during the passive terminal process, a Proximity-based Service, ProSe, function entity in a home network of a passive terminal sending back a ProSe response code and a ProSe response key to the passive terminal; the passive terminal receiving the ProSe response code and the ProSe response key; during the active terminal process, a ProSe function entity in a home network of an active terminal receiving a ProSe query code, a ProSe query key, a ProSe response code, and a ProSe response key, which are sent back from a ProSe function entity in another network; the ProSe function entity in the home network of the active terminal sending back the ProSe query code and the ProSe query key to the active terminal; during the query process: an active terminal calculating a query message integrity protection code, MIC, sending a ProSe query code, the query MIC, and a time calibration value, to a passive terminal; the passive terminal receiving the ProSe query code, the query MIC, and the time calibration value, which are sent by the active terminal; the passive terminal sending the ProSe query code, the query MIC, and a counter value based on UTC time, to a ProSe function entity in a home network of the passive terminal, and the ProSe function entity in the home network of the passive terminal checking the query MIC; the passive terminal calculating a response MIC, and sending back the ProSe response code, the response MIC and a time calibration value to the active terminal; during the matching process, an active terminal sending a ProSe response code, a response MIC, and a counter value based on UTC time, to a ProSe function entity in a home network of the active terminal; the ProSe function entity in the home network receiving the ProSe response code, the response MIC, and the counter value based on UTC time, analyzing the ProSe response code, and checking the response MIC; wherein the time calibration value corresponds to the lower four bits of the counter value based on UTC time; the query MIC is calculated using the ProSe query key, the ProSe query code and the counter value based on UTC time corresponding to the time calibrated value of the active terminal; and the response MIC is calculated using the ProSe response key, the ProSe response code and the counter value based on UTC time corresponding to the time calibrated value of the passive terminal. an active terminal calculating a query message integrity protection code, MIC, sending a ProSe query code, the query MIC, and a time calibration value, to a passive terminal; the passive terminal receiving the ProSe query code, the query MIC, and the time calibration value, which are sent by the active terminal; the passive terminal sending the ProSe query code, the query MIC, and a counter value based on UTC time, to a ProSe function entity in a home network of the passive terminal, and the ProSe function entity in the home network of the passive terminal checking the query MIC; the passive terminal calculating a response MIC, and sending back the ProSe response code, the response MIC and a time calibration value to the active terminal; during the matching process, an active terminal sending a ProSe response code, a response MIC, and a counter value based on UTC time, to a ProSe function entity in a home network of the active terminal; the ProSe function entity in the home network receiving the ProSe response code, the response MIC, and the counter value based on UTC time, analyzing the ProSe response code, and checking the response MIC;