Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML-to-XML
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...
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

ARC4 Encryption (ARCFOUR)

Demonstrates simple ARC4 encryption to match some simple test vectors published by Wikipedia. ARC4 is a fast stream cipher supporting key lengths from 8 to 2048 bits (i.e. from 1 to 256 bytes). It is not a block cipher, so there is no padding, no modes, and no initialization vector.

Download Chilkat Java Library

Download Chilkat Java x64 Library

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;
    }

    //  Set the encryption algorithm to ARC4:
    crypt.put_CryptAlgorithm("arc4");

    //  We want the encrypted output to be a hex-encoded string.
    crypt.put_EncodingMode("hex");

    //  Encrypt some test vectors from Wikipedia:

    String cipherText;
    String plainText;

    //  The key length (in bits) is equal to the number of us-ascii
    //  bytes in our key string * 8.

    // ARC4( "Key", "Plaintext" ) == BBF316E8D940AF0AD3
    crypt.put_KeyLength(24);
    crypt.SetEncodedKey("Key","ascii");
    cipherText = crypt.encryptStringENC("Plaintext");
    System.out.println(cipherText);
    plainText = crypt.decryptStringENC(cipherText);
    System.out.println(plainText);

    // ARC4( "Wiki", "pedia" ) == 1021BF0420
    crypt.put_KeyLength(32);
    crypt.SetEncodedKey("Wiki","ascii");
    cipherText = crypt.encryptStringENC("pedia");
    System.out.println(cipherText);
    plainText = crypt.decryptStringENC(cipherText);
    System.out.println(plainText);

    // ARC4( "Secret", "Attack at dawn" ) == 45A01F645FC35B383552544B9BF5
    crypt.put_KeyLength(48);
    crypt.SetEncodedKey("Secret","ascii");
    cipherText = crypt.encryptStringENC("Attack at dawn");
    System.out.println(cipherText);
    plainText = crypt.decryptStringENC(cipherText);
    System.out.println(plainText);

    //  Note: The call to SetEncodedKey serves to set the key
    //  to the us-ascii bytes of the string.

  }
}

 

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

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