【问题标题】:Encrypt / Decrypt UIImage in Swift using AES Encryption使用 AES 加密在 Swift 中加密/解密 UIImage
【发布时间】:2016-04-29 02:52:44
【问题描述】:

我捎带了Encrypt/decrypt for Image on IOS UIImage 加密策略,它使用 Obj-c 来实现我在 Swift 中寻找的东西。现在,请忽略那里的“保存到库”问题,因为我在应用程序本身中解密时遇到问题。

加密步骤似乎工作正常,我确实将加密图像发送到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

}

【问题讨论】:

    标签: swift


    【解决方案1】:

    上面代码的问题是我假设了图像的 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
    }
    

    【讨论】:

    • 你到底应该如何在 Swift 4 中执行 var context = CGBitmapContextCreate...?我的 Xcode 正在生成各种更正,并将我引向应用程序崩溃的花园路径。有时我绝对讨厌斯威夫特做废话的难度。我几乎拥有它。谢谢你的代码——我当然希望我能弄清楚斯威夫特到底想让我怎么更新它!
    • @andrew 你能复制最新的 swift 版本吗?
    猜你喜欢
    • 1970-01-01
    • 2013-06-22
    • 2012-02-24
    • 1970-01-01
    • 2016-09-22
    • 2015-01-20
    • 2014-01-05
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多