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

DecryptData() public method

Takes in a byte array and returns the data in a unencrypted format.
public DecryptData ( byte EncryptedData ) : byte[]
EncryptedData byte The data to decrypt.
return byte[]
    public byte[] DecryptData(byte[] EncryptedData)
    {
      Brunet.Util.MemBlock iv_ref = Brunet.Util.MemBlock.Reference(EncryptedData, 0, BlockSizeByte);
      ICryptoTransform dec = (ICryptoTransform) _decryptors[iv_ref];
      if(dec == null) {
        byte[] iv = new byte[BlockSizeByte];
        iv_ref.CopyTo(iv, 0);
        dec = _sa.CreateDecryptor(_sa.Key, iv);
      } else {
        _decryptors.Remove(iv_ref);
      }

      int count = EncryptedData.Length - BlockSizeByte;
      iv_ref = Brunet.Util.MemBlock.Reference(EncryptedData, count, BlockSizeByte);
      _decryptors[iv_ref] = dec;


      if((count % BlockSizeByte) > 0 || count == 0) {
        throw new CryptographicException("Invalid input block size.");
      }

      byte[] output = new byte[count];
      dec.TransformBlock(EncryptedData, BlockSizeByte, count, output, 0);

      byte padding = output[count - 1];
      int length = count - padding;
      for(int i = length; i < count; i++) {
        if(output[i] != padding) {
          throw new CryptographicException(String.Format("Bad padding at position {0}.", i));
        }
      }

      byte[] res = new byte[length];
      Buffer.BlockCopy(output, 0, res, 0, length);
      return res;
    }

Usage Example

Example #1
0
        ///<summary>Decrypts the packet given a SymmetricEncryption returning true
        ///if it was able to decrypt it.</summary>
        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);
        }
All Usage Examples Of Brunet.Security.SymmetricEncryption::DecryptData