objective c - NSCharacterSet URLHostAllowedCharacterSet doesn't replace '+' sign? -


i'm struggling transmit long encrypted strings on network , them come out correctly on server. example, have encrypted string on client:

wcwserzch8xm1hpbno1ksd1lvfmpuur4wmq9hquwek0vyclefpgwfr/sbtes1a4rpv6eyp9nzeeu9ukkifstdp+sposquf6evjf3wrhrxmre81lirhuryk0irwone5uik+vlpr41ketmznxa4+gelmf53r7oayrkkffnipdmpo+wbge0vl3pqeosxb01twjydibisz5wjiieim3zojw/sw==

as can see, has few characters not transmit on network without url encoding (+ , /, notably). i'm not entirely sure if there other characters arise in other situations, want make sure solution 'universally' correct. using line:

nsstring *escapedstring = [cipherstring stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset urlhostallowedcharacterset]]; 

which found in highly reviewed answer.

however, i'm still having trouble decrypting on server side, printed out results on client before sending, , see this:

wcwserzch8xm1hpbno1ksd1lvfmpuur4wmq9hquwek0vyclefpgwfr%2fsbtes1a4rpv6eyp9nzeeu9ukkifstdp+sposquf6evjf3wrhrxmre81lirhuryk0irwone5uik+vlpr41ketmznxa4+gelmf53r7oayrkkffnipdmpo+wbge0vl3pqeosxb01twjydibisz5wjiieim3zojw%2fsw==

why '+' signs still there? using wrong allowed character set? character set should use guarantee correctly escape problematic characters?

if helps, here code using encrypt plain text string. when done, base64 encode results before sending across network:

- (nsdata *)phpencryptcleartext : (nsdata *)cleartext {     nsdata *cleartextpadded = [self phppaddata:cleartext];      cccryptorstatus ccstatus        = kccsuccess;     size_t          cryptbytes      = 0;    // number of bytes moved buffer.     nsmutabledata  *ciphertextdata  = [nsmutabledata datawithlength:cleartextpadded.length];      ccstatus = cccrypt(kccencrypt,                        kccalgorithmaes128,                        0,                        _sessionkey.bytes,                        kcckeysizeaes128,                        _iv.bytes,                        cleartextpadded.bytes,                        cleartextpadded.length,                        ciphertextdata.mutablebytes,                        ciphertextdata.length,                        &cryptbytes);      if (ccstatus == kccsuccess) {         ciphertextdata.length = cryptbytes;     }     else {         nslog(@"kencryptionerror code: %d", ccstatus); // add error handling         ciphertextdata = nil;     }      return ciphertextdata; } 

thanks advice!

swift version here.

to escape character use stringbyaddingpercentencodingwithallowedcharacters:

nsstring *urlescapedstring = [string stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset urlqueryallowedcharacterset]]; 

the following useful character sets, characters not included in sets:

urlfragmentallowedcharacterset  "#%<>[\]^`{|} urlhostallowedcharacterset      "#%/<>?@\^`{|} urlpasswordallowedcharacterset  "#%/:<>?@[\]^`{|} urlpathallowedcharacterset      "#%;<>?[\]^`{|} urlqueryallowedcharacterset     "#%<>[\]^`{|} urluserallowedcharacterset      "#%/:<>?@[\]^` 

or create own characterset characters need escape.

nscharacterset *customcharacterset = [[nscharacterset charactersetwithcharactersinstring:@"your characters"] invertedset]; 

creating characterset combining of above:

nscharacterset *urlfullcharacterset = [[nscharacterset charactersetwithcharactersinstring:@" \"#%/:<>?@[\\]^`{|}"] invertedset]; 

creating base64

in case of base64 characterset:

nscharacterset *urlbase64characterset = [[nscharacterset charactersetwithcharactersinstring:@"/+=\n"] invertedset]; 

note: stringbyaddingpercentencodingwithallowedcharacters encode utf-8 characters requiring encoding.

example verify characters in set:

void characterinset(nscharacterset *set) {     nsmutablestring *characters = [nsmutablestring new];     nscharacterset *invertedset = set.invertedset;     (int i=32; i<127; i++) {         if ([invertedset characterismember:(unichar)i]) {             nsstring *c = [[nsstring alloc] initwithbytes:&i length:1 encoding:nsutf8stringencoding];             [characters appendstring:c];         }     }     printf("characters not in set: '%s'\n", [characters utf8string]); } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -