The internet of things (IoT) is a multiple devices, which connects with the internet for communication, in order to obtain the updated from the cloud. The fog can act as a controller and it is located between the IoT devices and cloud. The major attacks like de-synchronization, and disclosure has arises in the devices, this has been prevented. The major contribution in this work is key generation and authentication, for key generation the “advanced encryption standard algorithm” is developed, in which the new and old keys are generated. The encryption is done under the source side, and decryption is done under the device side. The fog security is maintained through “device tag, and bit wise XOR rotational algorithm”. The security, and the computational complexity is defined in this work and it is given in table format. The implementations are carried out in the MATLAB R2016 a. The proposed algorithm is compared with the existing protocols like LMAP, M2AP, EMAP, SASI, and RAPP, from the comparison the proposed methodology makes the better knowledge about the security and prevents from various attacks.