public static byte[] EncryptPayload(byte senderId, byte receiverId, byte[] homeId, byte sequenceNumber, byte[] receiverNonce,
                                            byte[] senderNonce, byte[] networkKey, byte[] textToEncrypt, int generationCount, bool isRealKey)
        {
            var ret = new COMMAND_CLASS_SECURITY_2.SECURITY_2_MESSAGE_ENCAPSULATION {
                sequenceNumber = sequenceNumber
            };
            InvariantPeerNodeId peerNodeId = new InvariantPeerNodeId(0);
            var mpanKey         = new byte[SecurityS2Utils.KEY_SIZE];
            var ccmKey          = new byte[SecurityS2Utils.KEY_SIZE];
            var personalization = new byte[SecurityS2Utils.PERSONALIZATION_SIZE];

            if (isRealKey)
            {
                SecurityS2Utils.NetworkKeyExpand(networkKey, ccmKey, personalization, mpanKey);
            }
            else
            {
                SecurityS2Utils.TempKeyExpand(networkKey, ccmKey, personalization, mpanKey);
            }
            SpanTable spanTable = new SpanTable();

            spanTable.Add(peerNodeId, receiverNonce, 0, 0);
            SpanContainer spanContainer = spanTable.GetContainer(peerNodeId);

            spanContainer.InstantiateWithSenderNonce(senderNonce, personalization);
            for (int i = 0; i < generationCount; i++)
            {
                spanContainer.NextNonce();
            }

            AAD aad = new AAD
            {
                SenderNodeId   = senderId,
                ReceiverNodeId = receiverId,
                HomeId         = homeId,
                PayloadLength  = (ushort)(textToEncrypt.Length + SecurityS2Utils.AUTH_DATA_HEADER_LENGTH),
                SequenceNumber = sequenceNumber
            };

            aad.PayloadLength += (ushort)((byte[])ret).Length;
            var cipherData = SecurityS2Utils.CcmEncryptAndAuth(ccmKey, spanContainer.Span, aad, textToEncrypt);

            if (cipherData != null && cipherData.Length > 0)
            {
                ret.ccmCiphertextObject = new List <byte>(cipherData);
            }
            else
            {
                ret = null;
            }
            return(ret);
        }
        protected static byte[] EncryptS2Internal(byte[] key, byte[] iv, byte senderNodeId, byte receiverNodeId,
                                                  byte[] homeId,
                                                  ushort payloadLength, byte sequenceNumber, byte statusByte, byte[] extensionData, byte[] data)
        {
            var aad = new AAD
            {
                SenderNodeId   = senderNodeId,
                ReceiverNodeId = receiverNodeId,
                HomeId         = homeId,
                SequenceNumber = sequenceNumber,
                StatusByte     = statusByte,
                PayloadLength  = payloadLength,
                ExtensionData  = extensionData
            };

            return(SecurityS2Utils.CcmEncryptAndAuth(key, iv, aad, data));
        }