在IOS上加密/解密图像
我们正在使用加密/解密和UIIMAGE。如果我们加密、解密并在不保存到iphone gallery的情况下使用UIIMAge,那么效果很好,但如果我们加密、保存到gallery、将(加密的图像)加载到应用程序中并对其进行解密,效果就不好 我们使用这个函数来加密/解密/保存/加载 //加密在IOS上加密/解密图像,ios,encryption,uiimage,aes,Ios,Encryption,Uiimage,Aes,我们正在使用加密/解密和UIIMAGE。如果我们加密、解密并在不保存到iphone gallery的情况下使用UIIMAge,那么效果很好,但如果我们加密、保存到gallery、将(加密的图像)加载到应用程序中并对其进行解密,效果就不好 我们使用这个函数来加密/解密/保存/加载 //加密 UIImage *image = self.imageView.image; CGContextRef ctx; CGImageRef imageRef = [image CGImage]; NSUIntege
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);
//解密
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);
//加载图像
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];
}
//还有我用来加密/解密的密码
- (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;
}
有人知道有什么问题吗
谢谢 如果将位图图像保存到照片库中,它将存储为JPG。JPG的有损格式将完全覆盖您的加密过程。尝试将位图图像转换为PNG图像,然后保存:
UIImage* pngImage = [UIImage imageWithData:UIImagePNGRepresentation(rawImage)];
UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, NULL);
@在我看来,rmaddy好像在保存一个有效的图像。他对图像像素进行加密,然后创建一个具有基本随机像素的图像,并将其存储在照片库中。(并不是说他不应该检查错误返回:)存储在照片库中的图像不总是转换为jpg吗?在这种情况下,您将进行有损压缩,并且无法期望您的加密/解密周期继续存在。@rmaddy他没有加密png或jpg。他正在加密像素数据本身。本质上,他只是随机化位图中各个像素的颜色。然后,他将随机像素转换为CGImage和UIImage,因此他得到的是一幅有效的图像(尽管看起来不是特别有用)@David Oops-我完全误解了这幅图像。谢谢。你有没有验证过,你可以在没有照片库的情况下完成加密/解密周期,恢复原始照片吗?您正在将位图图像存储到库中,它可能存储为JPG。你有没有试过在UIImagePNGRE演示文稿中强制循环图像?首先,感谢你花时间@David and rmaddy。我以前(现在)试过这样做,但现在不起作用。以你的例子来说,我可以加密(并像png一样保存),但如果我从库中拍摄加密的照片或UIImage中的图像不起作用。你是否验证过你可以通过加密/解密往返并取回你的原始照片?-首先,我从相机或图库中拍摄照片并加密。我可以在UIImage中看到加密的照片,我保存了它,效果很好。如果你去画廊,我可以看到加密的照片第二,使用加密的图像(来自UIImage without access gallery),我正确解密(我看到图像“orginal”)并像原始图像一样保存到gallery中。但是如果我做了第二步,从图库中拍摄加密照片,那么我就不再工作了。我知道当你保存到图库中时一定有问题,可能是压缩了照片。@cserrao你知道这个问题吗?