· 7 years ago · Aug 19, 2018, 05:22 AM
1Xor between 2 NSString gives wrong result
2(void)XorSecretKeyDeviceId
3{
4NSString* secretKey = @"123";//
5NSString* deviceId = @"abcdef";//
6
7NSData* stringKey = [secretKey dataUsingEncoding:NSUTF8StringEncoding];
8NSData* stringDeviceId = [deviceId dataUsingEncoding:NSUTF8StringEncoding];
9
10unsigned char* pBytesInput = (unsigned char*)[stringKey bytes]; //Bytes
11unsigned char* pBytesKey = (unsigned char*)[stringDeviceId bytes];
12
13unsigned int vlen = [secretKey length]; //Keys Length
14unsigned int klen = [deviceId length];
15
16unsigned int v;
17unsigned int k = vlen % klen;
18unsigned char c;
19
20for(v = 0; v < vlen; v++)
21{
22 c = pBytesInput[v] ^ pBytesKey[k];
23 pBytesInput[v] = c;
24 NSLog(@"%c", c);
25
26 k = (++k < klen ? k : 0);
27}
28}