Skip to main content

chunk-decoding

//---------------------------------------
// Create MTE MKE Decoder as shown above
//---------------------------------------

// Initialize chunking session
MteStatus decoderStatus = mkeDecoder.StartDecrypt();
if(decoderStatus != MteStatus.mte_status_success)
{
// MTE start chunking was not successful, handle failure appropriately
// Below is only an example
throw new Exception("Failed to start decode chunk. Status: "
+ mkeDecoder.GetStatusName(decoderStatus)+ " / "
+ mkeDecoder.GetStatusDescription(decoderStatus));
}

//------------------------------------------------------------------
// For each chunk of data to decode there are two options available
//------------------------------------------------------------------
// When the byte[] that is passed in is NOT the size of
// the amount of data inside of it use the full method
// DecryptChunk(byte[] dataToBeDecoded,
// int offset,
// int sizeOfDataToBeDecoded,
// byte[] returnedDecodedData,
// int offset)
//--------------------------------------------------------------------
// Option One
// Get what the decoded data size max will be to allocate byte[] for return data
var cipherBlocks = mkeDecoder.GetCiphersBlockBytes(mkeDecoder.GetCipher());
int buffBytes = bytesRead - cipherBlocks;
byte[] decodedData = new byte[buffBytes];

// Decode chunk, if int returned is -1 there was an error
int decryptError = mkeDecoder.DecryptChunk(encodedBytes, 0, encodedBytesSize, decodedData, 0);
if (decryptError < 0)
{
// Decoding chunk unsuccessful and cannot continue, handle failure appropriately
// Below is only an example
throw new ApplicationException("Error decoding data.");
}
//----------------------------------------------------------------------
// Option Two
// When encodedBytes byte[] same size as data inside
byte[] decodedData = mkeDecoder.DecryptChunk(encodedBytes);

// Finish the chunking session, this may or may not have data returned
// If no data is returned "finishDecrypt" will be null
byte[] finalDecodedChunk = mkeDecoder.FinishDecrypt(out MteStatus finishStatus);
if(finishStatus != MteStatus.mte_status_success)
{
// Decode chunk unsuccessful and cannot continue, handle failure appropriately
// Below is only an example
throw new Exception("Failed to finish decode chunk. Status: "
+ mkeDecoder.GetStatusName(finishStatus)+ " / "
+ mkeDecoder.GetStatusDescription(finishStatus));
}