Skip to main content

mke_decode_chunking

string encodedFileName = "encodedText";
string decodedFileName = "decodedText";

//-----------------------------------------
// Get fPath from prompt in encoded sample
//-----------------------------------------
encodedFileName = $"{encodedFileName}{Path.GetExtension(fPath)}";
decodedFileName = $"{decodedFileName}{Path.GetExtension(fPath)}";
//--------------------------------
// Create default MTE MKE Decoder
//--------------------------------
MteMkeDec mkeDecoder = new MteMkeDec();
//---------------------------------------------------------------------------
// Set identifier (must be same as encoder)
// These values should be treated like encryption keys
// The following personalization string and nonce values are for
// demonstration only and should not be used in a production environment
//---------------------------------------------------------------------------
string identifier = "demo";
//---------------------------------------------------------------
// Check how long entropy we need, set default all 0's
// should be treated like encryption keys - this is just example
//---------------------------------------------------------------
int entropyMinBytes = mkeDecoder.GetDrbgsEntropyMinBytes(mkeDecoder.GetDrbg());
string entropy = (entropyMinBytes > 0) ? new String('0', entropyMinBytes) : entropy;

//---------------------------------
// Set MKE values for the Decoder
//---------------------------------
mkeDecoder.SetEntropy(Encoding.UTF8.GetBytes(entropy));
mkeDecoder.SetNonce(0);
//------------------------
// Initialize the Decoder
//------------------------
decoderStatus = mkeDecoder.Instantiate(identifier);
if (decoderStatus != MteStatus.mte_status_success)
{
throw new ApplicationException($"Failed to initialize the MTE decoder engine. "
+ $"Status: {mkeDecoder.GetStatusName(encoderStatus)} / "
+ $"{mkeDecoder.GetStatusDescription(encoderStatus)}");
}
//-----------------------------
// Initialize chunking session
//-----------------------------
MteStatus decoderStatus = mkeDecoder.StartDecrypt();
if(decoderStatus != MteStatus.mte_status_success)
{
throw new Exception("Failed to start decode chunk. Status: "
+ mkeDecoder.GetStatusName(decoderStatus)+ " / "
+ mkeDecoder.GetStatusDescription(decoderStatus));
}
//------------------------------------
// Read file in and decode using MKE
//------------------------------------
using (FileStream stream = File.OpenRead("encoded.txt"))
using (FileStream writeStream = File.OpenWrite("decoded.txt"))
{
BinaryReader reader = new BinaryReader(stream);
BinaryWriter writer = new BinaryWriter(writeStream);
//-----------------------------------
// Create a buffer to hold the bytes
//-----------------------------------
byte[] buffer = new Byte[1024];
int bytesRead;
//----------------------------------------
// While the read method returns bytes
// Keep writing them to the output stream
//----------------------------------------
while ((bytesRead = stream.Read(buffer, 0, 1024)) > 0)
{
//-------------------------------------------
// Ensure we are only decoding what was read
//-------------------------------------------
byte[] decodedData = new byte[0];
if (bytesRead == buffer.Length)
{
decodedData = _mkeDecoder.DecryptChunk(buffer);
}
else
{
//------------------------------------------
// Find out what the decoded length will be
//------------------------------------------
var cipherBlocks = _mkeDecoder.GetCiphersBlockBytes(_mkeDecoder.GetCipher());
int buffBytes = bytesRead - cipherBlocks;
//----------------------------------
// Allocate buffer for decoded data
//----------------------------------
decodedData = new byte[buffBytes];
int decryptError = _mkeDecoder.DecryptChunk(buffer, 0, bytesRead, decodedData, 0);
if (decryptError < 0)
{
throw new ApplicationException("Error decoding data.");
}
}
writeStream.Write(decodedData, 0, decodedData.Length);
}
//-----------------------------
// Finish the chunking session
//-----------------------------
byte[] finalDecodedChunk = mkeDecoder.FinishDecrypt(out MteStatus finishStatus);
if(finishStatus != MteStatus.mte_status_success)
{
throw new Exception("Failed to finish decode chunk. Status: "
+ mkeDecoder.GetStatusName(finishStatus)+ " / "
+ mkeDecoder.GetStatusDescription(finishStatus));
}
//-----------------------------------------------------------------------
// Check if there is additional bytes if not initialize empty byte array
//-----------------------------------------------------------------------
if(finalDecodedChunk.Length <=0) { finalDecodedChunk = new byte[0]; }
//------------------------------------
// Append the final data to the file
//------------------------------------
writeStream.Write(finalDecodedChunk, 0, finalDecodedChunk.Length);
}