On the third question; To distribute public keys on the Internet, you need to build trust. You have to trust the other party. The key Diffie Hellman agreement alone does not create trust; just a common secret with another party. Therefore, you need a secure protocol (z.B TLS) with trust certificates or keys containing an authentication component. However, to establish such a protocol, a great deal of knowledge is needed; If you don`t fully understand the code you just showed us, you won`t be able to create such a protocol yourself. Prepare a module for the implementation of a key tuning algorithm. This class allows two cooperating parties to generate the same secret key, while parties that have nothing to do with the agreement generate the same key. The last option is to use a key tuning algorithm. Important agreement algorithms exchange certain public information between two parties so that they can calculate a common secret key. However, they do not exchange enough information as conversation listening companies can calculate the same freed key. Nothing in this key protocol prevents someone from imitating Bob-Alice, could exchange keys with me, I could say I`m Bob, and Alice and I could exchange encrypted data.
Although there is no need to encrypt transmissions of public keys, they must be signed for maximum security. This method reintegrates this keyagrement object so that it can be reused for other key chords. If this key agreement is not re-initiated by one of the init methods, the same private information and algorithm settings are used for the following key agreements. Generate bytes that represent the secret key; These bytes can then be used to create a SecretKey object. The type of this object depends on the algorithm that is implemented by this key chord. The bytes are either returned by this argument or placed in the specified table (starting with the specified offset). In the latter case, a ShortBufferException is triggered if the board is not large enough to hold all the bytes. If all the steps of the key MOU have not been completed, an illegal transaction will be generated. This algorithm works according to the characteristics of the Diffie-Hellman public/private key pair. These keys are not suitable for use in an encryption algorithm. they are only used in a most important agreement like this.
The keys involved in setting up a common secret key are created by one of the key generators (KeyPairGenerator or KeyGenerator), a KeyFactory or following an intermediate phase of the key memorandum of understanding. Run the next step in the key protocol. Important MOUs generally require that a certain number of operations be carried out in a certain order.