我正在实现兼容的二进制文件加密/解密.NET and Objective-C apps
。我正在使用RNCryptor
包装Objective-C
。据我所知,我可以encrypt/decrypt
字符串,但文件加密让我很困扰。问题是,当我读取文件并加密其数据并将其写入文件时,它不会在.NET app
. 如果我计算加密数据的 Base64 字符串并在 .NET 中解密它 - 从 Base64 字符串创建字节数组并使用与文件相同的登录名解密,它会解密。Objective C and .NET
在CryptoStream中将加密数据写入文件有什么区别?
还是我缺少一些基本的东西?在目标 C 中加密文件的代码是:-
RNEncryptor *encryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
password:password
handler:^(RNCryptor *cryptor, NSData *data) {
@autoreleasepool
{
NSLog(@"6length of out data %d",[data length]);
[encodedText appendString:[data base64EncodingWithLineLength:0]];
[outputStream write:data.bytes maxLength:data.length];
dispatch_semaphore_signal(semaphore);
data = nil;
if (cryptor.isFinished)
{
[outputStream close];
NSLog(@"my encryptedText %@",encodedText);
encryptionError = cryptor.error;
// call my delegate that I'm finished with decrypting
}
}
}];
encryptor.filesize=[attrs fileSize];
while (inputStream.hasBytesAvailable)
{
@autoreleasepool
{
uint8_t buf[blockSize];
NSUInteger bytesRead = [inputStream read:buf maxLength:blockSize];
if (bytesRead > 0)
{
NSData *data = [NSData dataWithBytes:buf length:bytesRead];
}
total = total + bytesRead;
[encryptor addData:data];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
}
}
[inputStream close];
[encryptor finish];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
解密
[encodedText appendString:[data base64EncodingWithLineLength:0]];
总是有效,但文件不解密。