简体   繁体   中英

AES128 encryption in Xcode and decrypt in PHP succeeds and fails depending on key value

  1. I followed steps at https://tharindufit.wordpress.com/2011/12/15/aes128-encryption-in-ios-and-decryption-in-php/ to develop client encryption in Xcode and server decryption in PHP.

  2. I generated a aes-128-ecb key at https://asecuritysite.com/encryption/keygen .

  3. My question is:

If I make the key a short string like "key", the PHP decryption works; but if I use a key generated by the above website, "D3C3794A79ADBDFD256645D59F01E2BD" for example, the PHP decryption does not work.

Why is that??

My exact code is below.

Client-side in 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

Server-side 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;
}
?>

Since you are using AES-128, your key should be 128 bits (ie 16 bytes/characters). The Objective-C code to which you linked says as much ( // 'key' should be 16 bytes for AES128 ), also that it will pad a shorter key to make it the appropriate length ( // fill with zeroes (for padding) ).

So that a key greater than 128 bits causes a problem is to be expected; and that a key less than 128 bits works is to be expected too (in that it is increased to 128 bits by padding it with zeroes).

The key-generation site you are using generates 16-character keys for AES-128 ECB...but shows them in hexadecimal (ie 2 hex digits per key character => 32 hex digits), which is why they appear as 32-"character" keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM