我在objc找不到我的錯誤,XOR加密
-(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 > d = 1" + dssstcn | E〜DN1; 9 NVE,E9; UPG( '%& 5nqfyelfdnfll $ + f'6icr & OH#'(S4> vas6; aclkl |} brg86tp4#XZ! !HB 2" B; 6njk>'〜hpbnktvaw 「!」 的pn> mp0gj9lkj`〜,3 | I6 =
但是我發送消息
qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm
請,幫助我找到我的錯誤。
發現我的問題,它在這裏$ k =(++ $ k <$ klen?$ k:0);我把它改寫,以簡單,如果不三元 – SergeyM 2012-03-02 13:12:09