A key generation method, comprising: receiving, by a terminal, a first message sent by a source base station, wherein the first message comprises a key exchange algorithm selected by a target base station and a first public key generated by the target base station, the key exchange algorithm being one of a plurality of key exchange algorithms supported by the terminal and sent by the terminal to the source base station; generating, by the terminal, a first shared key based on the key exchange algorithm, the first public key, and a first private key generated by the terminal; and sending, by the terminal, a second message to the target base station, wherein the second message comprises a second public key generated by the terminal, wherein before the receiving, by a terminal, a first message sent by a source base station, the method further comprises: sending, by the terminal, a plurality of key exchange algorithms supported by the terminal to the source base station, the method further comprising generating, by the terminal, a public-private key pair including the second public key and the first private key, wherein the first message further comprises a cell identity and a carrier frequency of a target cell; and before the sending, by the terminal, a second message to the target base station, the method further comprises: generating, by the terminal, a second key based on a prestored first key, and the cell identity and the carrier frequency of the target cell; and performing encryption processing on the second message by using the second key. receiving, by a terminal, a first message sent by a source base station, wherein the first message comprises a key exchange algorithm selected by a target base station and a first public key generated by the target base station, the key exchange algorithm being one of a plurality of key exchange algorithms supported by the terminal and sent by the terminal to the source base station; generating, by the terminal, a first shared key based on the key exchange algorithm, the first public key, and a first private key generated by the terminal; and sending, by the terminal, a second message to the target base station, wherein the second message comprises a second public key generated by the terminal, wherein before the receiving, by a terminal, a first message sent by a source base station, the method further comprises: sending, by the terminal, a plurality of key exchange algorithms supported by the terminal to the source base station, the method further comprising generating, by the terminal, a public-private key pair including the second public key and the first private key, wherein the first message further comprises a cell identity and a carrier frequency of a target cell; and sending, by the terminal, a plurality of key exchange algorithms supported by the terminal to the source base station, the method further comprising generating, by the terminal, a public-private key pair including the second public key and the first private key, wherein the first message further comprises a cell identity and a carrier frequency of a target cell; and before the sending, by the terminal, a second message to the target base station, the method further comprises: generating, by the terminal, a second key based on a prestored first key, and the cell identity and the carrier frequency of the target cell; and performing encryption processing on the second message by using the second key. generating, by the terminal, a second key based on a prestored first key, and the cell identity and the carrier frequency of the target cell; and performing encryption processing on the second message by using the second key.