Xcode中的AES128加密和PHP中的解密成功与否取决于密钥值


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

  1. 我在https://tharindufit.wordpress.com/2011/12/15/aes128-encryption-in-ios-and-decryption-in-php/开发Xcode中的客户端加密和PHP中的服务器解密。

  2. 我在https://asecuritysite.com/encryption/keygen.

  3. 我的问题是:

如果我把密钥做成一个类似"密钥"的短字符串,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字节/字符(。您链接到的Objective-C代码说明了同样多的内容(// ‘key’ should be 16 bytes for AES128(,还表示它将填充一个较短的键以使其具有适当的长度(// fill with zeroes (for padding)(。

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

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