AES encryption with GCM Mode using crypto swift

Hi All,
I have issue with doing AES encryption with GCM Mode earlier using CBC which have to change to now GCM as change in backend system,
getting error "javax.crypto.AEADBadTagException: Tag mismatch!"

Code have used with AES/CBC encryption is :

Code Block
static func encryptsData(parameters: parameters?) -> (String, String) {
        // Convert Parameters in Json string
        var jsonStr = DefaultValues.empty
        do {
            if let str = String(data: try JSONSerialization.data(withJSONObject: parameters ?? [:], options: .prettyPrinted), encoding: .utf8) {
                jsonStr = str
            }
        } catch {
            debugPrint(error.localizedDescription)
        }
        // Get Secrete key, iv of 128 bits
        let iv = [UInt8](repeating: 0, count: 16)
        let randomStr = randomString(length: 16)
        let key: Array<UInt8> = Array(randomStr.utf8)
        // Encrypt Request Data with Secrete Key (AES)
        let aes =  try! AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs5)
        let encrypted = try? aes.encrypt(Array(jsonStr.utf8))
        if let encryptedMsg = encrypted {
            let encryptedData =  Data(bytes: encryptedMsg, count: Int(encryptedMsg.count))
            let encryptedBase64 = encryptedData.base64EncodedString()
            //Encrypt Secrete Key using Server Public key (RSA)
            let keyData = Data(bytes: key, count: key.count)
            let encryptedSceKey = encryptSecKeyWithPublickKey(key: String(data: keyData, encoding: .utf8) ?? "")
            return (encryptedBase64, encryptedSceKey)
        }
        return (DefaultValues.empty, DefaultValues.empty)
    }

Please Help!!
Hope have made my point Clear!!
Thanks In Advance !!

AES encryption with GCM Mode using crypto swift

Are you referring specifically to the CryptoSwift library here? If so, you’ll probably have more luck escalating this via the support channel for that third-party library. Apple Developer Forums is, as the name suggests, primarily focused on Apple APIs.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
oh! thanks! eskimo...Actually have read somewhere that GCM is not supported in Common crypto swift library.. I also tried
If you can please suggest how to proceed with AES/GCM/No padding encryption in swift that would be great help.Thanks

Actually have read somewhere that GCM is not supported in Common
Crypto swift library

There’s a lot to unpack here:
  • CommonCrypto is not a Swift library, but rather an API on our various platforms.

  • CommonCrypto does have a AES-GCM implementation but, annoyingly, it’s not a public API.

  • Fortunately, there is a different API for AES-GCM on our systems, namely Apple CryptoKit.

CryptoKit is very opinionated, that is, it only offers crypto algorithms that we actively recommend. If CryptoKit works for you, that’s great. If it doesn’t have the specific features you need then there are no lower-level AES-GCM APIs so you’ll need to look to a third-party crypto library.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
AES encryption with GCM Mode using crypto swift
 
 
Q