7
我正在開發適用於iOS的ePub閱讀器。我從服務器上下載的ePub文件使用XOR算法進行加密。我使用密鑰將這些文件作爲.xlsx格式進行解密。我正在解密並將文件加載到閱讀器中,如下所示:iOS中的異或文件加密
- 從下載的目錄加載文件作爲NSData。
- 使用密鑰解密數據。
- 將解密的數據寫入臨時目錄。
- 將文件從臨時目錄加載到閱讀器。
我正在使用AePubReader來加載文件。
這裏是解密代碼:
- (NSData *)obfuscate:(NSData *)data withKey:(NSString *)key
{
NSMutableData *result = [data mutableCopy];
// Get pointer to data to obfuscate
char *dataPtr = (char *) [result mutableBytes];
// 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 result;
}
但是,當我試圖解密的文件加載到讀者,我如下得到一個錯誤:
2012-07-30 20:45:12.652 XYX[5986:12203] ERROR: ePub not Valid
2012-07-30 20:45:12.652 XYX[5986:12203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
我查了url路徑,它不是空的,甚至檢查文件是否存在路徑。文件存在於指定的路徑中。
我可能會出錯???任何幫助將不勝感激。提前致謝。
是不是這個UB:'* dataPtr = * dataPtr ++^* keyPtr ++;'? – 2012-07-30 16:06:45
@PaulR:對不起......我沒有得到UB的實際情況......你能解釋一下嗎? – PETER 2012-07-30 17:07:23
請參閱http://en.wikipedia.org/wiki/Sequence_point,瞭解序列點的說明以及規則斷開時的未定義行爲。 – 2012-07-30 17:10:23