Using X509 Certificate to Encrypt Data

Do you have any examples of using the Chilkat Encryption component with X509 Digital
Certificates to encrypt and decrypt data ?

To use an X509 certificate, the CryptAlgorithm is set to “PKI” and you set the certificate by calling SetEncryptCert.
The encryption algorithm may be controlled by using a Chilkat CSP object and calling SetCSP.

Here’s a quick example showing the use of SetEncryptCert:
Encrypt with X509 Certificate

How to Encrypt URL Query Parameters

Here’s a new example:

ASP: Encrypt URL Query Parameters
SQL Server: Encrypt URL Query Parameters
C#: Encrypt URL Query Parameters
MFC: Encrypt URL Query Parameters
Delphi: Encrypt URL Query Parameters
Visual FoxPro: Encrypt URL Query Parameters
Java: Encrypt URL Query Parameters
Perl: Encrypt URL Query Parameters
PHP: Encrypt URL Query Parameters
Python: Encrypt URL Query Parameters
Ruby: Encrypt URL Query Parameters
VB.NET: Encrypt URL Query Parameters
Visual Basic: Encrypt URL Query Parameters
VBScript: Encrypt URL Query Parameters

C# Encrypting/Decrypting with Stream

This C# example demonstrates how to use Chilkat.Crypt2 to encrypt and decrypting using the .NET FileStream class:

    Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
    bool success = crypt.UnlockComponent("Anything for 30-day trial");
    if (!success)

    crypt.CryptAlgorithm = "aes";
    crypt.CipherMode = "cbc";
    crypt.KeyLength = 128;

    crypt.SetEncodedIV("0000000000000000", "hex");
    crypt.SetEncodedKey("abcdefghijklmnop", "ascii");

    // Open input and output files as file streams...
    FileStream fsIn = File.OpenRead("hamlet.xml");
    FileStream fsOut = File.Create("encrypted.dat");

    crypt.FirstChunk = true;
    crypt.LastChunk = false;
    byte [] encryptedChunk;

    // Encrypt the stream in 1024 byte chunks.
    byte[] b = new byte[1024];

    int n;
    while ((n = fsIn.Read(b, 0, b.Length)) > 0)
        if (n < b.Length)
            // Don't encrypt the full 1024 bytes, only the amount read...
            byte[] tmp = new byte[n];
            int i;
            for (i = 0; i < n; i++) tmp[i] = b[i];
            encryptedChunk = crypt.EncryptBytes(tmp);
            encryptedChunk = crypt.EncryptBytes(b);

        fsOut.Write(encryptedChunk, 0, encryptedChunk.Length);
        crypt.FirstChunk = false;

    // Flush any remaining encrypted data.
    crypt.LastChunk = true;
    byte[] empty = { };
    encryptedChunk = crypt.EncryptBytes(empty);
    if (encryptedChunk.Length > 0)
        fsOut.Write(encryptedChunk, 0, encryptedChunk.Length);

    // Now decrypt in chunks.  The decryptor must know when the last
    // block is being processed so it may unpad it correctly.

    System.IO.FileInfo fi = new System.IO.FileInfo("encrypted.dat");
    long fileSize = fi.Length;
    int numChunks = (int)fileSize / b.Length;

    fsIn = File.OpenRead("encrypted.dat");
    fsOut = File.Create("decrypted.xml");

    crypt.FirstChunk = true;
    crypt.LastChunk = false;
    byte[] decryptedChunk;

    int idx;
    for (idx = 0; idx <= numChunks; idx++)
        n = fsIn.Read(b, 0, b.Length);

        // Is this the last chunk?
        if (idx == numChunks)
            // Decrypt only the amount read...
            byte[] lastBlock = new byte[n];
            int i;
            for (i = 0; i < n; i++) lastBlock[i] = b[i];
            crypt.LastChunk = true;
            decryptedChunk = crypt.DecryptBytes(lastBlock);
            decryptedChunk = crypt.DecryptBytes(b);

        fsOut.Write(decryptedChunk, 0, decryptedChunk.Length);
        crypt.FirstChunk = false;


