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
B

Bb M

@Bb M
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • AES-GCM 256 Encrypt & Decrypt in iOS Swift Sync with .Net output
    B Bb M

    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.

    iOS swift csharp ios data-structures security
  • Login

  • Don't have an account? Register

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