-1

I am using http://aes.online-domain-tools.com to encrypt my NSString and what i get back from this is an array of unsigned char like this c2 84 6b 71 72 6d d2 e7 cd 0b a6 08 cd 85 c3 0c.

Then is use this to convert it into NSString in my code:

const unsigned char encrpytedAppIDbytes[] = {0xe5, 0x35, 0xdf, 0x72, 0x57, 0xaf, 0xf7, 0xe6, 0x1f, 0x6d, 0x51, 0x1d, 0x26, 0xe8, 0x5e, 0xa2};
NSData *appIDToDecrypt = [NSData dataWithBytes:encrpytedAppIDbytes length:sizeof(encrpytedAppIDbytes)];
NSString *decryptedAppID = [[NSString alloc] initWithData:[appIDToDecrypt AES128DecryptedDataWithKey:@"something"] encoding:NSUTF8StringEncoding];

if([decryptedAppID isEqualToString:@"Something"]){} // This fails even when i look at them in the debugger they are the same.

But when i am trying to decrypt it, its showing up as the same string but when i compare it with the same NSString hardcode to check if it is the same string it doesn't work. This fails some authentication check i have in my app.

Please point anything wrong i am doing here.

Thanks,

Ashutosh
  • 5,614
  • 13
  • 52
  • 84
  • Show the comparison code. – trojanfoe Nov 03 '15 at 18:18
  • Please see my edit above – Ashutosh Nov 03 '15 at 18:27
  • There seems to be lots of repeated code in there. You log using a string created on-the-fly, and then set `authUssernameTextfield.text` to a string created from the data. Then you create a base-64 string from *that* string. It's a mess and the answer lies in the wanton over-reuse of redundant code. Cut it down to its simplest terms and the answer will reveal itself. – trojanfoe Nov 03 '15 at 18:31
  • it looks like when i am trying o compare it just after decrypting it..its failing so seems like the problem is when i try to decrypt it. Do you see any problem in that ? – Ashutosh Nov 03 '15 at 19:42
  • 1
    As mentioned, the code is a mess. Start by sorting that out. – trojanfoe Nov 03 '15 at 22:18
  • i did. And i am not going step by step. The string looks the same in debugger but when compared with the same string gives false. The first step itself i the problem, i will do more cleanup once i can get through the first step – Ashutosh Nov 03 '15 at 22:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94144/discussion-between-ashutosh-and-trojanfoe). – Ashutosh Nov 03 '15 at 23:01
  • @Ashutosh Hint: to format code with standard indentation in Xcode, select the code to indent and control-i. It is also better to use intermediate statements and variables instead of a long compound statement, the celebrate statements are easier to debug (and understand). Also it is best to indent with spaces and not tabs, there is an option to automatically do that. – zaph Nov 03 '15 at 23:22
  • It's less the indentation than the repeated re-encoding of the same data. The string is encoded as base-64 and then compared to what appears to be a hash. It would be better to know what it's being compared against and there is no need to convert to a string, let alone a base-64 string, just for the purposes of comparison, given binary data can be compared just fine. – trojanfoe Nov 03 '15 at 23:30
  • Yeah, I agree. Using intermediate statements and better formatting make that easier to see. The way the code it formatted now give me zero interested in even looking at it. – zaph Nov 04 '15 at 01:26
  • As everyone is so stuck with the indentation and the messy code. I have removed most of it to remove the confusion. But the above isn't working either. – Ashutosh Nov 04 '15 at 01:53

1 Answers1

0

Alright so after spending few hours with it i finally found the solutions which might not be optimal but works in my case.

It seems like after decryption, the string contains some other characters which are not visible in the debugger but when i am trying to check the length it shows greater than the number of characters in it which indicates that there is something wrong. For now what i have done is this :

const unsigned char nameBytes[] = {0xa6, 0xf0, 0xea, 0x36, 0x5f, 0x78, 0xb7, 0x52, 0x29, 0x6a, 0x67, 0xb7, 0xeb, 0x73, 0xd5, 0x14};

    NSData *nameBytesData = [NSData dataWithBytes:nameBytes length:sizeof(nameBytes)];
    NSString *nameBytesString = [[NSString alloc] initWithData:[nameBytesData AES128DecryptedDataWithKey:@"PaymentGateway"] encoding:NSUTF8StringEncoding];
     NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];

    NSString *safeSearchString = [[nameBytesString componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];

    NSLog(@"length:%lu",(unsigned long)[safeSearchString length]);
    NSLog(@"lengthActual:%lu",(unsigned long)[@"ashutosh" length]);
    if ([safeSearchString isEqualToString:@"ashutosh"]) {
        NSLog(@"Success");
    }
    NSLog(@"Decrypted:%@",nameBytesString);

The code above removes all the special characters and replaces it with @"" so the resulted string only has valid chars. For adding support to consider more chars as valid just add them to NSCharacterSet * set.

Ashutosh
  • 5,614
  • 13
  • 52
  • 84