Brunet.Security.SymmetricEncryption.EncryptData C# (CSharp) Method

EncryptData() public method

Takes in an unencrypted byte array and returns the encrypted form.
public EncryptData ( byte UnencryptedData ) : byte[]
UnencryptedData byte The byte array to encrypt.
return byte[]
    public byte[] EncryptData(byte[] UnencryptedData)
    {
      int count = UnencryptedData.Length;
      int full = (count / BlockSizeByte) * BlockSizeByte;
      int rem = count - full;
      int total = full + BlockSizeByte;

      byte[] output = new byte[BlockSizeByte + total];
      _enc_iv.CopyTo(output, 0);
      _enc.TransformBlock(UnencryptedData, 0, full, output, BlockSizeByte);

      Buffer.BlockCopy(UnencryptedData, full, _temp, 0, rem);
      byte padding = (byte) (BlockSizeByte - rem);
      for(int i = rem; i < BlockSizeByte; i++) {
        _temp[i] = padding;
      }

      _enc.TransformBlock(_temp, 0, BlockSizeByte, output, output.Length - BlockSizeByte);
      _enc_iv.CopyTo(output, 0);
      Buffer.BlockCopy(output, output.Length - BlockSizeByte, _enc_iv, 0, BlockSizeByte);

      return output;
    }

Usage Example

Example #1
0
        ///<summary>Encrypts the packet given a SymmetricEncryption.</summary>
        public void Encrypt(SymmetricEncryption se)
        {
            byte[] to_encrypt = new byte[4 + _data.Length + _signature.Length];
            int    pos        = 0;

            NumberSerializer.WriteInt(_data.Length, to_encrypt, pos);
            pos += 4;
            _data.CopyTo(to_encrypt, pos);
            pos += _data.Length;
            _signature.CopyTo(to_encrypt, pos);
            _encrypted_data  = se.EncryptData(to_encrypt);
            _update_icpacket = true;
        }
All Usage Examples Of Brunet.Security.SymmetricEncryption::EncryptData