2015-01-06 2 views
2

Objet-c를 사용하여 Swift에서 찾고있는 것을 달성하는 UIImage 암호화 전략 Encrypt/decrypt for Image on IOS을 피기 백합니다. 지금은 앱 자체에서 해독하는 데 문제가 있으므로 "라이브러리에 저장"문제를 무시하십시오.AES 암호화를 사용하여 Swift에서 UIImage 암호화/암호 해독

암호화 단계 는에 작업 벌금을 것, 그리고 내가 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 

} 

답변

1

위의 코드와 문제는 내가 추가 연구에 바보 같은 실수 이미지의 알파 채널을 가정이었다. 나는 CGImageAlphaInfo.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 
} 
+0

어떻게 스위프 4에서 var context = CGBitmapContextCreate ...를 수행해야합니까? 내 Xcode는 모든 종류의 수정 사항을 생성하고 정원 경로를 따라 앱 충돌을 유도합니다. 때로는 Swift가 얼마나 쓰레기를 만드는지 정말 싫어합니다. 나는 거의 그것을 가지고 있었다. 코드를 보내 주셔서 감사합니다 - 확실히 내가 도대체 ​​Swift가 나를 괴롭 히고 싶어한다는 것을 알아낼 수 있기를 바랍니다! –

+0

@ andrew는 최신의 신속한 버전으로 복제 할 수 있었습니까? –