Sample code for 30+ languages & platforms
Java

Encrypt / Decrypt a File and Verify it has not Changed

See more Encryption Examples

Demonstrates how to encrypt and decrypt a file, and verify it has not changed.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.FileAccess;

public class ChilkatExample {

    public static void main(String[] args) {
        // This requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Crypt2 crypt = new Crypt2();

        crypt.setCryptAlgorithm("aes");
        crypt.setCipherMode("cbc");
        crypt.setKeyLength(128);
        crypt.setPaddingScheme(0);

        String ivHex = "000102030405060708090A0B0C0D0E0F";
        crypt.setEncodedIV(ivHex, "hex");

        String keyHex = "00010203040506071011121314151617";
        crypt.setEncodedKey(keyHex, "hex");

        String dataFile = "qa_data/zips/HBIQ040615300005.ZIP";
        String outFile = "qa_output/HBIQ040615300005.enc";
        String outFile2 = "qa_output/HBIQ040615300005.ZIP";

        crypt.ckEncryptFile(dataFile, outFile);
        crypt.ckDecryptFile(outFile, outFile2);

        FileAccess fac = new FileAccess();
        boolean bEqual = fac.fileContentsEqual(dataFile, outFile2);
        if (!bEqual) {
            System.out.println("Decrypted file not equal to the original.");
        } else {
            System.out.println("Success.");
        }
    }
}