如何将 post 密码从 iOS 应用程序安全地发送到 PHP 服务器?

How to post password from iOS app to PHP server securely?

我对互联网安全几乎一无所知,遇到这个问题。

情况是:

用户必须从 iOS 应用程序登录到基于 PHP 的服务器。

我 POST 从 xcode 发送到 php 是这样的:

NSString *parameter = [NSString stringWithFormat:@"userid=%@&password=%@",usernameVar, passwordVar];

NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

url = [NSURL URLWithString: @"http://mywebsite.com/server.php"];

request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPBody:parameterData];

但这安全吗?我觉得以纯文本形式发送密码非常危险。

如果是这样,谁能告诉我如何安全地 post 密码?

在 HTTP 中传递机密数据Header 它将是安全的。

您还可以将密码转换为 MD5 格式,这样就无法在简单的字母数字字符串中读取。

您可以简单地加密您的密码并在服务器端解密。

您还可以看到这个 link 这个库可以满足您的需求 https://github.com/RNCryptor/RNCryptor

这里是AES256加解密的示例代码。

从此处下载 AES256 加密 类:https://gist.github.com/838614

Objective C代码:

    NSString *key = @"a16byteslongkey!";
    NSString *plaintext = @"iphone";

    NSString *ciphertext = [plaintext AES256EncryptWithKey: key];
    NSLog(@"ciphertext: %@", ciphertext);

在 api 的 link 中发送您的加密密码。并在服务器端解密密码。或者直接存储加密后的密码你也可以匹配加密后的密码

PHP代码:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = "a16byteslongkey!";
    $plaintext = "iphone";
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB, $iv);
    $ciphertext = base64_encode($ciphertext);
    echo "ciphertext: ".$ciphertext."<br/>";