This C# example program demonstrates how the Chilkat 3DES implementation produces the same results as the .NET Framework implementation of 3DES.
The TripleDesEncryptOneBlock method, which uses the .NET TripleDESCryptoServiceProvider class can be found here.
// Compare Chilkat 168-bit 3DES results with .NET's 3DES results.
public void CompareTripleDes()
{
Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
bool success;
success = crypt.UnlockComponent("Anything for 30-day trial");
if (success != true)
{
MessageBox.Show("Crypt component unlock failed");
return;
}
// Set the encryption algorithm to 168-bit 3DES (Electronic Cookbook Mode):
// Note: We still use the string "des" for the algorithm.
// The KeyLength of 168 indicates that it's really Triple-DES.
crypt.CryptAlgorithm = "des";
crypt.KeyLength = 168;
crypt.CipherMode = "ecb";
// PAD with NULLs. If input is already a multiple of the block size (8 bytes), then no padding is added.
// This will be the case here, so there is no padding..
crypt.PaddingScheme = 3;
int i;
for (i = 0; i < 16; i++)
{
// Get 24 bytes of key and 8 bytes of plain-text.
byte[] key = Get24RandomBytes();
byte[] plainText = GetRandomBytes();
// Encrypt using .NET:
byte[] dnEncrypted = TripleDesEncryptOneBlock(plainText, key);
// Encrypt using Chilkat:
crypt.SecretKey = key;
byte[] ckEncrypted = crypt.EncryptBytes(plainText);
// Encode to a hex string and display for comparison:
string dnHex = crypt.Encode(dnEncrypted, "hex");
string ckHex = crypt.Encode(ckEncrypted, "hex");
string keyHex = crypt.Encode(key, "hex");
string plainTextHex = crypt.Encode(plainText, "hex");
textBox1.Text += "key=" + keyHex + " , pt=" + plainTextHex + "\r\n";
textBox1.Text += "enc=" + dnHex + " = " + ckHex + "\r\n\r\n";
}
}
The output of this program is this:
key=E89E43FD42E41EC407D994C20BDB2D47D273825BF7B145E6 , pt=8E9CA6DDFC31E40A
enc=2743EEAC46CAF5E0 = 2743EEAC46CAF5E0
key=5A38481B3505E51080553EDFB74BFE628697508A3037F5C1 , pt=E69E04A70CC12C6E
enc=454104C705D29AAC = 454104C705D29AAC
key=8277607F31E0EF12BFB00246DEECCB01BD70436B4E3CD5ED , pt=5823BA741AC927E3
enc=14DB5802CE496DD7 = 14DB5802CE496DD7
key=10C4E439CE8A361734D4E83E17305A7A1E23944112DED602 , pt=D82A2D645E2BDAD1
enc=9EFE481C1417972A = 9EFE481C1417972A
key=1CF9156BE911AF51A8A33EE62C3BA5FFC8922B9F931AE478 , pt=5A05CBE4BC356E4E
enc=94704CC040F31999 = 94704CC040F31999
key=042BE26F91C0A8722B8DC5DEB3BDE85F94FCDD8E90ECADCC , pt=B33005990C42AA2E
enc=6A9102469A017E90 = 6A9102469A017E90
key=AFB828EDE70032D9A3A429615BF2198373C3162E13ADC84D , pt=F7B16CCFC6CC3EB9
enc=33087A0E5B7583FE = 33087A0E5B7583FE
key=C258F6DD10F46A99D4251584428AF60DC7994C77B3E6C2E5 , pt=9E21EAA1015235C4
enc=3D36A7A220EC2B2B = 3D36A7A220EC2B2B
key=264208FB498673C37864642DFA78ADFE5E265972257EF620 , pt=389D06842B239F64
enc=FB75295688CCB4BD = FB75295688CCB4BD
key=43DE3BD5263D5932B10E3D37AD06A3FB31DE9E8E423D814F , pt=A1A0AE33CD30A7B4
enc=BF7D25B3A914957B = BF7D25B3A914957B
key=C794FE9834144E20045A57597BCE40BBF97B45DFEB374E2A , pt=41CB5958BBD5BA0A
enc=717DC95674BC0C58 = 717DC95674BC0C58
key=36C3DA7B427EF495675EEA595E671B751276B7D488C359CC , pt=30308A1C935ED5AA
enc=A33F95C898B9469C = A33F95C898B9469C
key=22CAC7B5D522FEDD9DA06F3A52B4278272C0B49E456FA800 , pt=AE00FEBE066CEE93
enc=EB182CD1CF1D6178 = EB182CD1CF1D6178
key=D381A7A06036A32007F3481938E4D78F00A9956328CE6A6C , pt=35AC9C9053A7049F
enc=3E0FA7C3B521840B = 3E0FA7C3B521840B
key=E3288726BD8F503832C9E8ACA200999891CAA9321166F929 , pt=D707DB5D320FB882
enc=9D9667E5209D9F25 = 9D9667E5209D9F25
key=2A7081F7878A789B24C75DF6EB582677386E383DA7366531 , pt=F24198B5BDF7764E
enc=18E935BAFAFD5156 = 18E935BAFAFD5156