Brunet.Security.SecurityDataMessage.Decrypt C# (CSharp) Method

Decrypt() public method

Decrypts the packet given a SymmetricEncryption returning true if it was able to decrypt it.
public Decrypt ( SymmetricEncryption se ) : bool
se SymmetricEncryption
return bool
    public bool Decrypt(SymmetricEncryption se) {
      byte[] decrypted = se.DecryptData(_encrypted_data);
      int pos = 0;
      int data_len = NumberSerializer.ReadInt(decrypted, pos);
      pos += 4;
      _data = MemBlock.Reference(decrypted, pos, data_len);
      pos += data_len;
      _signature = MemBlock.Reference(decrypted, pos, decrypted.Length - pos);
      return true;
    }
  }

Usage Example

Example #1
0
        /// <summary>Decrypts the data and then verifys it.</summary>
        /// <param name="EncryptedData">The data to decrypt and verify.</param>
        /// <returns>The verified and decrypted data.</returns>
        public void DecryptAndVerify(SecurityDataMessage sdm)
        {
            if (_closed)
            {
                throw new Exception("SecurityHandler: closed");
            }
            else if (sdm.Epoch != Epoch)
            {
                throw new Exception(String.Format("Wrong index {0}, it should be {1}.",
                                                  sdm.Epoch, Epoch));
            }

            int seqid = sdm.Seqid;

            // Verify the seqid
            // If greater than current, new seqid and allow packet
            // If less than current but within window, allow packet
            // Else throw exception
            if (UseWindow)
            {
                if (seqid == Int32.MaxValue)
                {
                    Close();
                    throw new Exception("Maximum amount of packets sent over SecurityHandler.");
                }
                else if (seqid + WINDOW_SIZE < _last_incoming_seqid)
                {
                    throw new Exception(String.Format("Invalid seqid: {0}, current seqid: {1}, window: {2}.",
                                                      seqid, _last_incoming_seqid, WINDOW_SIZE));
                }
            }

            sdm.Decrypt(_decryptor);
            if (!sdm.Verify(_incoming_auth))
            {
                throw new Exception("Invalid signature");
            }

            if (seqid > _last_incoming_seqid)
            {
                _last_incoming_seqid = seqid;
            }
        }
All Usage Examples Of Brunet.Security.SecurityDataMessage::Decrypt