Objective-c implements authCode to solve the communication encryption problem between php and ios.
Recently, the project needs to encrypt the communication content with the server, mainly to prevent malicious packet capture and exploitation. such encryption can be found directly on the Internet, however, almost all OC files come from the same template. the encrypted characters cannot be parsed by the PHP backend, and there is no valid time parameter, therefore, you can only write an OC version against the PHP encryption code. many PHP methods are far from as simple as one sentence in the OC (:>_< ::), many problems have also been found. due to the urgent issue, we have not gone further. if we have time, we will continue to optimize the encryption ~
# Import
# Define STRING_SPLICE (a, B) ([NSString stringWithFormat: @ "% @", (NSString *) (a), (NSString *) (B)]) // string concatenation
+ (NSString *)md5:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5(cStr, strlen(cStr), result); // This is the md5 call return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ];}
// Param: the string to be encrypted // operation: input @ "ENCODE" is encrypted, and decryption is not written, so as long as "DECODE" is not passed, OK // expiry: effective time, in seconds. the default value is 0, indicating that there is no effective time. if the effective time is exceeded after the time is set, it cannot be decrypted + (NSString *) encryption :( NSString *) param operation :( NSString *) operation expiry :( int) expiry {NSString * key = @ "1992326qa"; // The encrypted key NSString * DECODE = @ "DECODE "; param = [param stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; operation = operation? Operation: DECODE; expiry = expiry? Expiry: 0; int keyLength = 4; key = [self md5: key]; NSString * keya = [self md5: [key substringToIndex: 16]; NSString * keyb = [self md5: [key substringFromIndex: 16]; NSString * time = [self microtime]; NSString * keyc = keyLength? ([Operation isEqualToString: DECODE]? [Param substringToIndex: keyLength]: [[self md5: time] substringFromIndex: [self md5: time]. length-keyLength]): @ ""; NSString * cryptkey = STRING_SPLICE (keya, [self md5: STRING_SPLICE (keya, keyc)]); keyLength = cryptkey. length; param = [NSString stringWithFormat: @ "% @%@%@", ([NSString stringWithFormat: @ "% 010d", expiry? Expiry + (int) [[NSDate dateWithTimeIntervalSinceNow: 0] timeIntervalSince1970]: expiry]), [[self md5: STRING_SPLICE (param, keyb)] substringToIndex: 16], param]; int paramLength = param. length; NSString * result = @ ""; NSMutableArray * box = [NSMutableArray arrayWithCapacity: UnicodeCount]; for (int I = 0; I <= UnicodeCount; I ++) {[box addObject: @ (I)];} NSMutableArray * rndkey = [NSMutableArray array]; for (int I = 0; I <= UnicodeCount; I ++) {const char rndkeyItem = [cryptkey characterAtIndex: I % keyLength]; NSString * asciiStr = [NSString stringWithCString: & rndkeyItem encoding: signature]; int asciiCode = [asciiStr limit: 0] [rndkey addObject: @ (asciiCode)] ;}for (int I = 0, j = 0; I <= UnicodeCount; I ++) {j = (j + [box [I] intValue] + [rndkey [I] intValue]) % (UnicodeCount + 1); int tmp = [box [I] intValue]; box [I] = box [j]; box [j] = @ (tmp) ;}for (int a = 0, j = 0, I = 0; I <paramLength; I ++) {a = (a + 1) % (UnicodeCount + 1); j = (j + [box [a] intValue]) % (UnicodeCount + 1 ); int tmp = [box [a] intValue]; box [a] = box [j]; box [j] = @ (tmp); int s1 = [self ord: param index: I]; int s2 = [box [([box [a] intValue] + [box [j] intValue]) % (UnicodeCount + 1)] intValue]; int s3 = s1 ^ s2; NSString * add = [self strChr: s3]; result = STRING_SPLICE (result, add);} return [NSString stringWithFormat: @ "% @", keyc, [[self base64: result] stringByReplacingOccurrencesOfString: @ "=" withString: @ ""];}
+ (NSString *) microtime // calculate the time string {NSDate * currentDate = [NSDate failed: 0]; NSTimeInterval interval = [currentDate timeIntervalSince1970]; NSString * intervalStr = [NSString stringWithFormat: @ "% f00", interval]; NSString * pre = [intervalStr substringWithRange: NSMakeRange (intervalStr. length-8, 8)]; NSString * suf = [intervalStr substringToIndex: intervalStr. length-9]; NSString * result = [NSString stringWithFormat: @ "0.% @ ", pre, suf]; return result ;}
+ (Int) ord :( NSString *) str index :( int) index // Obtain the ASCII code of a certain digit of the string {int asciiCode = [str characterAtIndex: index]; return asciiCode ;}
+ (Const char) chr :( int) asciiCode // get the character {return [[NSString stringWithFormat: @ "% C", (unichar) asciiCode] characterAtIndex: 0];}
+ (NSString *) strChr :( int) asciiCode // Obtain the string through the ASCII code {NSString * data = [NSString stringWithFormat: @ "% C", (unichar) asciiCode]; // A return data ;}
+ (NSString *) base64 :( NSString *) str // base64 encoded {NSString * base64EncodedString = [[str dataUsingEncoding: NSUTF8StringEncoding] Encoding: 0]; return base64EncodedString ;}
Note:
- Only encryption algorithms (because I do not need to decrypt)
- If you need to encrypt Chinese characters, there will be space characters during the encryption process, and the backend will fail to parse. Therefore, when parsing iOS in the background, replace the space in the encryption string with the + sign, in order to ensure that each resolution is successful
Problem:
- Because the ASCII code needs to be converted during the encryption process, but the ASCII code of Mac and Window is different after 127 bits, we can only use the first 127 bits.
I keep it first, so when can I use it again ~