seguridad realidad desencriptar copia aumentada apps ios encryption uiimage aes

realidad - Encriptar/descifrar para imagen en IOS



desencriptar copia de seguridad iphone (1)

Estamos trabajando con encriptar / descifrar y UIIMAGE. Si estamos encriptando y descifrando y UIIMAge sin guardar en la galería de iphone, funciona bien, pero si encriptamos, guardamos en la galería, cargamos (la imagen encriptada) en la aplicación y desciframos que funciona mal.

Estamos usando estas funciones para cifrar / descifrar / guardar / cargar

// cifrar

UIImage *image = self.imageView.image; CGContextRef ctx; CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(height * width * 4); int valor =(height * width * 4); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); NSData *data = [NSData dataWithBytes:(const void *)rawData length:sizeof(unsigned char)*valor]; NSData *encryptedData = [data AES256EncryptWithKey:@"/"thisIsASecre"]; rawData = [encryptedData bytes]; NSData *dataData2 = [NSData dataWithBytes:rawData length:sizeof(rawData)]; ctx = CGBitmapContextCreate(rawData, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ), kCGImageAlphaPremultipliedLast ); imageRef = CGBitmapContextCreateImage (ctx); UIImage *rawImage = [UIImage imageWithCGImage:imageRef]; self.imageView.image = rawImage; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); CGContextRelease(ctx);

// descifrar

UIImage *image = self.imageView.image; CGContextRef ctx; CGImageRef imageRef = [image CGImage]; NSUInteger width = CGImageGetWidth(imageRef); NSUInteger height = CGImageGetHeight(imageRef); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); unsigned char *rawData = malloc(height * width * 4); int valor =(height * width * 4); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); NSData *data = [NSData dataWithBytes:(const void *)rawData length:sizeof(unsigned char)*valor]; NSData *encryptedData = [data AES256DecryptWithKey:@"/"thisIsASecre"]; rawData = [encryptedData bytes]; ctx = CGBitmapContextCreate(rawData, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ), kCGImageAlphaPremultipliedLast ); imageRef = CGBitmapContextCreateImage (ctx); UIImage *rawImage = [UIImage imageWithCGImage:imageRef]; self.imageView.image = rawImage; image = rawImage; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); CGContextRelease(ctx);

// Cargar imagen

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL]; - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { self.imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; [picker dismissViewControllerAnimated:YES completion:NULL]; }

// Y las clases que uso para encriptar / descifrar

- (NSData *)AES256EncryptWithKey:(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]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That''s why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted]; } free(buffer); //free the buffer; return nil; } - (NSData *)AES256DecryptWithKey:(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]; NSUInteger dataLength = [self length]; //See the doc: For block ciphers, the output size will always be less than or //equal to the input size plus the size of one block. //That''s why we need to add the size of one block here size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); size_t numBytesDecrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES256, NULL /* initialization vector (optional) */, [self bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesDecrypted); if (cryptStatus == kCCSuccess) { //the returned NSData takes ownership of the buffer and will free it on deallocation return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted]; } free(buffer); //free the buffer; return nil; }

Alguien sabe cuál es el problema?

¡Thxs!


Si guarda una imagen de mapa de bits en la biblioteca de fotos, se almacena como un JPG. El formato con pérdida de JPG completamente manguera sobre su proceso de encriptación. Intenta convertir tu imagen de mapa de bits en una imagen PNG y luego guardarla:

UIImage* pngImage = [UIImage imageWithData:UIImagePNGRepresentation(rawImage)]; UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, NULL);