简体   繁体   中英

AES Encrypting in Obj-C and decrypt in PHP

I am fairly new to this. My PHP echo is returning garbage. I think there's an encoding step I'm missing.

My OBJ-C code

NSString *key = @"12345678901234561234567890123456";

    //main path for account login PHP file
    NSString *accountURL = [NSString stringWithFormat:configManager.accountURL, username, password];

    //convert password to data and encrypt
    NSData *crypt = [[[password stringValue]dataUsingEncoding:NSUTF8StringEncoding]AES256EncryptWithKey:key];

    NSString *variables = [NSString stringWithFormat:@"?username=%@&access=%@&page=%ld",[username stringValue], crypt, [sender tag]];

    // get rid of spaces in encoded URL
    NSString *niceURLString = [variables stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //tack niceURL on to end of base URL
    NSString *goToURL = [accountURL stringByAppendingString:niceURLString];
    NSLog(@"%@", goToURL);

    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:goToURL]];

PHP

$upswd = $_GET['access'];
    $key = "12345678901234561234567890123456";
$upswd = trim($upswd, "<>"); //remove these brackets from url

$result = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $upswd, 'ecb');
//$result = utf8_decode($result);
echo ($result);
        mcrypt_create_iv(
            mcrypt_get_iv_size(
                MCRYPT_RIJNDAEL_256,
                MCRYPT_MODE_ECB
            )

You are creating an new IV, you need to use the original IV. It is usually passed along with the encrypted text seperated by a delimiter commonly used delimiters are . or | you then have PHP split the encrypted string at the delimiter, use the IV for the decrypt function and use the other part as the encrypted data.

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