找不到我的错误,xor cipher

时间:2012-03-02 12:54:15

标签: php objective-c xor

我的代码是objc

-(IBAction)click:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/second.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *data=[self transform:@"qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm" Key:@"0800fc577294c34e0b28ad2839435945"];
    NSData *requestBody = [[NSString stringWithFormat:@"p=%@",data] dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:requestBody];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
    UIAlertView *alert=[[[UIAlertView  alloc] initWithTitle:@"Message" message:responseString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];
    [alert show];
    NSLog(@"log=%@",responseString);


}

- (NSString*) transform:(NSString*)input Key:(NSString*)key
{
    NSMutableString* output = [[NSMutableString alloc] init];
    unsigned int vlen = [input length];
    unsigned int klen = [key length];
    unsigned int v = 0;
    unsigned int k = 0;


    for (v=0; v < vlen; v++) {
        unichar c = [input characterAtIndex:v] ^ [key characterAtIndex:k];
        [output appendString:[NSString stringWithFormat:@"%C", c]];

        k = (++k < klen ? k : 0);
    }

    NSString* final = [[[NSString alloc] initWithString:output] autorelease];
    [output release];
    return final;
}

并在php解密

$res="";
$key="0800fc577294c34e0b28ad2839435945";
$k=0;
if ($_POST['p']){


        $vlen = strlen($_POST['p']);
        $klen = strlen($key);
        $v = 0;
        $k = 0;

        for ($v=0; $v < $vlen; $v++) {
            $c = $_POST['p'][$v] ^ $key[$k];
            $res=$res.$c;
            $k = (++$k < $klen ? $k : 0);
        }

    echo $res;
    }

但是当我检查结果是

  

qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcv&GT; d = 1" + dssstcn | E〜DN1; 9 NVE,E9; UPG( '%&安培; 5nqfyelfdnfll $ + f'6icr&安培; OH#'(S4&GT; vas6; aclkl |} brg86tp4#XZ! !HB 2" b; 6njk&GT;'〜hpbnktvaw “” PN&GT;!mp0gj9lkj`〜,3 | I6 =

然而我发送消息

  

qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm

请帮助我找到错误。

2 个答案:

答案 0 :(得分:1)

在第50位,您从字符串中删除字符“b”,字符“b”来自您的键,结果为null character。这可能被解释为控制角色并被丢弃。

您使用“密码”一词;但是,您采用的方法绝不是安全的。也许你可以提供更多关于你在这里想要达到的细节。您有没有选择不使用SSL进行加密的原因?

答案 1 :(得分:1)

您不能使用两个unichars并期望结果是有效的unichar。正如jnic所说,这是你的算法的一般问题,首先在50字符处出错。您需要先使用-dataWithEncoding:将NSString和密钥转换为NSData然后进行加密。然后,您需要将二进制数据发送到PHP服务器。 PHP服务器需要反转Objective-C程序中的所有步骤。