This C# example uses Chilkat.Crypt2 to do 56-bit DES encryption (ECB mode). It compares the results with those produced by the .NET Framework.
The DesEncryptOneBlock method, which uses the .NET DESCryptoServiceProvider class can be found here.
// Compare Chilkat 56-bit DES results with .NET's DES results.
public void CompareDes()
{
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 56-bit DES (Electronic Cookbook Mode):
crypt.CryptAlgorithm = "des";
crypt.KeyLength = 56;
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 8 bytes of key and plain-text.
byte[] key = GetRandomBytes();
byte[] plainText = GetRandomBytes();
// Encrypt using .NET:
byte[] dnEncrypted = DesEncryptOneBlock(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";
}
}
This is the output produced:
key=E89E43FD42E41EC4 , pt=07D994C20BDB2D47
enc=79C18B3259060B46 = 79C18B3259060B46
key=D273825BF7B145E6 , pt=8E9CA6DDFC31E40A
enc=A750C0D842E48C63 = A750C0D842E48C63
key=5A38481B3505E510 , pt=80553EDFB74BFE62
enc=E516EF2111181897 = E516EF2111181897
key=8697508A3037F5C1 , pt=E69E04A70CC12C6E
enc=55344C5210D0ADC5 = 55344C5210D0ADC5
key=8277607F31E0EF12 , pt=BFB00246DEECCB01
enc=A97199855BB12905 = A97199855BB12905
key=BD70436B4E3CD5ED , pt=5823BA741AC927E3
enc=03E8EBCF88FB74A9 = 03E8EBCF88FB74A9
key=10C4E439CE8A3617 , pt=34D4E83E17305A7A
enc=EFAAA0BF341303D5 = EFAAA0BF341303D5
key=1E23944112DED602 , pt=D82A2D645E2BDAD1
enc=1B507B7AB9FF27A1 = 1B507B7AB9FF27A1
key=1CF9156BE911AF51 , pt=A8A33EE62C3BA5FF
enc=5D210CBD737F123F = 5D210CBD737F123F
key=C8922B9F931AE478 , pt=5A05CBE4BC356E4E
enc=3272EBF476CC6780 = 3272EBF476CC6780
key=042BE26F91C0A872 , pt=2B8DC5DEB3BDE85F
enc=4E323A25497A22B6 = 4E323A25497A22B6
key=94FCDD8E90ECADCC , pt=B33005990C42AA2E
enc=AA04E23096018F06 = AA04E23096018F06
key=AFB828EDE70032D9 , pt=A3A429615BF21983
enc=E6355CC82F71346D = E6355CC82F71346D
key=73C3162E13ADC84D , pt=F7B16CCFC6CC3EB9
enc=0F44993441C6BE7A = 0F44993441C6BE7A
key=C258F6DD10F46A99 , pt=D4251584428AF60D
enc=092E381AD2ACEE80 = 092E381AD2ACEE80
key=C7994C77B3E6C2E5 , pt=9E21EAA1015235C4
enc=AE7FB83A89CA064E = AE7FB83A89CA064E