Posts

Post not yet marked as solved
7 Replies
2.7k Views
I am using cryptoSwift library for encryption and decryption. But it's working with only string 16 bytes. If i am passing small string or less than 16 bytes then getting nil result. I am using Incremental operations use instance of Cryptor and encrypt/decrypt one part at a time. Please help me here, is there anything which i am doing wrong. Thanks in advance. &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;// Encryption start &#9;&#9;&#9;&#9;&#9;&#9;let data =&#9;Data.init(base64Encoded: "12345678901234567890123456789012".base64Encoded()!) &#9;&#9;&#9;&#9;&#9;&#9;let iv : Array<UInt8> = [0,0,0,0,0,0,0,0,0,0,0,0] &#9;&#9;&#9;&#9;&#9;&#9;let nIv = Data(iv) &#9;&#9;&#9;&#9;&#9;&#9;let gcmEnc = GCM(iv: nIv.bytes, mode: .detached) &#9;&#9;&#9;&#9;&#9;&#9;var enc = try? AES(key: data!.bytes, blockMode: gcmEnc, padding: .noPadding).makeEncryptor() &#9;&#9;&#9;&#9;&#9;&#9;let arrStr = ["My name is tarun"] // Working &#9;&#9;&#9;&#9;&#9;&#9;//let arrStr = ["tarun"] // Not working for this string &#9;&#9;&#9;&#9;&#9;&#9;var ciphertext = Array<UInt8>() &#9;&#9;&#9;&#9;&#9;&#9;for txt in arrStr{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; let ciperText = try? enc?.update(withBytes: Array(txt.utf8)) // Getting nil for small string. &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; ciphertext.append(contentsOf: ciperText!) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;var res = try? enc?.finish() &#9;&#9;&#9;&#9;&#9;&#9;gcmEnc.authenticationTag = self.randomGenerateBytes(count: 16)?.bytes &#9;&#9;&#9;&#9;&#9;&#9;res?.append(contentsOf: (gcmEnc.authenticationTag)!) &#9;&#9;&#9;&#9;&#9;&#9;let cipherData = Data(ciphertext) + Data(res!) &#9;&#9;&#9;&#9;&#9;&#9;let strEnc = String(decoding: cipherData, as: UTF8.self) &#9;&#9;&#9;&#9;&#9;&#9;print(strEnc) &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;// Decryption start from here &#9;&#9;&#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let gcmDec = GCM.init(iv: nIv.bytes, additionalAuthenticatedData: nil, tagLength: 16, mode: .detached) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;var aesDec = try! AES(key: data!.bytes, blockMode: gcmDec, padding: .noPadding).makeDecryptor() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let tag_length = 16 &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let encData = cipherData.subdata(in: 0..<cipherData.count - tag_length) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let tag = cipherData.subdata(in: encData.count ..< cipherData.count) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let decData = try? aesDec.update(withBytes: encData.bytes) //Getting nil here for small string &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let strData = String(decoding: decData!, as: UTF8.self) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(strData) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;do{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;var res = try? aesDec.finish(withBytes: tag.bytes) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;res?.append(contentsOf: tag) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}catch{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;// failed &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;} &#9;&#9;func randomGenerateBytes(count: Int) -> Data? { &#9;&#9;&#9;&#9;let bytes = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) &#9;&#9;&#9;&#9;defer { bytes.deallocate() } &#9;&#9;&#9;&#9;let status = CCRandomGenerateBytes(bytes, count) &#9;&#9;&#9;&#9;guard status == kCCSuccess else { return nil } &#9;&#9;&#9;&#9;return Data(bytes: bytes, count: count) &#9;&#9;}
Posted Last updated
.