使用 AES 加密在 Swift 中加密/解密 UIImage

Encrypt / Decrypt UIImage in Swift using AES Encryption

我正在使用 Encrypt/decrypt for Image on IOS UIImage 加密策略,它使用 Obj-c 来实现我在 Swift 中寻找的东西。现在,请忽略那里的 "save to library" 问题,因为我在应用程序本身的解密方面遇到问题。

加密步骤 似乎 工作正常,我确实将加密图像输出到 imageView.image,但是当我尝试解密时,我得到了另一张图像待加密,永不回原.png图片。

关于我哪里出错了有什么想法吗? AES 加密文件在这里:https://github.com/alexeypro/EncryptDecrypt

加密/解密:

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

}

上面代码的问题是我假设了图像的 alpha 通道,经过进一步研究这是一个愚蠢的错误。我用变量 let alphaInfo = CGImageGetAlphaInfo(imageRef) 替换了假定的 CGImageAlphaInfo.PremultipliedLast alpha 通道,并将该变量传递给两个 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
}