我正在使用以下代码来混淆我的测试应用程序的密码。
- (NSString *)obfuscate:(NSString *)string withKey:(NSString *)key
{
// Create data object from the string
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// Get pointer to data to obfuscate
char *dataPtr = (char *) [data bytes];
// Get pointer to key data
char *keyData = (char *) [[key dataUsingEncoding:NSUTF8StringEncoding] bytes];
// Points to each char in sequence in the key
char *keyPtr = keyData;
int keyIndex = 0;
// For each character in data, xor with current value in key
for (int x = 0; x < [data length]; x++)
{
// Replace current character in data with
// current character xor'd with current key value.
// Bump each pointer to the next character
*dataPtr = *dataPtr++ ^ *keyPtr++;
// If at end of key data, reset count and
// set key pointer back to start of key value
if (++keyIndex == [key length])
keyIndex = 0, keyPtr = keyData;
}
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
这就像所有字符串的魅力一样,但是比较以下结果时我遇到了一些问题
NSLog([[self obfuscate:@"0000", @"maki"]); //Returns 0]<W
NSLog([[self obfuscate:@"0809", @"maki"]); //Returns 0]<W
如您所见,两个带数字的字符串虽然不同,但返回相同的结果!我附加的代码有什么问题导致这两个数字的结果相同?
另一个例子:
NSLog([self obfuscate:@"8000" withKey:@"maki"]); //Returns 8U4_
NSLog([self obfuscate:@"8290" withKey:@"maki"]); //Returns 8U4_ as well
我可能误解了混淆的概念,但我的印象是每个唯一的字符串都返回一个唯一的混淆字符串!
请帮我修复这个错误/故障代码源:http: //iosdevelopertips.com/cocoa/obfuscation-encryption-of-string-nsstring.html