2012-02-05 68 views
0

我正在使用Objective-C開發OSX應用程序,我需要做的事情之一是讀取在Windows機器上使用簡單位加密的text/xml文件移位算法。在Windows端加密代碼非常簡單,在德爾福:Objective-c:讀取在Windows上加密的文件

const 
    EncryptKey : word = ????; 
var 
    InMS : TMemoryStream; 
    cnt : Integer; 
    c  : byte; 
begin 
    InMS := TMemoryStream.Create; 
    result := TMemoryStream.Create; 
    try 
    InMS.LoadFromFile(FileName); 
    InMS.Position := 0; 
    for cnt := 0 to InMS.Size - 1 do 
     begin 
     InMS.Read(c, 1); 
     c := (c xor not (ord(EncryptKey shr cnt))); 
     result.Write(c, 1); 
     end; 
    finally 
    InMS.Free; 
    end; 
end; 

問題是我無法弄清楚如何正確閱讀和解密這在Mac上側。我嘗試過使用NSData的各種方法,但沒有成功。

任何幫助或建議將不勝感激。

回答

1

可能,這將幫助你(簡單的XOR加密):

-(void) decryptData :(NSMutableData *) data{ 
    unsigned char *bytes = (unsigned char *)malloc([data length]); 
    unsigned char magic[4] = {(currentCipher >> 24) & 0xff,(currentCipher >> 16) & 0xff,(currentCipher >> 8) & 0xff,(currentCipher) & 0xff}; 
    [data getBytes:bytes]; 
    int magic_pointer = 0; 
    for (int i = 16; i < [data length]; i++) { 
     bytes[i] ^= magic[magic_pointer];   
     if (magic_pointer == 3) magic_pointer = 0; else magic_pointer++; 
    } 
    free(bytes); 
    [data setData:[NSMutableData dataWithBytes : bytes length: [data length] ]]; 
} 

這裏: currentCipher是你EcrytpKey,^異。在C中右移也是>>,而不是運營商是!

+0

謝謝你的例子。我會花時間完成它,並得到結果,問題等。 – 2012-02-07 02:20:14

+0

我終於有時間坐下來,讓它工作。你的代碼是一個很好的幫助。一張紙條;按位NOT運算符是**〜**不是**!**。代碼如下所示: NSMutableData * fileData = [NSMutableData dataWithContentsOfFile:dataPath]; UInt16 currentCipher = ????; unsigned char * bytes =(unsigned char *)malloc([fileData length]); [fileData getBytes:bytes];對於(int i = 0; i <[fileData length]; i ++){ bytes [i]^=〜(currentCipher >> i); } [fileData setData:[NSMutableData dataWithBytes:bytes length:[fileData length]]]; 再次感謝。 – 2012-02-12 21:04:37