【问题标题】:AES128 encryption in Xcode and decrypt in PHP succeeds and fails depending on key valueXcode 中的 AES128 加密和 PHP 中的解密成功和失败取决于密钥值
【发布时间】:2015-05-20 14:30:17
【问题描述】:
  1. 我按照https://tharindufit.wordpress.com/2011/12/15/aes128-encryption-in-ios-and-decryption-in-php/ 的步骤开发了 Xcode 中的客户端加密和 PHP 中的服务器解密。

  2. 我在 https://asecuritysite.com/encryption/keygen 生成了一个 aes-128-ecb 密钥。

  3. 我的问题是:

如果我将密钥设为“key”之类的短字符串,则 PHP 解密有效;但是如果我使用上述网站生成的密钥,例如“D3C3794A79ADBDFD256645D59F01E2BD”,则PHP解密不起作用。

为什么会这样??

我的确切代码如下。

Xcode 中的客户端:

#import "ViewController.h"
#import "NSString+AESCrypt.h"

static NSString *const KEY = @"D3C3794A79ADBDFD256645D59F01E2BD";

@interface ViewController (){
    NSString *rawPassword;
    NSMutableData *receivedData;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
} 
- (IBAction)send:(UIButton *)sender {
    rawPassword = @"passwordHere";
    [self sendRequest];
}

- (void) sendRequest {
    NSString *rawUserid = @"useridHere";

    // Encrypt with key
    NSString *encoded_usr = [rawUserid AES128EncryptWithKey: KEY];
    NSString *encoded_pwd = [rawPassword AES128EncryptWithKey: KEY];

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

    NSLog(@"sending:%@", parameter);
    NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString: @"http://mywebsite.com/server.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPBody:parameterData];

    [request setHTTPMethod:@"POST"];
    [request addValue: @"application/x-www-form-urlencoded; charset=utf-8"  forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];


    if(connection){
        receivedData = [[NSMutableData alloc]init];
        NSLog(@"CONNECTING");
    } else {
        NSLog(@"NO CONNECTING");
    }
}

#pragma mark NSURLConnection delegates
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"CONNECTION FAILED");
    return;
 }
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
 {
    NSString* newStr = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"connectionDidFinishLoading:%@", newStr);
 }
 @end

服务器端 PHP:

<?php

$encoded_pwd = $_POST['password'];
$encoded_usr = $_POST['userid'];
$device      = $_POST['device'];

$decoded_pwd = decrypt_password( $encoded_pwd , "D3C3794A79ADBDFD256645D59F01E2BD");
$decoded_usr = decrypt_password( $encoded_usr , "D3C3794A79ADBDFD256645D59F01E2BD");

echo "decoded userid:".$decoded_usr."  decoded password:".$decoded_pwd;

function decrypt_password($pass,$key)
{
 $base64encoded_ciphertext = $pass;

 $res_non = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($base64encoded_ciphertext), MCRYPT_MODE_ECB);

 $decrypted = $res_non;
 $dec_s2 = strlen($decrypted);

 $padding = ord($decrypted[$dec_s2-1]);
 $decrypted = substr($decrypted, 0, -$padding);

 return  $decrypted;
}
?>

【问题讨论】:

  • 如果您使用的是 AES-128,那么您的密钥应该是 128 位(即 16 个字节/字符)。 code to which you linked 说了很多:// ‘key’ should be 16 bytes for AES128

标签: php ios xcode encryption aes


【解决方案1】:

由于您使用的是 AES-128,因此您的密钥应该是 128 位(即 16 个字节/字符)。您链接到的 Objective-C 代码说明了很多 (// ‘key’ should be 16 bytes for AES128),而且它会填充一个较短的键以使其具有适当的长度 (// fill with zeroes (for padding))。

因此,大于 128 位的密钥会导致问题是可以预料的;并且小于 128 位的密钥也可以正常工作(因为通过用零填充它会增加到 128 位)。

您使用的密钥生成站点为 AES-128 ECB 生成 16 个字符的密钥...但以十六进制显示它们(即每个密钥字符 2 个十六进制数字 => 32 个十六进制数字) ,这就是它们显示为 32 个“字符”键的原因。

【讨论】:

  • 对不起,我不明白.. 那我应该如何创建密钥?只使用任何 16 字节的字符串?好吧,我试过了,太短或太长的加密字符串不会在 php 中解密..
  • 您可以从该站点创建 128 位密钥,但您必须将其生成的内容视为字节数组并将其转换为字符串。如果您不清楚如何在 Objective-C 中执行这种转换的例子很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 2013-02-06
  • 2015-08-01
  • 2011-03-12
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多