Tuesday, June 8, 2021

AES GCM iOS

AES-GCM

The result of Galois/Counter Mode (GCM) encryption is ciphertext and authentication tag, that is later used to decryption.


encryption


do {

    // In combined mode, the authentication tag is directly appended to the encrypted message. This is usually what you want.

    let gcm = GCM(iv: iv, mode: .combined)

    let aes = try AES(key: key, blockMode: gcm, padding: .noPadding)

    let encrypted = try aes.encrypt(plaintext)

    let tag = gcm.authenticationTag

} catch {

    // failed

}


decryption


do {

    // In combined mode, the authentication tag is appended to the encrypted message. This is usually what you want.

    let gcm = GCM(iv: iv, mode: .combined)

    let aes = try AES(key: key, blockMode: gcm, padding: .noPadding)

    return try aes.decrypt(encrypted)

} catch {

    // failed

}


Note: GCM instance is not intended to be reused. So you can't use the same GCM instance from encoding to also perform decoding.



references:

https://github.com/krzyzanowskim/CryptoSwift#aes-gcm


No comments:

Post a Comment