简体   繁体   English

Obj-C中的AES加密和PHP中的解密

[英]AES Encrypting in Obj-C and decrypt in PHP

I am fairly new to this. 我对此很陌生。 My PHP echo is returning garbage. 我的PHP回声返回垃圾。 I think there's an encoding step I'm missing. 我认为我缺少一个编码步骤。

My OBJ-C code 我的OBJ-C代码

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 的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. 您要创建一个新的IV,您需要使用原始的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. 然后,您需要PHP在定界符处分割加密的字符串,将IV用于解密功能,并将另一部分用作加密数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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