Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let crypt = CkoCrypt2()!

    crypt.cryptAlgorithm = "aes"
    crypt.cipherMode = "cbc"
    crypt.keyLength = 128
    crypt.paddingScheme = 0

    var ivHex: String? = "000102030405060708090A0B0C0D0E0F"
    crypt.setEncodedIV(ivStr: ivHex, encoding: "hex")

    var keyHex: String? = "00010203040506071011121314151617"
    crypt.setEncodedKey(keyStr: keyHex, encoding: "hex")

    var dataFile: String? = "qa_data/zips/HBIQ040615300005.ZIP"
    var outFile: String? = "qa_output/HBIQ040615300005.enc"
    var outFile2: String? = "qa_output/HBIQ040615300005.ZIP"

    success = crypt.ckEncryptFile(srcFile: dataFile, destFile: outFile)
    success = crypt.ckDecryptFile(srcFile: outFile, destFile: outFile2)

    let fac = CkoFileAccess()!
    var bEqual: Bool = fac.fileContentsEqual(path1: dataFile, path2: outFile2)
    if bEqual != true {
        print("Decrypted file not equal to the original.")
    }
    else {
        print("Success.")
    }


}