Pokemon
Pokemon

Reputation: 524

AES256 encrypt in Swift3

I had tried to do AES256 encryption in Swift3, by using many libraries like CryptoSwift, but couldnt get proper result.

    let aes = try AES(key: anykey, iv: "")
    let ciphertext = try aes.encrypt(data)

The mode I want to try is as below.

Algorithm: Rijndael-256
MODE:  ECB
IV: NULL

if there is mistake in my code, or any better way to AES256 encrypt in Swift3, answer me please.

Upvotes: 1

Views: 8777

Answers (2)

adamfowlerphoto
adamfowlerphoto

Reputation: 2751

You could also try the CommonCrypto C library which RNCryptor wraps. This comes as standard with XCode. Unfortunately it doesn't have a swift framework setup for it. This project creates a framework for easy use in swift https://github.com/sergejp/CommonCrypto/tree/master/CommonCrypto

You can find example code using the CommonCrypto library here https://github.com/adam-fowler/swift-library/blob/master/data/aes.swift.

Upvotes: 0

Brandon A
Brandon A

Reputation: 8279

RNCryptor is a useful framework for AES256 encryption and decryption.

// Encryption
let data ... // Some data you want to encrypt
let password = "0Bfy8q9475jgjjbsu"
let ciphertext = RNCryptor.encryptData(data, password: password)

// Decryption
do {
    let originalData = try RNCryptor.decryptData(ciphertext, password: password)

} catch let error {
    print("Can not Decrypt With Error: \n\(error)\n")
} 

Upvotes: 3

Related Questions