【发布时间】:2013-09-06 13:32:21
【问题描述】:
我正在.NET and Objective-C apps 之间实现兼容的二进制文件加密/解密。我在Objective-C 端使用RNCryptor 包。据我所知,我可以encrypt/decrypt 字符串,但文件加密让我很困扰。问题是,当我读取文件并加密其数据并将其写入文件时,它不会在.NET app 中解密。如果我计算加密数据的 Base64 字符串并在 .NET 中解密它 - 从 Base64 字符串创建字节数组并使用与文件相同的登录名解密,它会解密。
将加密数据写入Objective C and .NETCryptoStream?中的文件有什么区别
或者我错过了一些基本的东西? 在目标 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]];
始终有效,但文件无法解密。
【问题讨论】:
标签: .net objective-c encryption cryptography aes