douciping4283 2015-12-06 21:24
浏览 31

如何在使用这种不安全的Swift NSURL代码的同时安全地与PHP脚本交互


I have written a swift function that retrieves the contents of a URL. I am passing variable with POST that use PHP to interact with a MySQL database. Unfortunately, after writing my application I have realized that this is very unsecure, as the POST variable can be easily intercepted. Is there a way to keep the existing code, but secure it enough that it cannot be exploited easily? I have heard of SSL certificates, however I have no idea how to go about applying them to this kind of interaction. I have also thought about encrypting the URL, however I cannot write a good encryption myself, therefore I would need to use a swift library to do it for me. Obviously the same library would not work in PHP do decrypt to interact with the database. If you have any way to secure this guy, please let me know! Thanks
Code:

let username = "bob" 
let secretAccessKay = "123abc"
let myUrl = NSURL(string: "http://127.0.0.1/phpscript.php")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let postString = "username=\(username)&secretAccessKey=\(secretAccessKey)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    data, response, error in
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        if error != nil {
             print("Error: \(error)")
        } 
        dispatch_async(dispatch_get_main_queue()) {
            self.testLabel.text = "\(responseString!)"
        }
    }
}
task.resume() 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题