I am piggybacking off of Encrypt/decrypt for Image on IOS UIImage encryption strategy which uses Obj-c to achieve what I am looking for in Swift. For now, ignore the "save to library" issue there, as I am having issues with decrypting in the app itself.
我正在利用iosuiimage加密策略上的图像加密/解密,该策略使用object -c实现我在Swift中寻找的内容。现在,忽略“保存到库”的问题,因为我在应用程序本身中存在解密问题。
The encryption step seems to work fine, and I do get an encrypted image out to imageView.image
, but when I try to decrypt I get another image which appears to be encrypted, never back to the original .png image.
加密步骤似乎工作得很好,我确实得到了一个加密的图像到imageView。图像,但是当我尝试解密时,我得到了另一个图像,它看起来是加密的,永远不会回到原始的。png图像。
Any thoughts as to where I went wrong? AES encryption files here: https://github.com/alexeypro/EncryptDecrypt
你知道我哪里出错了吗?这里是AES加密文件:https://github.com/alexeypro/EncryptDecrypt
Encryption / Decryption:
加密/解密:
func pixelEncryptDecrypt() {
let image = imageView.image!
var imageRef = image.CGImage
let width = CGImageGetWidth(imageRef)
let height = CGImageGetHeight(imageRef)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel: UInt = 4
let bytesPerRow: UInt = bytesPerPixel * width
let bitsPerComponent: UInt = 8
let sizeOfRawDataInBytes: Int = Int(height * width * 4)
var rawData = UnsafeMutablePointer<Void>.alloc(sizeOfRawDataInBytes)
let context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) | CGBitmapInfo.ByteOrder32Big)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)
var data = NSData(bytes: rawData, length: sizeOfRawDataInBytes)
data = encrypted ? data.AES256DecryptWithKey(key) : data.AES256EncryptWithKey(key)
rawData = data.mutableCopy().mutableBytes
let cryptContext = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue))
imageRef = CGBitmapContextCreateImage(cryptContext);
let encryptedImage = UIImage(CGImage: imageRef)
imageView.image = encryptedImage
encrypted = !encrypted
}
1 个解决方案
#1
1
The issue with the above code was that I assumed the alpha channel of the image, which upon further research was a silly mistake. I replaced the assumed alpha channel of CGImageAlphaInfo.PremultipliedLast
with a variable let alphaInfo = CGImageGetAlphaInfo(imageRef)
and passed that variable to both CGBitmapContextCreate
calls.
上面代码的问题是我假设了图像的alpha通道,经过进一步的研究,这是一个愚蠢的错误。我替换了假设的CGImageAlphaInfo的alpha通道。PremultipliedLast和一个变量let alphaInfo = CGImageGetAlphaInfo(imageRef),并将该变量传递给两个CGBitmapContextCreate调用。
func pixelEncryptDecrypt() {
let image = imageView.image!
var imageRef = image.CGImage
let width = CGImageGetWidth(imageRef)
let height = CGImageGetHeight(imageRef)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel: UInt = 4
let bytesPerRow: UInt = bytesPerPixel * width
let bitsPerComponent: UInt = 8
let alphaInfo = CGImageGetAlphaInfo(imageRef) // this is what solved the issue.
let sizeOfRawDataInBytes: Int = Int(height * width * 4)
var rawData = UnsafeMutablePointer<Void>.alloc(sizeOfRawDataInBytes)
var context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(alphaInfo.rawValue) | CGBitmapInfo.ByteOrder32Big)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)
var data = NSData(bytes: rawData, length: sizeOfRawDataInBytes)
data = encrypted ? data.AES256DecryptWithKey(key) : data.AES256EncryptWithKey(key)
rawData = data.mutableCopy().mutableBytes
context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(alphaInfo.rawValue))
imageRef = CGBitmapContextCreateImage(context);
let encryptedImage = UIImage(CGImage: imageRef)
imageView.image = encryptedImage
encrypted = !encrypted
}
#1
1
The issue with the above code was that I assumed the alpha channel of the image, which upon further research was a silly mistake. I replaced the assumed alpha channel of CGImageAlphaInfo.PremultipliedLast
with a variable let alphaInfo = CGImageGetAlphaInfo(imageRef)
and passed that variable to both CGBitmapContextCreate
calls.
上面代码的问题是我假设了图像的alpha通道,经过进一步的研究,这是一个愚蠢的错误。我替换了假设的CGImageAlphaInfo的alpha通道。PremultipliedLast和一个变量let alphaInfo = CGImageGetAlphaInfo(imageRef),并将该变量传递给两个CGBitmapContextCreate调用。
func pixelEncryptDecrypt() {
let image = imageView.image!
var imageRef = image.CGImage
let width = CGImageGetWidth(imageRef)
let height = CGImageGetHeight(imageRef)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel: UInt = 4
let bytesPerRow: UInt = bytesPerPixel * width
let bitsPerComponent: UInt = 8
let alphaInfo = CGImageGetAlphaInfo(imageRef) // this is what solved the issue.
let sizeOfRawDataInBytes: Int = Int(height * width * 4)
var rawData = UnsafeMutablePointer<Void>.alloc(sizeOfRawDataInBytes)
var context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(alphaInfo.rawValue) | CGBitmapInfo.ByteOrder32Big)
CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), imageRef)
var data = NSData(bytes: rawData, length: sizeOfRawDataInBytes)
data = encrypted ? data.AES256DecryptWithKey(key) : data.AES256EncryptWithKey(key)
rawData = data.mutableCopy().mutableBytes
context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapInfo(alphaInfo.rawValue))
imageRef = CGBitmapContextCreateImage(context);
let encryptedImage = UIImage(CGImage: imageRef)
imageView.image = encryptedImage
encrypted = !encrypted
}