Swift中的图像加密 [英] Image Encryption in Swift

查看:154
本文介绍了Swift中的图像加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IDZSwiftCommonCrypto使用StreamCryptor进行图像加密,该示例在其GitHub页面上进行了描述: https://github.com/iosdevzone/IDZSwiftCommonCrypto

I am using IDZSwiftCommonCrypto for image encryption using StreamCryptor described as an example at its GitHub page: https://github.com/iosdevzone/IDZSwiftCommonCrypto

我无法成功解密.这是我用于加密和解密的代码(imageData来自UIImageView).加密后的输出与输入不同(imageData与xx不同).

I am not able to successfully decrypt. Here is my code for encryption and decryption (imageData comes from UIImageView). The output is different from input after encryption (imageData is different from xx).

加密:

func performImageEncryption(imageData: Data) -> Void {

        var inputStream = InputStream(data: imageData)

        let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")

        var sc = StreamCryptor(operation:.encrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:Array<UInt8>())

        var inputBuffer = Array<UInt8>(repeating:0, count:1024)
        var outputBuffer = Array<UInt8>(repeating:0, count:1024)

        inputStream.open()

        var cryptedBytes = 0

        var xx = Data()

        var count = 0

        while inputStream.hasBytesAvailable
        {
            count = count + 1024
            let bytesRead = inputStream.read(&inputBuffer, maxLength: inputBuffer.count)
            let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)

            xx.append(contentsOf: outputBuffer)
        }

        let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)


        xx.append(contentsOf: outputBuffer)


        inputStream.close()

        performImageDecryption(encryptedImageData: xx)


    }

解密:

func performImageDecryption(encryptedImageData: Data) -> Void {

        let key = arrayFrom(hexString: "2b7e151628aed2a6abf7158809cf4f3c")

        var sc = StreamCryptor(operation:.decrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:Array<UInt8>())

        var inputStreamD = InputStream(data: encryptedImageData)

        var inputBuffer = Array<UInt8>(repeating:0, count:1024)
        var outputBuffer = Array<UInt8>(repeating:0, count:1024)

        inputStreamD.open()

        var cryptedBytes = 0


        var xx = Data()
        while inputStreamD.hasBytesAvailable
        {
            let bytesRead = inputStreamD.read(&inputBuffer, maxLength: inputBuffer.count)
            let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
            xx.append(contentsOf: outputBuffer)
        }

        let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
        xx.append(contentsOf: outputBuffer)

        inputStreamD.close()

    }

推荐答案

xx.append(outputBuffer, count: cryptedBytes)

应该有帮助.

下面是示例代码,用于拾取加密的图像文件并返回数据.

below is sample code for picking up an encrypted image file and returning the data.

func decryptImage(from path:URL)-> Data? {
    var decryptData = Data()

    let sc = StreamCryptor(operation:.decrypt, algorithm:.aes, options:.PKCS7Padding, key:key, iv:iv)

    guard let encryptedInputStream = InputStream(fileAtPath: path.relativePath) else {
        return nil
    }

    var inputBuffer = [UInt8](repeating: 0, count: Int(1024))
    var outputBuffer = [UInt8](repeating: 0, count: Int(1024))

    encryptedInputStream.open()

    var cryptedBytes : Int = 0
    while encryptedInputStream.hasBytesAvailable
    {
        let bytesRead = encryptedInputStream.read(&inputBuffer, maxLength: inputBuffer.count)

        let status = sc.update(bufferIn: inputBuffer, byteCountIn: bytesRead, bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)

        if (status != Status.success) {
        encryptedInputStream.close()
            return nil
        }

        if(cryptedBytes > 0)
        {
            decryptData.append(outputBuffer, count: cryptedBytes)
        }
    }

    let status = sc.final(bufferOut: &outputBuffer, byteCapacityOut: outputBuffer.count, byteCountOut: &cryptedBytes)
    if (status != Status.success) {
        encryptedInputStream.close()
        return nil
    }

    if(cryptedBytes > 0)
    {
        decryptData.append(outputBuffer, count: cryptedBytes)
    }
    encryptedInputStream.close()
    return decryptData
}

快乐编码:)

这篇关于Swift中的图像加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