Reputation: 87
Below is my code that attempt to encrypt the data
input/invoke my encryption method:
[self myED:@"wcc123" :@"hahaha" :@"yyyy"];
- (NSData*) myED:(NSData*)data :(NSData*) key :(NSData*)iv{
@try {
// Try something
NSLog( @"Original String: %@", data );
size_t bufferSize = [data length]*2;
void *buffer = malloc(bufferSize);
size_t encryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
[key bytes], [key length], [iv bytes], [data bytes], [data length],
buffer, bufferSize, &encryptedSize);
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
@finally {
// Added to show finally works as well
}
return nil;
}
However, when try to run this code and it prompt me exception below
2012-07-03 16:52:44.776 wccTest[930:f803] Exception: -[__NSCFConstantString bytes]:
Can anyone help/advice on this?
Upvotes: 0
Views: 1008
Reputation: 33428
Following @borrrden comment I will also give a self-explicative name to your method. For example:
- (NSData*)encryptEDData:(NSData*)data withKey:(NSData*)key initVector:(NSData*)iv {
// same as before
}
So, the selector
for this method will be encryptEDData:withKey:initVector:
.
You can call that method like:
NSData* edData = //...
NSData* keyData = //...
NSData* ivData = //...
[self encryptEDData:edData withKey:keyData initVector:ivData];
Furthermore, I would perform the NSString
to NSData
conversion within the method leaving it to accept strings. I think it allows to understand better the goal for that method.
Hope that helps.
Upvotes: 0
Reputation: 14304
Instead of
[self myED:@"wcc123" :@"hahaha" :@"yyyy"];
Try calling
[self myED:[@"wcc123" dataUsingEncoding:NSUTF8StringEncoding] :[@"hahaha" dataUsingEncoding:NSUTF8StringEncoding] :[@"yyyy" dataUsingEncoding:NSUTF8StringEncoding]];
Upvotes: 2