0

I encoded the body in base64, but I can only pass it as NSData, and PHP will only decrpyt the string. How do I decrypt NSData in php?

Swift code

request.HTTPMethod = "POST"
    let encode = bodyData.dataUsingEncoding(NSUTF8StringEncoding)!;
    let crypt = encode.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    request.HTTPBody = crypt 

PHP code

$postVarUser = base64_decode($_POST["usernamedata"]);
$postVarPass = base64_decode($_POST["passworddata"]);
echo $_POST["usernamedata"];//prints nil
echo $_POST["passworddata"];//prints nil

2 Answers 2

2

As you are receiving bytes, you should receive them using php://input, like so:

// Get data from POST
$dataEnc = file_get_contents('php://input');
// Decode data
$dataDec = base64_decode($dataEnc);
// Get username
$username = $dataDec["usernamedata"];
echo($username);

This should work if the object has been constructed correctly prior to encoding.

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

1 Comment

I made the changes on the php script, but is this correct on the ios end? puu.sh/p5d6S/5b9314dc30.png
0

NSData is just a bunch of bytes. If you set the HTTPBody to that, you can't use $_POST on the PHP side to treat it as key/values before you decode it.

I don't know PHP, but from googling, it looks like you want $HTTP_RAW_POST_DATA, Try: base64_decode($HTTP_RAW_POST_DATA);

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.