我的 AES 方法(下面的代码)仅适用于自加密数据。我不知道哪里出错了。你能建议我一些框架或更好的实现吗?
- (NSData *)aesOperation:(CCOperation)op OnData:(NSData *)data key:(NSString*)inKey {
NSData *outData = nil;
int bufferSize = 6000000;
NSLog(@"AES InData: %@",data);
if (inKey == NULL) {
NSLog(@"A klucz to chcesz wyczarować? Podaj klucz do zaszyfrowanego pliku");
return NULL;
}
const void *key = inKey ;
const void *dataIn = data.bytes;
size_t dataInLength = data.length;
// Data out parameters
size_t outMoved = 0;
unsigned char outBuffer[bufferSize];
memset(outBuffer, 0, bufferSize);
CCCryptorStatus status = -1;
status = CCCrypt(op, kCCAlgorithmAES128, kCCOptionECBMode, key, kCCKeySizeAES128, NULL,
dataIn, dataInLength, &outBuffer, bufferSize, &outMoved);
if(status == kCCSuccess) {
outData = [NSData dataWithBytes:outBuffer length:outMoved];
} else if(status == kCCBufferTooSmall) {
size_t newsSize = outMoved;
void *dynOutBuffer = malloc(newsSize);
memset(dynOutBuffer, 0, newsSize);
outMoved = 0;
status = CCCrypt(op,kCCAlgorithmAES128,kCCOptionECBMode,key,kCCKeySizeAES128,NULL,dataIn, dataInLength, &outBuffer, bufferSize, &outMoved);
if(status == kCCSuccess) {
outData = [NSData dataWithBytes:outBuffer length:outMoved];
}
}
return outData;
}