Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标c:读取Windows上加密的文件_Objective C_Delphi_Encryption - Fatal编程技术网

Objective c 目标c:读取Windows上加密的文件

Objective c 目标c:读取Windows上加密的文件,objective-c,delphi,encryption,Objective C,Delphi,Encryption,我正在使用Objective-C开发一个OSX应用程序,我需要做的一件事是读取在Windows机器上使用简单的位移位算法加密的文本/xml文件。在Delphi中,Windows端的加密代码相当简单: const EncryptKey : word = ????; var InMS : TMemoryStream; cnt : Integer; c : byte; begin InMS := TMemoryStream.Create; result :

我正在使用Objective-C开发一个OSX应用程序,我需要做的一件事是读取在Windows机器上使用简单的位移位算法加密的文本/xml文件。在Delphi中,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,但都没有成功


如果您有任何帮助或建议,我们将不胜感激。

这可能会对您有所帮助(简单的xor加密):

-(void)解密数据:(NSMutableData*)数据{
无符号字符*字节=(无符号字符*)malloc([数据长度]);
无符号字符魔术[4]={(currentCipher>>24)&0xff,(currentCipher>>16)&0xff,(currentCipher>>8)&0xff,(currentCipher)&0xff};
[数据获取字节:字节];
int-magic_指针=0;
对于(int i=16;i<[数据长度];i++){
字节[i]^=magic[magic_指针];
if(magic_pointer==3)magic_pointer=0;else magic_pointer++;
}
空闲(字节);
[data setData:[NSMutableData dataWithBytes:字节长度:[数据长度]];
}
在这里:
currentcippher
是您的EcrytpKey,
^
xor。C中的右移也是
>
,而不是运算符是

谢谢您的示例。我会花时间完成它,然后带着结果、问题等回来。我终于有时间坐下来让它工作了。你的代码帮了大忙。一个音符;按位NOT运算符为~NOT!。代码如下所示:NSMutableData*fileData=[NSMutableData dataWithContentsOfFile:dataPath];UInt16 currentCipher=????;无符号字符*字节=(无符号字符*)malloc([fileData长度]);[fileData getBytes:字节];对于(int i=0;i<[fileData length];i++{bytes[i]^=~(currentCipher>>i);}[fileData setData:[NSMutableData dataWithBytes:bytes length:[fileData length]];再次感谢。
-(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] ]];
}