Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#C# WinRTDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPythonRubySQL ServerSwiftUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET WinRTVBScript

Java Examples

Certificates
Email Object
Encryption
FTP
HTML-to-XML/Text
Gzip
HTTP
IMAP
MHT / HTML Email
PFX/P12
Java KeyStore (JKS)
POP3
RSA Encryption
MIME
SMTP
Socket/SSL/TLS
SSH Key
SSH
SFTP
Tar Archive
XML
XMP
Zip

More Examples...
Amazon S3
Bounced Email
CSV
Diffie-Hellman
DKIM / DomainKey
NTLM

 

 

 

 

 

 

 

File Encryption / Decryption

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

Chilkat Java Downloads

Java Libs for Windows, Linux, MAC OS X, Solaris,
ARM Embedded Linux, and PowerLinux

Java Libs for Android

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkCrypt2 crypt = new CkCrypt2();

    boolean success;
    success = crypt.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        System.out.println("Crypt component unlock failed");
        return;
    }

    crypt.put_CryptAlgorithm("aes");
    crypt.put_CipherMode("cbc");
    crypt.put_KeyLength(128);

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

    //  The IV is equal to the block size of the encryption algorithm.
    String iv;
    iv = "1234567890123456";

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

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

    //  AES Encrypt the file (the file may be any size because it will
    //  stream the file in/out.
    success = crypt.CkEncryptFile("hamlet.xml","aesEncrypted.dat");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  AES Decrypt the file (the file may be any size because it will
    //  stream the file in/out.
    success = crypt.CkDecryptFile("aesEncrypted.dat","hamlet_aes.xml");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    System.out.println("AES File Encryption Success.");

    //  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.
    crypt.put_CryptAlgorithm("des");
    crypt.put_CipherMode("cbc");
    crypt.put_KeyLength(168);

    //  3DES Encrypt the file
    success = crypt.CkEncryptFile("hamlet.xml","tripleDesEncrypted.dat");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  3DES Decrypt the file
    success = crypt.CkDecryptFile("tripleDesEncrypted.dat","hamlet_3des.xml");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    System.out.println("3DES File Encryption Success.");

    //  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.
    crypt.put_CryptAlgorithm("blowfish2");
    crypt.put_CipherMode("cbc");
    crypt.put_KeyLength(128);

    //  Blowfish Encrypt the file
    success = crypt.CkEncryptFile("hamlet.xml","blowfishEncrypted.dat");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  Blowfish Decrypt the file
    success = crypt.CkDecryptFile("blowfishEncrypted.dat","hamlet_blowfish.xml");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    System.out.println("Blowfish File Encryption Success.");

    //  Do RC2 file encryption:

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

    //  RC2 Encrypt the file
    success = crypt.CkEncryptFile("hamlet.xml","rc2Encrypted.dat");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  RC2 Decrypt the file
    success = crypt.CkDecryptFile("rc2Encrypted.dat","hamlet_rc2.xml");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    System.out.println("RC2 File Encryption Success.");

    //  Do ARC4 file encryption:

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

    //  ARC4 Encrypt the file
    success = crypt.CkEncryptFile("hamlet.xml","arc4Encrypted.dat");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    //  ARC4 Decrypt the file
    success = crypt.CkDecryptFile("arc4Encrypted.dat","hamlet_arc4.xml");
    if (success != true) {
        System.out.println(crypt.lastErrorText());
        return;
    }

    System.out.println("ARC4 File Encryption Success.");


  }
}

 

This code sample uses the Chilkat Java Library for Windows, Linux, Android, ARM Embedded Linux, MAC OS X, Solaris, and PowerLinux

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