Skip to main content

mke_encode_chunking

//-----------------------
// Set encoded file name
//-----------------------
string encodedFileName = "encodedText";

//-----------------------------------
// Create default MTE MKE and Status
//-----------------------------------
MteMkeEnc mkeEncoder = new MteMkeEnc();
MteStatus encoderStatus;

//------------------------------------------------------------------------
// Set identifier
// 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";

string fPath = string.Empty;
//--------------------------------------------------
// Prompt for path till we have one and it is valid
//--------------------------------------------------
while (string.IsNullOrWhiteSpace(fPath) || !File.Exists(fPath))
{
//------------------------------------
// Prompting message for file to copy
//------------------------------------
Console.WriteLine("Please enter path to file\n");

fPath = Console.ReadLine();
//-------------------------------
// Check to make sure file exits
//-------------------------------
if (!File.Exists(fPath))
{
Console.WriteLine($"File at the path '{fPath}' does not exist");
}
}
//----------------------------------------
// Set the correct extension for the file
//----------------------------------------
encodedFileName = $"{encodedFileName}{Path.GetExtension(fPath)}";

//---------------------------------------------------------------------
// Check how long entropy we need, set default all 0's
// should be treated like encryption keys - this is just example
//---------------------------------------------------------------------
int entropyMinBytes = mkeEncoder.GetDrbgsEntropyMinBytes(mkeEncoder.GetDrbg());
string entropy = (entropyMinBytes > 0) ? new String('0', entropyMinBytes) : entropy;

//--------------------------------
// Set MKE values for the Encoder
//--------------------------------
mkeEncoder.SetEntropy(Encoding.UTF8.GetBytes(entropy));
mkeEncoder.SetNonce(0);

//-------------------------
// Initialize MKE Encoder
//-------------------------
encoderStatus = mkeEncoder.Instantiate(identifier);
if (encoderStatus != MteStatus.mte_status_success)
{
throw new ApplicationException($"Failed to initialize the MTE encoder engine. Status: " +
$"{mkeEncoder.GetStatusName(encoderStatus)} / {mkeEncoder.GetStatusDescription(encoderStatus)}");
}
//-----------------------------
// Initialize chunking session
//-----------------------------
encoderStatus = mkeEncoder.StartEncrypt();
if(encoderStatus != MteStatus.mte_status_success)
{
throw new Exception("Failed to start encode chunk. Status: "
+ mkeEncoder.GetStatusName(encoderStatus)+ " / "
+ mkeEncoder.GetStatusDescription(encoderStatus));
}
//-------------------------------------------------------
// Before we start we want to delete any files
// that are already there in order to create new ones
//-------------------------------------------------------
if (File.Exists(encodedFileName))
{
File.Delete(encodedFileName);
}
//------------------------------------
// Read file in and encode using MKE
//------------------------------------
using (FileStream stream = File.OpenRead(fPath))
using (FileStream writeStream = File.OpenWrite(encodedFileName))
{
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)
{
//-------------------------------
// Encode the data in place
// encoded data put back in buffer
//-------------------------------
MteStatus chunkStatus = mkeEncoder.EncryptChunk(buffer, 0, bytesRead);
if(chunkStatus != MteStatus.mte_status_success)
{
throw new Exception("Failed to encode chunk. Status: "
+ mkeEncoder.GetStatusName(chunkStatus)+ " / "
+ mkeEncoder.GetStatusDescription(chunkStatus));
}
writeStream.Write(buffer, 0, bytesRead);
}
//-----------------------------
// Finish the chunking session
//-----------------------------
byte[] finalEncodedChunk = mkeEncoder.FinishEncrypt(out MteStatus finishStatus);
if(finishStatus != MteStatus.mte_status_success)
{
throw new Exception("Failed to finish encode chunk. Status: "
+ mkeEncoder.GetStatusName(finishStatus)+ " / "
+ mkeEncoder.GetStatusDescription(finishStatus));
}
//-----------------------------------
// Append the final data to the file
//-----------------------------------
writeStream.Write(finalEncodedChunk, 0, finalEncodedChunk.Length);
}