Programming Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C Examples

Bounced Mail
Bz2
Certificates/Keys
Charset
CSV
DKIM / DomainKey
Diffie-Hellman
DSA
Email Object
Encryption
FileAccess
FTP
HTML Conversion
HTTP
IMAP
MHT / HTML Email
MIME
NTLM
POP3
RSA
SMTP
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
Tar
Upload
XML
Zip
Amazon S3

 

 

 

 

 

 

 

 

File Encryption / Decryption

File-to-file encryption in C using AES, Blowfish, RC2, ARC4, or 3DES.

Downloads:

MS Windows Visual C/C++ Libraries
Linux/CentOS C/C++ Libraries
MAC OS X C/C++ Libraries
Solaris C/C++ Libraries
C++ Builder Libraries
FreeBSD C++ Libraries
HP-UX C++ Libraries
BlackBerry QNX C++ Libraries
#include <C_CkCrypt2.h>

void ChilkatSample(void)
    {
    HCkCrypt2 crypt;
    BOOL success;
    const char * key;
    const char * iv;

    crypt = CkCrypt2_Create();

    success = CkCrypt2_UnlockComponent(crypt,"Anything for 30-day trial");
    if (success != TRUE) {
        printf("Crypt component unlock failed\n");
        return;
    }

    CkCrypt2_putCryptAlgorithm(crypt,"aes");
    CkCrypt2_putCipherMode(crypt,"cbc");
    CkCrypt2_putKeyLength(crypt,128);

    //  16 bytes of key for 128-bit encryption.
    key = "1234567890123456";

    //  The IV is equal to the block size of the encryption algorithm.

    iv = "1234567890123456";

    //  Set the key.
    CkCrypt2_SetEncodedKey(crypt,key,"ascii");

    //  Set the IV
    CkCrypt2_SetEncodedIV(crypt,iv,"ascii");

    //  AES Encrypt the file (the file may be any size because it will
    //  stream the file in/out.
    success = CkCrypt2_CkEncryptFile(crypt,"hamlet.xml","aesEncrypted.dat");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    //  AES Decrypt the file (the file may be any size because it will
    //  stream the file in/out.
    success = CkCrypt2_CkDecryptFile(crypt,"aesEncrypted.dat","hamlet_aes.xml");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    printf("AES File Encryption Success.\n");

    //  Now do 3DES file encryption:

    //  To use Triple-DES, set the algorithm = "des",
    //  and the key length = 168.
    //  To use DES, set the key length = 56 bits.
    CkCrypt2_putCryptAlgorithm(crypt,"des");
    CkCrypt2_putCipherMode(crypt,"cbc");
    CkCrypt2_putKeyLength(crypt,168);

    //  3DES Encrypt the file
    success = CkCrypt2_CkEncryptFile(crypt,"hamlet.xml","tripleDesEncrypted.dat");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    //  3DES Decrypt the file
    success = CkCrypt2_CkDecryptFile(crypt,"tripleDesEncrypted.dat","hamlet_3des.xml");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    printf("3DES File Encryption Success.\n");

    //  Do Blowfish file encryption:

    //  To use Blowfish, set the algorithm = "blowfish2".
    //  The original Chilkat "blowfish" implementation outputs
    //  4321 swapped bytes.  "blowfish2" output is in the correct
    //  byte order.
    CkCrypt2_putCryptAlgorithm(crypt,"blowfish2");
    CkCrypt2_putCipherMode(crypt,"cbc");
    CkCrypt2_putKeyLength(crypt,128);

    //  Blowfish Encrypt the file
    success = CkCrypt2_CkEncryptFile(crypt,"hamlet.xml","blowfishEncrypted.dat");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    //  Blowfish Decrypt the file
    success = CkCrypt2_CkDecryptFile(crypt,"blowfishEncrypted.dat","hamlet_blowfish.xml");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    printf("Blowfish File Encryption Success.\n");

    //  Do RC2 file encryption:

    //  To use RC2, set the algorithm = "rc2".
    //  Also, set the Rc2EffectiveKeyLength property.
    CkCrypt2_putCryptAlgorithm(crypt,"rc2");
    CkCrypt2_putCipherMode(crypt,"cbc");
    //  Key length and effective key length should range
    //  from 8 to 1024 bits.
    CkCrypt2_putKeyLength(crypt,128);
    CkCrypt2_putRc2EffectiveKeyLength(crypt,128);

    //  RC2 Encrypt the file
    success = CkCrypt2_CkEncryptFile(crypt,"hamlet.xml","rc2Encrypted.dat");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    //  RC2 Decrypt the file
    success = CkCrypt2_CkDecryptFile(crypt,"rc2Encrypted.dat","hamlet_rc2.xml");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    printf("RC2 File Encryption Success.\n");

    //  Do ARC4 file encryption:

    //  To use ARC4, set the algorithm = "arc4".
    CkCrypt2_putCryptAlgorithm(crypt,"arc4");
    CkCrypt2_putKeyLength(crypt,128);

    //  ARC4 Encrypt the file
    success = CkCrypt2_CkEncryptFile(crypt,"hamlet.xml","arc4Encrypted.dat");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    //  ARC4 Decrypt the file
    success = CkCrypt2_CkDecryptFile(crypt,"arc4Encrypted.dat","hamlet_arc4.xml");
    if (success != TRUE) {
        printf("%s\n",CkCrypt2_lastErrorText(crypt));
        return;
    }

    printf("ARC4 File Encryption Success.\n");

    CkCrypt2_Dispose(crypt);

    }

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.