Skip to main content

mke_encode_decode

using System;
using System.Text;

namespace MTE {
class demoCsMke {
// The timestamp window and sequencing window are hard coded for this
// demo.
private const UInt64 timestampWindow = 1;
private const Int32 sequenceWindow = 0;

static int Main(string[] args) {
// Status.
MteStatus status = MteStatus.mte_status_success;

// Options.
MteDrbgs drbg;
int tokBytes;
MteVerifiers verifiers;
MteCiphers cipher;
MteHashes hash;

// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license
// info from the environment if required.
MteBase baseObj = new MteBase();
if (!baseObj.InitLicense("YOUR_COMPANY", "YOUR_LICENSE")) {
string company = Environment.GetEnvironmentVariable("MTE_COMPANY");
string license = Environment.GetEnvironmentVariable("MTE_LICENSE");
if (company == null || license == null ||
!baseObj.InitLicense(company, license)) {
status = MteStatus.mte_status_license_error;
Console.Error.WriteLine("License init error ({0}): {1}",
baseObj.GetStatusName(status),
baseObj.GetStatusDescription(status));
return (int)status;
}
}

// Get the input data.
Console.Write("Enter the data to encode> ");
string input = Console.ReadLine();

// Get the personalization string.
Console.Write("Enter the personalization> ");
string personal = Console.ReadLine();

// Set up the options to use the buildtime options if the library is built
// for that; otherwise prompt for the options.
if (baseObj.HasRuntimeOpts()) {
// Get the DRBG.
Console.Write("DRBGs:");
for (int i = 1; i < baseObj.GetDrbgsCount(); ++i) {
Console.Write(" {0}", baseObj.GetDrbgsName((MteDrbgs)i));
}
Console.WriteLine();
Console.Write("Enter the DRBG> ");
drbg = baseObj.GetDrbgsAlgo(Console.ReadLine());

// Get the token size.
Console.Write("Enter the token size in bytes> ");
tokBytes = Convert.ToInt32(Console.ReadLine());

// Get the verifiers.
Console.Write("Verifiers:");
for (int i = 0; i < baseObj.GetVerifiersCount(); ++i) {
Console.Write(" {0}", baseObj.GetVerifiersName((MteVerifiers)i));
}
Console.WriteLine();
Console.Write("Enter the verifiers> ");
verifiers = baseObj.GetVerifiersAlgo(Console.ReadLine());

// Get the cipher.
Console.Write("Ciphers:");
for (int i = 1; i < baseObj.GetCiphersCount(); ++i) {
Console.Write(" {0}", baseObj.GetCiphersName((MteCiphers)i));
}
Console.WriteLine();
Console.Write("Enter the cipher> ");
cipher = baseObj.GetCiphersAlgo(Console.ReadLine());

// Get the hash.
Console.Write("Hashes:");
for (int i = 1; i < baseObj.GetHashesCount(); ++i) {
Console.Write(" {0}", baseObj.GetHashesName((MteHashes)i));
}
Console.WriteLine();
Console.Write("Enter the hash> ");
hash = baseObj.GetHashesAlgo(Console.ReadLine());
} else {
drbg = baseObj.GetDefaultDrbg();
tokBytes = baseObj.GetDefaultTokBytes();
verifiers = baseObj.GetDefaultVerifiers();
cipher = baseObj.GetDefaultCipher();
hash = baseObj.GetDefaultHash();
}

// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
int entropyBytes = baseObj.GetDrbgsEntropyMinBytes(drbg);
byte[] entropy = new byte[entropyBytes];

// Create the Encoder.
MteMkeEnc encoder = new MteMkeEnc(drbg,
tokBytes,
verifiers,
cipher,
hash);
encoder.SetEntropy(entropy);
encoder.SetNonce(0);
status = encoder.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encoder instantiate error ({0}): " +
encoder.GetStatusDescription(status),
encoder.GetStatusName(status));
return (int)status;
}

// Encode the input.
string encoded = encoder.EncodeB64(input, out status);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encode error ({0}): {1}",
encoder.GetStatusName(status),
encoder.GetStatusDescription(status));
return (int)status;
}

// Display the encoded message.
Console.WriteLine("Base64 message: {0}", encoded);

// Create the Decoder.
MteMkeDec decoder = new MteMkeDec(drbg,
tokBytes,
verifiers,
cipher,
hash,
timestampWindow,
sequenceWindow);
decoder.SetEntropy(entropy);
decoder.SetNonce(0);
status = decoder.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decoder instantiate error ({0}): " +
decoder.GetStatusDescription(status),
decoder.GetStatusName(status));
return (int)status;
}

// Decode the message.
string decoded = decoder.DecodeStrB64(encoded, out status);
if (decoder.StatusIsError(status)) {
Console.Error.WriteLine("Decode error ({0}): {1}",
decoder.GetStatusName(status),
decoder.GetStatusDescription(status));
return (int)status;
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
decoder.GetStatusName(status),
decoder.GetStatusDescription(status));
}

// Output the decoded data.
Console.WriteLine("Decoded data: {0}", decoded);

// Compare the decoded data against the original data.
if (decoded == input) {
Console.WriteLine("The original data and decoded data match.");
} else {
Console.WriteLine("The original data and decoded data DO NOT match.");
return -1;
}

// Success.
return 0;
}
}
}