30/06/10
How to encrypt/decrypt using AES 256
Here is some code I found in iphonedevelopment.blogspot.com on how to extend NSMutableData to encrypt and decrypt using AES 256 encryption standard, very useful.
#import <CommonCrypto/CommonCryptor.h>
@implementation NSMutableData (AES256)
- (BOOL) encryptWithKey: (NSString *) key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char * keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding)
// fetch key data
[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];
// encrypts in-place, since this is a mutable data object
size_t numBytesEncrypted = 0;
CCCryptorStatus result = CCCrypt( kCCEncrypt, , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self mutableBytes], [self length], /* input */
[self mutableBytes], [self length], /* output */
&numBytesEncrypted );
return ( result == kCCSuccess );
}
- (BOOL) decryptWithKey: (NSString *) key {
// ‘key’ should be 32 bytes for AES256, will be null-padded otherwise
char * keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero( keyPtr, sizeof(keyPtr) ); // fill with zeroes (for padding)
// fetch key data
[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF8StringEncoding];
// encrypts in-place, since this is a mutable data object
size_t numBytesEncrypted = 0;
CCCryptorStatus result = CCCrypt( kCCDecrypt, , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self mutableBytes], [self length], /* input */
[self mutableBytes], [self length], /* output */
&numBytesEncrypted );
return ( result == kCCSuccess );
}
@end
NSString to NSData
NSData to NSString
29/06/10
Debugging with NSLog: format specifiers
NSLog is a Foundation kit function for printing debug statements to the console. It’s very helpful to debug your applications, but sometimes you need to print some special kind of data. These are its format specifiers:
%@ Object
%d, %i signed int
%u unsigned int
%f float/double
%x, %X hexadecimal int
%o octal int
%zu size_t
%p pointer
%e float/double (in scientific notation)
%g float/double (as %f or %e, depending on value)
%s C string (bytes)
%S C string (unichar)
%.*s Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c character
%C unichar
%lld long long
%llu unsigned long long
%Lf long double
23/06/10
UIImage negative color effect
It’s simple to convert an UIImage to his negative image. Only use this code:
UIGraphicsBeginImageContext(renderImage.size);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
[renderImage drawInRect:CGRectMake(0, 0, renderImage.size.width, renderImage.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, renderImage.size.width, renderImage.size.height));
renderImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();