Java Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CUnicode C++Unicode CMFCDelphi DLLDelphi ActiveXFoxProJavaPerlPHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
SFTP
Signatures
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
Upload
XML
XMP
Zip

More Examples...
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

Match Java JCE AES CBC Encryption Results

Shows sample code in Java that matches Java JCE encryption results for 128-bit AES encryption using CBC mode. The equivalent Java JCE code is provided as commented-out code following the Java code.

 Chilkat Java Library Downloads for Windows, Linux, and MAC OS X

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

    // Any string argument automatically begins the 30-day trial.
    boolean success;
    success = crypt.UnlockComponent("30-day trial");
    if (success != true) {
        System.out.println("Crypt component unlock failed");
        return;
    }

    String password;
    password = "secretPassphrase";

    // Use the Digest MD5 algorithm to get a 16-byte binary secret key.
    crypt.put_HashAlgorithm("md5");
    crypt.put_EncodingMode("hex");
    String secretKeyHex;
    secretKeyHex = crypt.hashStringENC(password);

    crypt.put_CryptAlgorithm("aes");
    crypt.put_CipherMode("cbc");
    crypt.put_KeyLength(128);
    crypt.SetEncodedKey(secretKeyHex,"hex");
    crypt.SetEncodedIV(secretKeyHex,"hex");

    String text;
    text = "Though this be madness, yet there is method in 't";
    String encText;
    encText = crypt.encryptStringENC(text);

    // Displays:
    // 79c357db030ac9ae66a84528a2f387717c5f10b48ef4385e5fe7b9227b3eeb8a
    // 77f24900890e72e35758b1f79f8f78d51ebe94536c30c57fd7b2c17b927715d7

    System.out.println(encText);

    // 
    // This is the equivalent code using Java JCE:
    // 
    // String password = "secretPassphrase";
    // 
    // MessageDigest md = MessageDigest.getInstance("MD5");
    // md.update(password.getBytes("UTF-8"), 0, password.length());
    // byte[] rawKey = md.digest();
    // String text = "Though this be madness, yet there is method in 't";
    // 
    // SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
    // IvParameterSpec ivSpec = new IvParameterSpec(rawKey);
    // 
    // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    // cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
    // 
    // byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
    // 
    // System.out.println(toHex(encrypted));

    // This is the toHex method in Java:
    // 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("0");
    // 
    // strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    // }
    // 
    // return strbuf.toString();
    // }

  }
}

 

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