C# Blowfish Test Vectors

C# to match the Blowfish test vectors at this URL:

    // keys
    string[] key = new string[] { 
        "FEDCBA9876543210" };

    string[] plainText = new string[] { 

    string[] cipherBytes = new string[] { 
        "6B5C5A9C5D9E0A5A" };

    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");

    //  Attention: use "blowfish2" for the algorithm name:
    crypt.CryptAlgorithm = "blowfish2";

    //  CipherMode may be "ecb", "cbc", or "cfb"
    crypt.CipherMode = "ecb";

    //  KeyLength (in bits) may be a number between 32 and 448.
    //  128-bits is usually sufficient.  The KeyLength must be a
    //  multiple of 8.
    crypt.KeyLength = 64;

    //  The padding scheme determines the contents of the bytes
    //  that are added to pad the result to a multiple of the
    //  encryption algorithm's block size.  Blowfish has a block
    //  size of 8 bytes, so encrypted output is always
    //  a multiple of 8.
    crypt.PaddingScheme = 3;

    //  EncodingMode specifies the encoding of the output for
    //  encryption, and the input for decryption.
    //  It may be "hex", "url", "base64", or "quoted-printable".
    crypt.EncodingMode = "hex";

    int numTests = key.Length;
    int i;

    for (i = 0; i < numTests; i++)
        crypt.SetEncodedKey(key[i], "hex");

        string encStr = crypt.EncryptEncoded(plainText[i]);
        textBox1.Text += encStr + " should equal " + cipherBytes[i] + "\r\n";

Matching DES Encryption in Java and VBScript

This page shows both Java JCE DES encryption examples, as well as examples using the Chilkat Encryption component in both Java and VBScript. Each of these examples produces identical output:

The string: ABC123 encrypts to 6DA02B6AE1EA32D8 (as a hexidecimalized string)

Java bytes-to-hex Encoding Utility Function

 	public static String toHex (byte buf[]) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));

      return strbuf.toString();

56-bit DES Encryption Java JCE Example 1

        String password = "12345678";
        byte[] keyBytes = password.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        String original = "ABC123";
        byte[] inBytes = original.getBytes();
        byte[] encrypted = cipher.doFinal(inBytes);
        String dStr = decrypt("12345678",encrypted);

56-bit DES Encryption Java JCE Example 2

        DESKeySpec ks = new DESKeySpec( keyBytes );
        SecretKeyFactory kf = SecretKeyFactory.getInstance( "DES" );
        SecretKey ky = kf.generateSecret( ks );
        Cipher cf = Cipher.getInstance( "DES" );
        cf.init( Cipher.ENCRYPT_MODE, ky );
        byte[] encrypted2 = cipher.doFinal(inBytes);

56-bit DES Encryption Java w/ Chilkat

    CkCrypt2 crypt = new CkCrypt2();
    crypt.UnlockComponent("30-day Trial");
	CkByteData inData = new CkByteData();
	CkString encryptedStr = new CkString();

56-bit DES Encryption VBScript w/ Chilkat

set crypt = CreateObject("Chilkat.Crypt2")

crypt.UnlockComponent "30-day Trial"

crypt.CryptAlgorithm = "DES"
crypt.CipherMode = "ECB"
crypt.KeyLength = 64
crypt.EncodingMode = "hex"
crypt.SetEncodedKey "12345678","ascii"

MsgBox crypt.EncryptStringENC("ABC123")

56-bit DES Decryption in Java JCE

    public static String decrypt(String password, byte[] encodedData)
      throws Exception

        byte[] originalData = null;

        SecretKeySpec key = new SecretKeySpec(password.getBytes (), "DES");
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        originalData = cipher.doFinal(encodedData);

        String s = new String(originalData);
        return s;