6

Data fetched from server using

var request = var request = URLRequest(url: URL(string: "http://www.example.com/test.php")!)   
        request.httpMethod = "POST"   
        let akey:String =  txt_key.stringValue;   
        let email:String = txt_email.stringValue   
        let VAL:String="test"      
        var data="blah"   
        let postString = data   
        request.httpBody = postString.data(using: .utf8)   
        let task = URLSession.shared.dataTask(with: request) { data, response, error in   
            guard let data = data, error == nil else {                                                 /   
                print("error=\(error)")   
                return   
            }   
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           /   
                print("statusCode should be 200, but is \(httpStatus.statusCode)")   
                print("response = \(response)")   
            }   
            let responseString = String(data: data, encoding:  .utf8)   
            print(responseString)   
        }   
        task.resume()   

Decryption using

   func aesDecrypt(key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? {
        if let keyData = key.data(using: String.Encoding.utf8),
            let data = NSData(base64Encoded: self, options: .ignoreUnknownCharacters),
            let cryptData    = NSMutableData(length: Int((data.length)) + kCCBlockSizeAES128) {

            let keyLength              = size_t(kCCKeySizeAES128)
            let operation: CCOperation = UInt32(kCCDecrypt)
            let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
            let options:   CCOptions   = UInt32(options)

            var numBytesEncrypted :size_t = 0

            let cryptStatus = CCCrypt(operation,
                                      algoritm,
                                      options,
                                      (keyData as NSData).bytes, keyLength,
                                      iv,
                                      data.bytes, data.length,
                                      cryptData.mutableBytes, cryptData.length,
                                      &numBytesEncrypted)

            if UInt32(cryptStatus) == UInt32(kCCSuccess) {
                cryptData.length = Int(numBytesEncrypted)
                let unencryptedMessage = String(data: cryptData as Data, encoding:String.Encoding.utf8)
                return unencryptedMessage
            }
            else {
                return nil
            }
        }
        return nil
}

Even when responseString is not null,The following code always returns optional("")

 let unencode = String(describing: responseString!).aesDecrypt(key: "123456789012asdsadasd", iv: "iv-salt-string--")

Why is this happening ? Please advice.

39
  • 1
    What does your first print(responseString) shows? Is your post successful? Commented Oct 11, 2017 at 18:38
  • 1
    A concrete example (with responseString, key and iv) is needed ... Commented Oct 11, 2017 at 18:40
  • 1
    @techno: No. You have to provide a minimal reproducible example, in the question itself. Commented Oct 11, 2017 at 18:45
  • 1
    What you send to the server is var data="blah" ... the key and email are not used at all. Is that your real code? Commented Oct 11, 2017 at 18:53
  • 1
    Summary so far: Decrypting the (unknown) response of an unknown request with some key (which you don't provide) fails, and what you show is not the real code ... I am not sure what answer you expected. Commented Oct 11, 2017 at 19:03

1 Answer 1

1

Per MartinR's comment:

I have verified that "JDoNBXk21oJ9x15Bkv12uw==" is exactly the result of encrypting an empty string with your key and iv. The error is in your PHP script, not in the Swift code.

Therefore you need to resolve the error on the server.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.