【发布时间】: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