Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. iOS
  4. AES-GCM 256 Encrypt & Decrypt in iOS Swift Sync with .Net output

AES-GCM 256 Encrypt & Decrypt in iOS Swift Sync with .Net output

Scheduled Pinned Locked Moved iOS
swiftcsharpiosdata-structuressecurity
1 Posts 1 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bb M
    wrote on last edited by
    #1

    I want to use AES-GCM encryption of a plaintext with 'key' only. The 'key' will be received from backend (.net), in webservice response. The same key will be used later in backend as well to decrypt the encrypted value.

    I have used extension as follwoing

    To generate string to Symetric key

    extension SymmetricKey {
    init(string keyString: String, size: SymmetricKeySize = .bits256) throws {
    guard var keyData = keyString.data(using: .utf8) else {
    print("Could not create base64 encoded Data from String.")
    throw CryptoKitError.incorrectParameterSize
    }

    let keySizeBytes = size.bitCount / 8
    keyData = keyData.subdata(in: 0..= keySizeBytes else { throw CryptoKitError.incorrectKeySize }
    self.init(data: keyData)
    } }

    Encryption & Decryption code snipet:

    func aesgcmEncryptDecrypt() {
    let str : String = "FwhXRYJ$xLf?^Kh6_&YfTJ%RuG+EqcTY"
    var key : SymmetricKey = SymmetricKey(size: .bits256)
    do{
    key = try SymmetricKey(string: str)
    }catch{
    }
    let plain = "HOW ARE YOU?"
    let nonce = try! AES.GCM.Nonce(data: Data(base64Encoded: "fv1nixTVoYpSvpdA")!)
    let tag = Data(base64Encoded: "e1eIgoB4+lA/j3KDHhY4BQ==")!
    // Encrypt
    let sealedBox = try! AES.GCM.seal(plain.data(using: .utf8)!, using: key,
    nonce: nonce, authenticating: tag)
    // Decrypt
    let sealedBoxRestored = try! AES.GCM.SealedBox(combined: sealedBox.combined!)
    let decrypted = try! AES.GCM.open(sealedBoxRestored, using: key,
    authenticating: tag)
    Swift.print("Combined:\n\(sealedBox.combined!.base64EncodedString())\n")
    Swift.print("Cipher:\n\(sealedBox.ciphertext.base64EncodedString())\n")
    Swift.print("Nonce:\n\(nonce.withUnsafeBytes {
    Data(Array($0)).base64EncodedString() })\n")
    Swift.print("Tag:\n\(tag.base64EncodedString())\n")
    Swift.print("Decrypted:\n\(String(data: decrypted, encoding: .utf8)!)\n")
    }
    Our(generated from this code & .Net code) encrypted text is not matching. Anyone can help me, what I am doing wrong, detailing will be more helpful. Basically I was looking for saloution in Objective-C but I did not get any supported library for Objective-C.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups