Objective-c:读取在Windows上加密的文件 [英] Objective-c: read file encrypted on Windows
问题描述
我正在使用Objective-C处理OSX应用程序,而我需要做的一件事就是使用简单的位移算法在Windows机器上读取加密文本/ xml文件。 Windows的加密代码在Delphi中相当简单:
I am working on an OSX application using Objective-C, and one of the things I need to do is read text/xml files which were encrypted on a Windows machine using a simple bit-shift algorithm. The encryption code on the Windows side is fairly simple, in Delphi:
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的各种方法,没有任何成功。
The problem is I can't figure out how to properly read and decrypt this on the Mac side. I've tried various approaches to using NSData with no success whatsoever.
任何帮助或建议将不胜感激。
Any help or suggestions would be greatly appreciated.
推荐答案
可能这将有助于您(简单的xor加密):
May be this would help you (simple xor encrypting):
-(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, ^
xor。在C中右移也是>>
,而不是运算符是!
。
here:
currentCipher
is your EcrytpKey, ^
xor. also shift right in C is >>
, not operator is !
.
这篇关于Objective-c:读取在Windows上加密的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!