gpt4 book ai didi

objective-c - NSCharacterSet URLHostAllowedCharacterSet 不替换 '+' 符号?

转载 作者:太空狗 更新时间:2023-10-30 03:21:17 26 4
gpt4 key购买 nike

我正在努力通过网络传输长加密字符串并让它们在服务器上正确输出。例如,我在客户端上有这个加密字符串:

wcWSERZCh8Xm1hpbNo1kSD1LvFmpuUr4wmq9hQUWeK0vYcLeFPGwFR/sBTES1A4rPV6eyp9nzEEU9uKkiFSTdP+SPOSqUf6evjf3WRHrXMRe81lIrHuRyk0iRwoNe5uIk+VlpR41kETmznXa4+gELmf53r7oayRkkffnIPDmpO+WbgE0VL3PQeOsXB01tWJyDiBIsz5WJiiEIm3ZoJW/sw==

如您所见,它有一些字符如果没有一些 URL 编码(+/,最显着)将无法通过网络传输。我不完全确定在其他情况下是否会出现其他字符,因此我想确保我的解决方案“普遍”正确。我正在使用这一行:

NSString *escapedString = [cipherString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

这是我在高度评价的 answer 中找到的.

但是,我在服务器端解密时仍然遇到问题,所以我在发送前立即在客户端打印了结果,我看到了这个:

wcWSERZCh8Xm1hpbNo1kSD1LvFmpuUr4wmq9hQUWeK0vYcLeFPGwFR%2FsBTES1A4rPV6eyp9nzEEU9uKkiFSTdP+SPOSqUf6evjf3WRHrXMRe81lIrHuRyk0iRwoNe5uIk+VlpR41kETmznXa4+gELmf53r7oayRkkffnIPDmpO+WbgE0VL3PQeOsXB01tWJyDiBIsz5WJiiEIm3ZoJW%2Fsw==

为什么“+”号仍然存在?我使用了错误的允许字符集吗?我应该使用哪个字符集来保证正确转义所有有问题的字符?

如果有帮助,下面是我用来加密纯文本字符串的代码。完成后,我在通过网络发送之前对结果进行 base64 编码:

- (NSData *)phpEncryptCleartext : (NSData *)cleartext
{
NSData *cleartextPadded = [self phpPadData:cleartext];

CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0; // Number of bytes moved to 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;
}

感谢您的任何建议!

最佳答案

Swift version here.

要转义字符,请使用 stringByAddingPercentEncodingWithAllowedCharacters:

NSString *URLEscapedString =
[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

以下是有用的字符集,实际上是集里没有的字符:

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`

或者创建您自己的字符集,只包含您需要转义的字符。

NSCharacterSet *customCharacterset = [[NSCharacterSet characterSetWithCharactersInString:@"your characters"] invertedSet];

结合以上所有内容创建一个字符集:

NSCharacterSet *URLFullCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@" \"#%/:<>?@[\\]^`{|}"] invertedSet];

创建一个 Base64

如果是Base64字符集:

NSCharacterSet *URLBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"/+=\n"] invertedSet];

注意:stringByAddingPercentEncodingWithAllowedCharacters 也会对需要编码的 UTF-8 字符进行编码。

验证集合中字符的示例:

void characterInSet(NSCharacterSet *set) {
NSMutableString *characters = [NSMutableString new];
NSCharacterSet *invertedSet = set.invertedSet;
for (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]);
}

关于objective-c - NSCharacterSet URLHostAllowedCharacterSet 不替换 '+' 符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com